2013-08-03 54 views
1

我正在爲地鐵訂購程序編寫代碼。我已經佈置了圖形用戶界面,但不確定如何獲取並設置訂單摘要中的所有按鈕。總結需要顯示名稱,日期,時間,次要成分,小計,可能的二級成分,二級小計,以及一個總訂單。我瞭解GUI,但是當涉及到數組和列表時,我不太確定,我需要包含一個數組列表來存儲訂單。如何從多個JOptionPane中獲取信息並將其放入摘要對話框中

我需要爲蔬菜,奶酪,和調味品創建數組類像我一樣的三明治和麪包?

任何建議將是偉大的。謝謝!

import java.awt.GridLayout; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.*; 


public class SubwayForm extends JFrame 
{ 
    public static List<Sandwich> orderedSandwiches; 
    private List<Sandwich> sandwiches; 
    public static List<Bread> breads; 
    public static List<Bread> orderedBreads; 


    public SubwayForm() { 

      setTitle("Subway"); 
      setSize(250,250); 
      setLayout(null); 
      setLocationRelativeTo(null); 
      CreateSandwiches(); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
      setVisible(true); 

      //FRAME 

      JFrame frame = new JFrame(); 

      //NAME 

      String name = (String) JOptionPane.showInputDialog(frame, 
          "Enter your name", "SUBWAY", JOptionPane.QUESTION_MESSAGE, 
          null, null, null); 

      //SIZE 

     JPanel size = new JPanel(new GridLayout(2,1)); 
     JRadioButton jrbInch = new JRadioButton("6 inch"); 
     JRadioButton jrbFoot = new JRadioButton("12 inch"); 
     size.add(jrbInch); 
     size.add(jrbFoot); 

     JOptionPane.showOptionDialog(frame, size, "Sub Size", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
              null, null); 

      //BREAD 

      JPanel bread = new JPanel(new GridLayout(6,1)); 
      JRadioButton jrbWheat = new JRadioButton("9-Grain Wheat"); 
      JRadioButton jrbHoney = new JRadioButton("Honey Oat"); 
      JRadioButton jrbItalian = new JRadioButton("Italian"); 
      JRadioButton jrbHerbs = new JRadioButton("Italian Herbs & Cheese"); 
      JRadioButton jrbMontChed = new JRadioButton("Monterey Cheddar"); 
      JRadioButton jrbFlat = new JRadioButton("Flatbread"); 
      bread.add(jrbWheat); 
      bread.add(jrbHoney); 
      bread.add(jrbItalian); 
      bread.add(jrbHerbs); 
      bread.add(jrbMontChed); 
      bread.add(jrbFlat); 

      JOptionPane.showOptionDialog(frame, bread, "Choose your bread", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
              null, null); 


      //TYPE OF SUB 

      JPanel type1 = new JPanel(new GridLayout(6,1)); 
      JRadioButton jrbChix = new JRadioButton("Oven Roasted Chicken - $3.00"); 
      JRadioButton jrbMeatball = new JRadioButton("Meatball Marinara - $3.50"); 
      JRadioButton jrbHam = new JRadioButton("Blackforest Ham - $2.25"); 
      JRadioButton jrbBLT = new JRadioButton("BLT - $4.00"); 
      JRadioButton jrbCold = new JRadioButton("Cold Cut - $3.00"); 
      JRadioButton jrbVeggie = new JRadioButton("Veggie Delight - $4.00"); 
      type1.add(jrbChix); 
      type1.add(jrbMeatball); 
      type1.add(jrbHam); 
      type1.add(jrbBLT); 
      type1.add(jrbCold); 
      type1.add(jrbVeggie); 

      JOptionPane.showOptionDialog(frame, type1, "Choose your Sub Type", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
              null, null); 

      //CHEESE 

      JPanel cheese = new JPanel(new GridLayout(6,1)); 
      JRadioButton jrbSwiss = new JRadioButton("Swiss"); 
      JRadioButton jrbProv = new JRadioButton("Provologne"); 
      JRadioButton jrbCheddar = new JRadioButton("Cheddar"); 
      JRadioButton jrbPepper = new JRadioButton("Pepperjack"); 
      JCheckBox jcbExcheese = new JCheckBox("Extra Cheese - $1.00"); 
      cheese.add(jrbSwiss); 
      cheese.add(jrbProv); 
      cheese.add(jrbCheddar); 
      cheese.add(jrbPepper); 
      cheese.add(jcbExcheese); 

      JOptionPane.showOptionDialog(frame, cheese, "Choose your cheese", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); 

      //TOASTED 

      JPanel toasted = new JPanel(new GridLayout(2,1)); 
      JRadioButton jrbToasted = new JRadioButton("Yes"); 
      JRadioButton jrbNottoasted = new JRadioButton("No"); 
      toasted.add(jrbToasted); 
      toasted.add(jrbNottoasted); 

      JOptionPane.showOptionDialog(frame, toasted, "Would you like it toasted?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
              null, null); 

      //VEGGIES 

      JPanel veg = new JPanel(new GridLayout(8,1)); 

      JCheckBox jcbLettuce = new JCheckBox("Lettuce"); 
      JCheckBox jcbSpinach = new JCheckBox("Spinach"); 
      JCheckBox jcbOnion = new JCheckBox("Onion"); 
      JCheckBox jcbPickles = new JCheckBox("Pickles"); 
      JCheckBox jcbTomatoes = new JCheckBox("Tomatoes"); 
      JCheckBox jcbPeppers = new JCheckBox("Peppers"); 
      veg.add(jcbLettuce); 
      veg.add(jcbSpinach); 
      veg.add(jcbPickles); 
      veg.add(jcbOnion); 
      veg.add(jcbTomatoes); 
      veg.add(jcbPeppers); 

      JOptionPane.showOptionDialog(frame, veg, "Choose your veggies", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); 

      //CONDIMENTS 

      JPanel condiments = new JPanel(new GridLayout(8,1)); 

      JCheckBox jcbMayo = new JCheckBox("Mayo"); 
      JCheckBox jcbMustard = new JCheckBox("Mustard"); 
      JCheckBox jcbDressing = new JCheckBox("Italian Dressing"); 
      condiments.add(jcbMayo); 
      condiments.add(jcbMustard); 
      condiments.add(jcbDressing); 

      JOptionPane.showOptionDialog(frame, condiments, "Choose your condiments", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null, null, null); 

      //DO ANOTHER? 

      JPanel another = new JPanel(new GridLayout(2,1)); 
      JRadioButton jrbAnother = new JRadioButton("Yes"); 
      JRadioButton jrbNotanother = new JRadioButton("No"); 
      another.add(jrbAnother); 
      another.add(jrbNotanother); 

      JOptionPane.showOptionDialog(frame, another, "Do you want another?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
              null, null); 

      //SUMMARY 

      JPanel summary = new JPanel(); 
      JOptionPane.showMessageDialog(frame, " Customer: ",null,  JOptionPane.PLAIN_MESSAGE); 
      System.exit(0); 


    }//end of subwayform 

      private void CreateBread() 
      { 
        breads = new ArrayList<Bread>(); 
        Bread orc = new Bread("Italian"); 
        breads.add(orc); 
        breads.add(new Bread("Wheat")); 
        breads.add(new Bread("Honey Oat")); 
        breads.add(new Bread("Herbs and Cheese")); 
        breads.add(new Bread("Cheddar")); 
        breads.add(new Bread("Flatbread")); 
      }//end of bread 

     private void CreateSandwiches() 
     { 
     sandwiches = new ArrayList<Sandwich>(); 
     Sandwich orc = new Sandwich("Oven Roasted Chicken", 3.00); 
     sandwiches.add(orc); 
     sandwiches.add(new Sandwich("Meatball Marinara", 3.50)); 
     sandwiches.add(new Sandwich("Blackforest Ham", 2.25)); 
     sandwiches.add(new Sandwich("BLT", 4.00)); 
     sandwiches.add(new Sandwich("Cold Cut Combo", 3.00)); 
     sandwiches.add(new Sandwich("Veggie Delite", 4.00)); 
     }//end of sandwiches 


     public static void main(String[] args) 
     { 
        new SubwayForm(); 

        orderedSandwiches = new ArrayList<Sandwich>(); 
        orderedBreads = new ArrayList<Bread>(); 

     }  


}//end of subwayform jframe  

class Bread 
{ 
    private String name; 
    private String bread; 

    public Bread(String name) 
    { 
      this.name = name; 
    } 

    public void setBread(String s) 
    { 
      this.bread = s; 
    } 

    public String getBread() 
    { 
      return bread; 
    } 

}// end of bread 


class Sandwich { 

    private String name; 
    private double cost; 


    public Sandwich(String name, double price) 
    { 
      this.name = name; 
      this.cost = price; 
    } 

    public String getName() 
    { 
      return name; 
    } 

    public double getCost() 
    { 
      return cost; 
    } 

}//end of sandwich 

回答

2

首先,你應該有一個Order類,它具有SandwichBread實例字段,如分組在一起,而不是使用像列表的方式。您還應該製作CheeseCondiment,& c類並將它們包括爲實例變量。

其次,你爲什麼創建frame,並在同一時間的延長JFrame?你只需要做一個或另一個,而不是兩個。您可以刪除frame變量,而使用this

第三,你要注意,在主最後兩個語句永遠不會做,因爲構造的到底是System.exit(0)

我可以給你幾千條其他建議,但如果我走了那麼遠,我可能會開始褻瀆。

2

幾點建議:

  • 避免子類的JFrame。很少需要這樣做,並且避免這樣做可以提高程序的靈活性。
  • orderedSandwiches變量不應該是靜態的。您可能需要對程序進行其他更改以使其成爲實例變量。
  • 不要設置GUI或組件的大小,也不要使用null佈局。雖然新手似乎更容易使用null佈局並直接放置組件,但他們創建的GUI越多,他們就會發現通過這樣做他們將自己繪製在一個角落裏。正確使用佈局管理器將使其更易於維護和升級GUI。
  • 將大型方法和構造函數分解爲更小的更易於管理和可測試的單元。
  • 無需調用System.exit(0),並且不會因爲它看起來像是將程序的邏輯短路。
  • 提高代碼的格式,使您的程序更易於閱讀和調試都對我們你。同一級別的所有代碼塊應縮進相同的數量(我通常使用3個空格)。塊之間有一條空白線很多。
  • 不是在用戶拋出了一堆選項窗格中更好地考慮爲GUI的每個子部分創建JPanels,然後結合,在更大的JPanel屬於一起的那些將允許用戶輸入幾個選項​​上一個視圖。您的用戶會喜歡這一點。
  • 避免有許多void方法通過副作用(通過直接更改類字段)來改變程序的狀態,而是傾向於單獨使用大多數類字段的方法,而是返回該類所使用的對象。這將大大減輕您對錯誤的調試以及更新和優化程序的能力。
  • 你的一些小類似乎有點多餘。也許你可以使用一個抽象類SandwichComponent,它有一個名稱和成本字段,並使用它的子類作爲特定子類型的組件。
相關問題