Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

write me a java code: The application must contain two windows 1 The first (main) window…

  1. User

    write me a java code: The application must contain two windows 1 The first (main) window must allow to enter the order of a customer thanks to  Radiobuttons  Buttons  comboBoxes  The window must also contain labels that display the price of each menu Example of an order 2 Small pizzas with 6 toppings + 1 Large pizza with 2 toppings + 2 juices + 2 Pop A button must allow to add the different types of pizza if the customer wants it as in the example above. Once the order is finished, a Pay button opens the second window in which the details of the order are displayed. A total button displays the invoice with taxes (13%). Another button Close allows you to close the second window and return to the main window in order to window to take another order. Here are the prices of the menu Pizza Prices: Small: 6.79Medium:6.79 Medium: 8.29 Large: 9.49ExtraLarge:9.49 Extra Large: 10.29 Party Square: 15.99ToppingSmall:15.99 Topping Small: 1.20 Medium: 1.40Large:1.40 Large: 1.60 Extra Large: 1.80Party:1.80 Party: 2.30 Drinks Pop 1.10Juice1.10 Juice 1.35

  2. Assistant
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    
    public class PizzaOrder {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));
        }
    }
    
    class MainWindow extends JFrame {
        private static final long serialVersionUID = 1L;
    
        // Menu Prices
        static double[] pizzaPrices = {6.79, 8.29, 9.49, 10.29, 15.99};
        static double[] toppingPrices = {1.20, 1.40, 1.60, 1.80, 2.30};
        static double popPrice = 1.10;
        static double juicePrice = 1.35;
    
        JComboBox<Integer> smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum;
        JComboBox<Integer> smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum;
        JComboBox<Integer> popNum, juiceNum;
        JButton payButton;
    
        public MainWindow() {
            setTitle("Pizza Order");
            setLocationRelativeTo(null);
            setSize(400, 500);
            setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 4));
    
            smPizzaNum = addPizzaRow(panel, "Small: $6.79");
            mdPizzaNum = addPizzaRow(panel, "Medium: $8.29");
            lgPizzaNum = addPizzaRow(panel, "Large: $9.49");
            xlgPizzaNum = addPizzaRow(panel, "Extra Large: $10.29");
            partyPizzaNum = addPizzaRow(panel, "Party Square: $15.99");
    
            smToppingsNum = addToppingRow(panel, "Small: $1.20");
            mdToppingsNum = addToppingRow(panel, "Medium: $1.40");
            lgToppingsNum = addToppingRow(panel, "Large: $1.60");
            xlgToppingsNum = addToppingRow(panel, "Extra Large: $1.80");
            partyToppingsNum = addToppingRow(panel, "Party: $2.30");
    
            addDrinkRow(panel);
            payButton = new JButton("Pay");
            payButton.addActionListener(e -> new SecondWindow().setVisible(true));
            panel.add(payButton);
    
            this.add(panel);
        }
    
        private JComboBox<Integer> addPizzaRow(JPanel panel, String label) {
            panel.add(new JLabel(label));
            JComboBox<Integer> numPizza = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(numPizza);
            return numPizza;
        }
    
        private JComboBox<Integer> addToppingRow(JPanel panel, String label) {
            panel.add(new JLabel(label));
            JComboBox<Integer> numToppings = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5, 6});
            panel.add(numToppings);
            return numToppings;
        }
    
        private void addDrinkRow(JPanel panel) {
            panel.add(new JLabel("Pop: $1.10"));
            popNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(popNum);
    
            panel.add(new JLabel("Juice: $1.35"));
            juiceNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(juiceNum);
        }
    }
    
    class SecondWindow extends JDialog {
        private static final long serialVersionUID = 1L;
    
        JTextArea orderDetails;
        JButton totalButton;
        JLabel totalPrice;
        JButton closeButton;
    
        DecimalFormat df = new DecimalFormat("#.##");
    
        public SecondWindow() {
            setTitle("Order Details");
            setLocationRelativeTo(null);
            setSize(400, 300);
            setModal(true);
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 1));
    
            orderDetails = new JTextArea();
            orderDetails.setEditable(false);
            panel.add(orderDetails);
    
            totalButton = new JButton("Total");
            totalButton.addActionListener(new TotalPriceListener());
            panel.add(totalButton);
    
            totalPrice = new JLabel("Total Price: ");
            panel.add(totalPrice);
    
            closeButton = new JButton("Close");
            closeButton.addActionListener(e -> setVisible(false));
            panel.add(closeButton);
    
            this.add(panel);
        }
    
        private class TotalPriceListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                double price = 0;
                for (int i = 0; i < MainWindow.pizzaPrices.length; i++) {
                    int numPizzas = (Integer) MainWindow.getInstance().pizzaSelectors.get(i).getSelectedItem();
                    int numToppings = (Integer) MainWindow.getInstance().toppingSelectors.get(i).getSelectedItem();
                    price += MainWindow.pizzaPrices[i] * numPizzas + MainWindow.toppingPrices[i] * numToppings * numPizzas;
                }
                int numPop = (Integer) MainWindow.getInstance().popNum.getSelectedItem();
                int numJuice = (Integer) MainWindow.getInstance().juiceNum.getSelectedItem();
                price += MainWindow.popPrice * numPop + MainWindow.juicePrice * numJuice;
    
                price *= 1.13; // Add 13% tax
                totalPrice.setText("Total Price: $" + df.format(price));
            }
        }
    }
    
  3. User

    fix up my java project because there is an error on every .getInstance(). here is my code: import java.awt.EventQueue;

    import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.TitledBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import javax.swing.border.BevelBorder; import javax.swing.border.CompoundBorder; import javax.swing.border.LineBorder; import javax.swing.UIManager; import javax.swing.border.SoftBevelBorder; import javax.swing.border.MatteBorder; import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.DefaultComboBoxModel; import javax.swing.BoxLayout; import java.awt.FlowLayout; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.text.DecimalFormat;

    public class PizzaOrder { public static void main(String[] args) { SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true)); } }

    class MainWindow extends JFrame { private static final long serialVersionUID = 1L;

    // Menu Prices
    static double[] pizzaPrices = {6.79, 8.29, 9.49, 10.29, 15.99};
    static double[] toppingPrices = {1.20, 1.40, 1.60, 1.80, 2.30};
    static double popPrice = 1.10;
    static double juicePrice = 1.35;
    
    JComboBox<Integer> smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum;
    JComboBox<Integer> smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum;
    JComboBox<Integer> popNum, juiceNum;
    JButton payButton;
    
    public MainWindow() {
        setTitle("Pizza Order");
        setLocationRelativeTo(null);
        setSize(400, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 4));
    
        smPizzaNum = addPizzaRow(panel, "Small: $6.79");
        mdPizzaNum = addPizzaRow(panel, "Medium: $8.29");
        lgPizzaNum = addPizzaRow(panel, "Large: $9.49");
        xlgPizzaNum = addPizzaRow(panel, "Extra Large: $10.29");
        partyPizzaNum = addPizzaRow(panel, "Party Square: $15.99");
    
        smToppingsNum = addToppingRow(panel, "Small: $1.20");
        mdToppingsNum = addToppingRow(panel, "Medium: $1.40");
        lgToppingsNum = addToppingRow(panel, "Large: $1.60");
        xlgToppingsNum = addToppingRow(panel, "Extra Large: $1.80");
        partyToppingsNum = addToppingRow(panel, "Party: $2.30");
    
        addDrinkRow(panel);
        payButton = new JButton("Pay");
        payButton.addActionListener(e -> new SecondWindow().setVisible(true));
        panel.add(payButton);
    
        this.add(panel);
    }
    
    private JComboBox<Integer> addPizzaRow(JPanel panel, String label) {
        panel.add(new JLabel(label));
        JComboBox<Integer> numPizza = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
        panel.add(numPizza);
        return numPizza;
    }
    
    private JComboBox<Integer> addToppingRow(JPanel panel, String label) {
        panel.add(new JLabel(label));
        JComboBox<Integer> numToppings = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5, 6});
        panel.add(numToppings);
        return numToppings;
    }
    
    private void addDrinkRow(JPanel panel) {
        panel.add(new JLabel("Pop: $1.10"));
        popNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
        panel.add(popNum);
    
        panel.add(new JLabel("Juice: 1.35"));
        juiceNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
        panel.add(juiceNum);
    }
    

    }

    class SecondWindow extends JDialog { private static final long serialVersionUID = 1L;

    JTextArea orderDetails;
    JButton totalButton;
    JLabel totalPrice;
    JButton closeButton;
    
    DecimalFormat df = new DecimalFormat("#.##");
    
    public SecondWindow() {
        setTitle("Order Details");
        setLocationRelativeTo(null);
        setSize(400, 300);
        setModal(true);
    
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1));
    
        orderDetails = new JTextArea();
        orderDetails.setEditable(false);
        panel.add(orderDetails);
    
        totalButton = new JButton("Total");
        totalButton.addActionListener(new TotalPriceListener());
        panel.add(totalButton);
    
        totalPrice = new JLabel("Total Price: ");
        panel.add(totalPrice);
    
        closeButton = new JButton("Close");
        closeButton.addActionListener(e -> setVisible(false));
        panel.add(closeButton);
    
        this.add(panel);
    }
    
    private class TotalPriceListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            double price = 0;
            for (int i = 0; i < MainWindow.pizzaPrices.length; i++) {
                int numPizzas = (Integer) MainWindow.getInstance().pizzaSelectors.get(i).getSelectedItem();
                int numToppings = (Integer) MainWindow.getInstance().toppingSelectors.get(i).getSelectedItem();
                price += MainWindow.pizzaPrices[i] * numPizzas + MainWindow.toppingPrices[i] * numToppings * numPizzas;
            }
            int numPop = (Integer) MainWindow.getInstance().popNum.getSelectedItem();
            int numJuice = (Integer) MainWindow.getInstance().juiceNum.getSelectedItem();
            price += MainWindow.popPrice * numPop + MainWindow.juicePrice * numJuice;
    
            price *= 1.13; // Add 13% tax
            totalPrice.setText("Total Price: + df.format(price)");
        }
    }
    

    }

  4. Assistant

    There are a few issues with the code that you posted. I noticed that there are unused imports, and the getInstance() method does not exist for MainWindow. My suggestion is to use the variables in the MainWindow class directly instead of calling a non-existent method. I have updated the code below.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.text.DecimalFormat;
    
    public class PizzaOrder {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));
        }
    }
    
    class MainWindow extends JFrame {
        private static final long serialVersionUID = 1L;
    
        // Menu Prices
        static double[] pizzaPrices = {6.79, 8.29, 9.49, 10.29, 15.99};
        static double[] toppingPrices = {1.20, 1.40, 1.60, 1.80, 2.30};
        static double popPrice = 1.10;
        static double juicePrice = 1.35;
    
        JComboBox<Integer> smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum;
        JComboBox<Integer> smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum;
        JComboBox<Integer> popNum, juiceNum;
        JButton payButton;
    
        public MainWindow() {
            setTitle("Pizza Order");
            setLocationRelativeTo(null);
            setSize(400, 500);
            setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 4));
    
            smPizzaNum = addPizzaRow(panel, "Small: $6.79");
            mdPizzaNum = addPizzaRow(panel, "Medium: $8.29");
            lgPizzaNum = addPizzaRow(panel, "Large: $9.49");
            xlgPizzaNum = addPizzaRow(panel, "Extra Large: $10.29");
            partyPizzaNum = addPizzaRow(panel, "Party Square: $15.99");
    
            smToppingsNum = addToppingRow(panel, "Small: $1.20");
            mdToppingsNum = addToppingRow(panel, "Medium: $1.40");
            lgToppingsNum = addToppingRow(panel, "Large: $1.60");
            xlgToppingsNum = addToppingRow(panel, "Extra Large: $1.80");
            partyToppingsNum = addToppingRow(panel, "Party: $2.30");
    
            addDrinkRow(panel);
            payButton = new JButton("Pay");
            payButton.addActionListener(e -> new SecondWindow(smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum, smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum, popNum, juiceNum).setVisible(true));
            panel.add(payButton);
    
            this.add(panel);
        }
    
        private JComboBox<Integer> addPizzaRow(JPanel panel, String label) {
            panel.add(new JLabel(label));
            JComboBox<Integer> numPizza = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(numPizza);
            return numPizza;
        }
    
        private JComboBox<Integer> addToppingRow(JPanel panel, String label) {
            panel.add(new JLabel(label));
            JComboBox<Integer> numToppings = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5, 6});
            panel.add(numToppings);
            return numToppings;
        }
    
        private void addDrinkRow(JPanel panel) {
            panel.add(new JLabel("Pop: $1.10"));
            popNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(popNum);
    
            panel.add(new JLabel("Juice: 1.35"));
            juiceNum = new JComboBox<>(new Integer[]{0, 1, 2, 3, 4, 5});
            panel.add(juiceNum);
        }
    }
    
    class SecondWindow extends JDialog {
        private static final long serialVersionUID = 1L;
    
        JTextArea orderDetails;
        JButton totalButton;
        JLabel totalPrice;
        JButton closeButton;
    
        DecimalFormat df = new DecimalFormat("#.##");
    
        public SecondWindow(JComboBox<Integer> smPizzaNum, JComboBox<Integer> mdPizzaNum, JComboBox<Integer> lgPizzaNum, JComboBox<Integer> xlgPizzaNum, JComboBox<Integer> partyPizzaNum, JComboBox<Integer> smToppingsNum, JComboBox<Integer> mdToppingsNum, JComboBox<Integer> lgToppingsNum, JComboBox<Integer> xlgToppingsNum, JComboBox<Integer> partyToppingsNum, JComboBox<Integer> popNum, JComboBox<Integer> juiceNum) {
            setTitle("Order Details");
            setLocationRelativeTo(null);
            setSize(400, 300);
            setModal(true);
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 1));
    
            orderDetails = new JTextArea();
            orderDetails.setEditable(false);
            panel.add(orderDetails);
    
            totalButton = new JButton("Total");
            totalButton.addActionListener(new TotalPriceListener(smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum, smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum, popNum, juiceNum));
            panel.add(totalButton);
    
            totalPrice = new JLabel("Total Price: ");
            panel.add(totalPrice);
    
            closeButton = new JButton("Close");
            closeButton.addActionListener(e -> setVisible(false));
            panel.add(closeButton);
    
            this.add(panel);
        }
    
        private class TotalPriceListener implements ActionListener {
            JComboBox<Integer>[] pizzaSelectors;
            JComboBox<Integer>[] toppingSelectors;
            JComboBox<Integer> popNum, juiceNum;
    
            @SuppressWarnings("unchecked")
            public TotalPriceListener(JComboBox<Integer> smPizzaNum, JComboBox<Integer> mdPizzaNum, JComboBox<Integer> lgPizzaNum, JComboBox<Integer> xlgPizzaNum, JComboBox<Integer> partyPizzaNum, JComboBox<Integer> smToppingsNum, JComboBox<Integer> mdToppingsNum, JComboBox<Integer> lgToppingsNum, JComboBox<Integer> xlgToppingsNum, JComboBox<Integer> partyToppingsNum, JComboBox<Integer> popNum, JComboBox<Integer> juiceNum) {
                this.pizzaSelectors = new JComboBox[]{smPizzaNum, mdPizzaNum, lgPizzaNum, xlgPizzaNum, partyPizzaNum};
                this.toppingSelectors = new JComboBox[]{smToppingsNum, mdToppingsNum, lgToppingsNum, xlgToppingsNum, partyToppingsNum};
                this.popNum = popNum;
                this.juiceNum = juiceNum;
            }
    
            public void actionPerformed(ActionEvent e) {
                double price = 0;
                for (int i = 0; i < MainWindow.pizzaPrices.length; i++) {
                    int numPizzas = (Integer) pizzaSelectors[i].getSelectedItem();
                    int numToppings = (Integer) toppingSelectors[i].getSelectedItem();
                    price += MainWindow.pizzaPrices[i] * numPizzas + MainWindow.toppingPrices[i] * numToppings * numPizzas;
                }
                int numPop = (Integer) popNum.getSelectedItem();
                int numJuice = (Integer) juiceNum.getSelectedItem();
                price += MainWindow.popPrice * numPop + MainWindow.juicePrice * numJuice;
    
                price *= 1.13; // Add 13% tax
                totalPrice.setText("Total Price: $" + df.format(price));
            }
        }
    }
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.