0

我正在尝试从我的订单中获取并设置价格到订单摘要。你会看看我的代码,看看我做错了什么吗?

我的任务要求我使用 arrayList 和 JOptionPane 来列出订单和总数。

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

public class Subway extends JFrame {

    private String[] breadNames = {"9-grain Wheat", "Italian", "Honey Oat", "Herbs and Cheese", "Flatbread"};
    private String[] subTypes = {"Oven Roasted Chicken - $3.50", "Meatball Marinara - $4.50", "Cold Cut Combo - $4.00",
        "BLT - $3.75", "Blackforest Ham - $4.00", "Veggie Delight - $2.50"};
    private String[] cheesetypes = {"Cheddar", "American", "Provolone", "Pepperjack"};
    private String[] size = {"6 inch", "Footlong"};
    private String[] toasted = {"Yes", "No"};
    private JTextField jtfname = new JTextField(10);
    private JComboBox<String> jcbBread = new JComboBox<String>(breadNames);
    private JComboBox<String> jcbtype = new JComboBox<String>(subTypes);
    private JComboBox<String> jcbcheese = new JComboBox<String>(cheesetypes);
    private JButton jbtExit = new JButton("EXIT");
    private JButton jbtAnother = new JButton("Next Order");
    private JButton jbtSubmit = new JButton("SUBMIT");
    private JComboBox<String> jcbSize = new JComboBox<String>(size);
    private JComboBox<String> jcbToasted = new JComboBox<String>(toasted);
    private JCheckBox jcbLettuce = new JCheckBox("Lettuce");
    private JCheckBox jcbSpinach = new JCheckBox("Spinach");
    private JCheckBox jcbOnion = new JCheckBox("Onion");
    private JCheckBox jcbPickles = new JCheckBox("Pickles");
    private JCheckBox jcbTomatoes = new JCheckBox("Tomatoes");
    private JCheckBox jcbPeppers = new JCheckBox("Peppers");
    private JCheckBox jcbMayo = new JCheckBox("Mayo");
    private JCheckBox jcbMustard = new JCheckBox("Mustard");
    private JCheckBox jcbDressing = new JCheckBox("Italian Dressing");

    public Subway() {
        //name
        JPanel p1 = new JPanel(new GridLayout(24, 1));
        p1.add(new JLabel("Enter Name"));
        p1.add(jtfname);
        //size
        p1.add(new JLabel("Select a size"));
        p1.add(jcbSize);
        //bread
        p1.add(new JLabel("Select a Bread"));
        p1.add(jcbBread);
        //type
        p1.add(new JLabel("What type of sub would you like?"));
        p1.add(jcbtype);
        //cheese
        p1.add(new JLabel("Select a cheese"));
        p1.add(jcbcheese);
        //toasted
        p1.add(new JLabel("Would you like it toasted?"));
        p1.add(jcbToasted);
        //toppings
        p1.add(new JLabel("Select your toppings"));
        p1.add(jcbLettuce);
        p1.add(jcbSpinach);
        p1.add(jcbPickles);
        p1.add(jcbOnion);
        p1.add(jcbTomatoes);
        p1.add(jcbPeppers);
        p1.add(jcbMayo);
        p1.add(jcbMustard);
        p1.add(jcbDressing);
        // BUTTON PANEL
        JPanel p5 = new JPanel();
        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p5.add(Box.createHorizontalGlue());// KEEPS THEM HORIZONTAL
        p5.add(jbtExit);
        p5.add(jbtAnother);
        p5.add(jbtSubmit);
        // ADDING PANELS AND WHERE THEY GO
        add(p1, BorderLayout.NORTH);// TOP
        add(p5, BorderLayout.SOUTH);// BOTTOM
        // SETTING INVISIBLE BORDERS AROUND PANELS TO SPACE THEM OUT
        p1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        p5.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        // EXIT BUTTON LISTENER
        jbtExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        // Another order BUTTON LISTENER
        jbtAnother.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                makeASandwich();
            }
        });
        // SUBMIT BUTTON LISTENER
        jbtSubmit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                makeASandwich();
                for (Sandwich s : Sandwich.order) {
                    String Order = " ";
                    Order = jtfname.getText() + "\n"
                            + "\nSize: " + s.getSize()
                            + "\nType of Sub: " + s.getName()
                            + "\nBread: " + s.getBread()
                            + "\nCheese: " + s.getCheese()
                            + "\nToasted? " + s.getToasted() + "\nToppings: \n";
                    if (jcbLettuce.isSelected()) {
                        Order += jcbLettuce.getText();
                    }
                    if (jcbSpinach.isSelected()) {
                        Order += jcbSpinach.getText();
                    }
                    if (jcbPickles.isSelected()) {
                        Order += jcbPickles.getText();
                    }
                    if (jcbOnion.isSelected()) {
                        Order += jcbOnion.getText();
                    }
                    if (jcbTomatoes.isSelected()) {
                        Order += jcbTomatoes.getText();
                    }
                    if (jcbPeppers.isSelected()) {
                        Order += jcbPeppers.getText();
                    }
                    if (jcbMayo.isSelected()) {
                        Order += jcbMayo.getText();
                    }
                    if (jcbMustard.isSelected()) {
                        Order += jcbMustard.getText();
                    }
                    if (jcbDressing.isSelected()) {
                        Order += jcbDressing.getText();
                    }
                    Order +=
                            "\n Price: " + s.getPrice()
                            + "\n\n---Next Order---";
                    JOptionPane.showMessageDialog(null, Order);
                }
            }
        });
    }

    private void makeASandwich() {
        double BLT_Price = 3.00;
        Sandwich sandwich = new Sandwich(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        sandwich.setBread(jcbBread.getItemAt(jcbBread.getSelectedIndex()));
        sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
        sandwich.setToasted(jcbToasted.getItemAt(jcbToasted.getSelectedIndex()));
        sandwich.setSize(jcbSize.getItemAt(jcbSize.getSelectedIndex()));
        //sandwich.setPrice(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        if (jcbtype.getSelectedIndex() == 0) {
            sandwich = new Sandwich(jcbtype.getItemAt(jcbtype.getSelectedIndex()), BLT_Price);
        }
        Sandwich.order.add(sandwich);
    }

    public static void main(String[] args) {
        Subway frame = new Subway();
        frame.pack();
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("SUBWAY");
        frame.setVisible(true);
        frame.setSize(600, 750);
    }// ENDOFMAIN
}// ENDOFCLASS

class HoldOrder {

    public static List<Sandwich> order = new ArrayList<Sandwich>();
}

class Sandwich {

    String bread = "";
    String sandwichName = "";
    String cheese = "";
    String size = "";
    String toasted = "";
    String lettuce = "";
    private String cost = " ";
    public static List<Sandwich> order = new ArrayList<Sandwich>();

    public Sandwich(String typeOfSandwich, double SubPrice) {
        sandwichName = typeOfSandwich;
        cost += SubPrice;
    }

    public String getPrice() {
        return cost;
    }

    public String getName() {
        return sandwichName;
    }

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

    public String getBread() {
        return bread;
    }

    public void setCheese(String s) {
        cheese = s;
    }

    public String getCheese() {
        return cheese;
    }

    public void setSize(String s) {
        size = s;
    }

    public String getSize() {
        return size;
    }

    public void setToasted(String s) {
        toasted = s;
    }

    public String getToasted() {
        return toasted;
    }
//public void setPrice(double s) { 
    //total = 0;
//}
//public double getPrice() { 
    //return total;
//}
}
4

2 回答 2

1

这是一个很难回答的问题,因为它取决于您未共享的代码部分,但基本前提是相同的......

如果价格组合框包含String,您需要将值解析为double...

Object value = jcbtype.getSelectedItem();
double price = Double.parseDouble(value);

sandwich.setPrice(price);

NumberFormatException请注意,如果该值不可解析,这将抛出一个。

如果组合框包含double值(它应该然后用 a 格式化CellRenderer),那么您可能需要强制转换...

Object value = jcbtype.getSelectedItem();
double price = (Double)value;

sandwich.setPrice(price);

JComboBox(我说可能,因为如果您使用的是Java 7,则可以使用泛型返回使用的基本类型,double price = jcbtype.getItemAt(jcbtype.getSelectedIndex())例如)

于 2013-08-05T03:19:12.760 回答
0

在所需类的源代码窗口中。

右键单击 -> Source -> Generate setter and getter ... Eclipse 将为您生成 setter 和 getter 方法。

于 2013-08-05T02:47:29.057 回答