1

我正在为 Subway 订购程序编写代码。我已经布置了 GUI,但不确定我应该如何获取并将所有按钮设置到订单摘要中。摘要需要显示名称、日期、时间、子成分、小计、可能的 2nd order 成分、2nd sub total 和 Grand order 总计。我了解 GUI,但是当涉及到数组和列表时,我不太确定,我需要包含一个数组列表来存储订单。

我是否需要像为三明治和面包那样为蔬菜、奶酪和调味品创建数组类?

任何建议都会很棒。谢谢!

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
4

2 回答 2

2

一些建议:

  • 避免对 JFrame 进行子类化。很少需要这样做,避免这样做可以提高程序的灵活性。
  • orderedSandwiches 变量不应该是静态的。您可能需要对程序进行其他更改以使其成为实例变量。
  • 不要设置 GUI 或组件的大小,也不要使用null布局。虽然对于新手来说,使用空布局和直接放置它们的组件似乎更容易,但他们创建的 GUI 越多,他们就会发现通过这样做他们把自己画在一个角落里。正确使用布局管理器将更容易维护和升级您的 GUI。
  • 将庞大的方法和构造函数分解为更小的更易于管理和可测试的单元。
  • 无需调用System.exit(0),也有充分的理由不调用,因为它似乎使您的程序逻辑短路。
  • 改进您的代码格式,使您的程序更易于阅读和调试,对我们您来说都是如此。同一级别的所有代码块都应该缩进相同的数量(我通常使用 3 个空格)。块之间的一个空白行就足够了。
  • 比向用户扔一堆选项窗格更好,考虑为你的 GUI 的每个子部分创建 JPanel,然后将属于一起的那些合并到一个更大的 JPanel 中,这将允许用户在一个视图上输入多个选择。您的用户会欣赏这一点。
  • 避免使用许多通过副作用(通过直接更改类字段)来改变程序状态的 void 方法,而是更喜欢保留大多数类字段并返回类然后使用的对象的方法。这将大大简化您的错误调试以及更新和改进程序的能力。
  • 你的一些小班似乎有点多余。也许您可以使用一个抽象类 SandwichComponent,它具有名称和成本字段,并将其子类用于特定子类型的组件。
于 2013-08-03T13:06:18.560 回答
2

首先,您应该有一个Order具有SandwichBread实例字段的类,作为将它们分组在一起的一种方式,而不是使用这样的列表。您还应该创建Cheese, Condiment, &c 类并将它们作为实例变量包含在内。

第二,为什么要同时创建frame和扩展JFrame?你只需要做一个或另一个,永远不要两者兼而有之。您可以删除该frame变量并this改为使用。

第三,您可能想注意到 main 中的最后两个语句永远不会完成,因为构造函数的结尾是System.exit(0).

我可以给你其他几千条建议,但如果我走到那一步,我可能会开始变得亵渎神明。

于 2013-08-03T12:01:14.193 回答