Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

add an array list to my java code so that when the button is clicked, it will add the…

  1. User

    add an array list to my java code so that when the button is clicked, it will add the value of (ChoixPizzaNumPizza)+(NumTopping, the topping price depends on what size the pizza is)+(ChoixBreuvageNumBreuvage) to an list which can then be shown on the textarea list: import java.awt.EventQueue;

    import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import javax.swing.SwingConstants; 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.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;

    public class PizzaOrder extends JFrame {

    private JPanel contentPane;
    private JPanel ToppingSelect;
    private JTextField NumPizza;
    private JTextField NumTopping;
    private JTextField NumBreuvage;
    private JButton Ajouter;
    
    
    public static void main(String[] args) {
    	EventQueue.invokeLater(new Runnable() {
    		public void run() {
    			try {
    				PizzaOrder frame = new PizzaOrder();
    				frame.setVisible(true);
    			} catch (Exception e) {
    				e.printStackTrace();
    
    			}
    		}
    	});
    
    }
    
    
    
    public PizzaOrder() {
    
    	setTitle("Order");
    	setResizable(false);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setBounds(100, 100, 630, 689);
    	contentPane = new JPanel();
    	contentPane.setBackground(new Color(255, 147, 0));
    	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    
    	setContentPane(contentPane);
    	contentPane.setLayout(null);
    
    	JPanel Menu = new JPanel();
    	Menu.setBackground(new Color(255, 147, 0));
    	Menu.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Menu", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
    	Menu.setBounds(6, 6, 618, 158);
    	contentPane.add(Menu);
    	Menu.setLayout(new GridLayout(0, 3, 0, 0));
    
    	JPanel PizzaPrice = new JPanel();
    	PizzaPrice.setBackground(new Color(255, 147, 0));
    	PizzaPrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Pizza", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    	Menu.add(PizzaPrice);
    	PizzaPrice.setLayout(null);
    
    	JLabel PetitPizza = new JLabel("Petit: 6.79$");
    	PetitPizza.setBounds(17, 21, 72, 16);
    	PizzaPrice.add(PetitPizza);
    
    	JLabel MoyenPizza = new JLabel("Moyen: 8.29$");
    	MoyenPizza.setBounds(17, 40, 85, 16);
    	PizzaPrice.add(MoyenPizza);
    
    	JLabel LargePizza = new JLabel("Large: 9.49$");
    	LargePizza.setBounds(17, 59, 85, 16);
    	PizzaPrice.add(LargePizza);
    
    	JLabel ExtraLargePizza = new JLabel("Extra Large: 10.29$");
    	ExtraLargePizza.setBounds(17, 78, 127, 16);
    	PizzaPrice.add(ExtraLargePizza);
    
    	JLabel FetePizza = new JLabel("Fete: 15.99$");
    	FetePizza.setBounds(17, 97, 93, 16);
    	PizzaPrice.add(FetePizza);
    
    	JPanel ToppingPrice = new JPanel();
    	ToppingPrice.setBackground(new Color(255, 147, 0));
    	ToppingPrice.setLayout(null);
    	ToppingPrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Toppings", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
    	Menu.add(ToppingPrice);
    
    	JLabel Petittopping = new JLabel("Petit: 1.20$");
    	Petittopping.setBounds(17, 21, 72, 16);
    	ToppingPrice.add(Petittopping);
    
    	JLabel Moyentopping = new JLabel("Moyen: 1.40$");
    	Moyentopping.setBounds(17, 40, 85, 16);
    	ToppingPrice.add(Moyentopping);
    
    	JLabel Largetopping = new JLabel("Large: 1.60$");
    	Largetopping.setBounds(17, 59, 85, 16);
    	ToppingPrice.add(Largetopping);
    
    	JLabel ExtraLargetopping = new JLabel("Extra Large: 1.80$");
    	ExtraLargetopping.setBounds(17, 78, 127, 16);
    	ToppingPrice.add(ExtraLargetopping);
    
    	JLabel Fetetopping = new JLabel("Fete: 2.30$");
    	Fetetopping.setBounds(17, 97, 93, 16);
    	ToppingPrice.add(Fetetopping);
    
    	JPanel BreuvagePrice = new JPanel();
    	BreuvagePrice.setBackground(new Color(255, 147, 0));
    	BreuvagePrice.setLayout(null);
    	BreuvagePrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Breuvages", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
    	Menu.add(BreuvagePrice);
    
    	JLabel Pop = new JLabel("Pop: 1.10$");
    	Pop.setBounds(17, 21, 72, 16);
    	BreuvagePrice.add(Pop);
    
    	JLabel Jus = new JLabel("Jus: 1.35$");
    	Jus.setBounds(17, 40, 85, 16);
    	BreuvagePrice.add(Jus);
    
    	JLabel Eau = new JLabel("Eau: 1.00$");
    	Eau.setBounds(17, 59, 85, 16);
    	BreuvagePrice.add(Eau);
    
    	JPanel PizzaSelect = new JPanel();
    	PizzaSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    	PizzaSelect.setBackground(new Color(255, 147, 0));
    	PizzaSelect.setBounds(16, 187, 350, 300);
    	contentPane.add(PizzaSelect);
    	PizzaSelect.setLayout(null);
    
    	JLabel NombrePizzaTitle = new JLabel("Choix Pizza");
    	NombrePizzaTitle.setForeground(new Color(0, 252, 255));
    	NombrePizzaTitle.setFont(new Font("Hiragino Sans GB", Font.BOLD, 15));
    	NombrePizzaTitle.setBounds(139, 6, 90, 27);
    	PizzaSelect.add(NombrePizzaTitle);
    
    
    	JComboBox<String> ChoixPizza = new JComboBox<String>();
    	ChoixPizza.setModel(new DefaultComboBoxModel(PizzaSize.values()));
    	ChoixPizza.setBounds(122, 90, 126, 27);
    	ChoixPizza.setMaximumRowCount(5);
    	PizzaSelect.add(ChoixPizza);
    
    
    	NumPizza = new JTextField();
    	NumPizza.setBounds(122, 129, 130, 26);
    	PizzaSelect.add(NumPizza);
    	NumPizza.setColumns(10);
    
    	JLabel PizzaIcon = new JLabel("");
    	PizzaIcon.setBounds(6, 6, 350, 279);
    	PizzaSelect.add(PizzaIcon);
    	PizzaIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/PizzaImage.png")));
    
    	JPanel ToppingSelect;
    	ToppingSelect = new JPanel();
    	ToppingSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    	ToppingSelect.setBackground(new Color(255, 147, 0));
    	ToppingSelect.setBounds(400, 187, 208, 129);
    	contentPane.add(ToppingSelect);
    	ToppingSelect.setLayout(null);;
    
    	JLabel NombreToppingTitle = new JLabel("Nombre de topping");
    	NombreToppingTitle.setForeground(new Color(0, 252, 255));
    	NombreToppingTitle.setFont(new Font("Hiragino Sans GB", Font.BOLD, 15));
    	NombreToppingTitle.setBounds(29, 23, 150, 27);
    	ToppingSelect.add(NombreToppingTitle);
    
    	NumTopping = new JTextField();
    	NumTopping.setBounds(39, 50, 130, 26);
    	NumTopping.setColumns(10);
    	ToppingSelect.add(NumTopping);
    
    	JLabel ToppingIcon = new JLabel("");
    	ToppingIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/ToppingImage.png")));
    	ToppingIcon.setBounds(6, 8, 208, 109);
    	ToppingSelect.add(ToppingIcon);
    
    	JPanel BreuvageSelect = new JPanel();
    	BreuvageSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    	BreuvageSelect.setBackground(new Color(255, 147, 0));
    	BreuvageSelect.setBounds(400, 358, 208, 129);
    	contentPane.add(BreuvageSelect);
    	BreuvageSelect.setLayout(null);;
    
    	JLabel NombreToppingTitle_1 = new JLabel("Choix de breuvages");
    	NombreToppingTitle_1.setBounds(34, 6, 150, 27);
    	BreuvageSelect.add(NombreToppingTitle_1);
    	NombreToppingTitle_1.setForeground(new Color(0, 252, 255));
    	NombreToppingTitle_1.setFont(new Font("Hiragino Sans GB", Font.BOLD, 15));
    
    	NumBreuvage = new JTextField();
    	NumBreuvage.setBounds(47, 62, 130, 26);
    	NumBreuvage.setColumns(10);
    	BreuvageSelect.add(NumBreuvage);
    
    	JComboBox<String> ChoixBreuvage = new JComboBox<String>();
    	ChoixBreuvage.setModel(new DefaultComboBoxModel(Breuvages.values()));
    	ChoixBreuvage.setBounds(65, 34, 79, 27);
    	ChoixBreuvage.setMaximumRowCount(3);
    	BreuvageSelect.add(ChoixBreuvage);
    
    	JLabel BreuvageIcon = new JLabel("");
    	BreuvageIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/BreuvageImage.png")));
    	BreuvageIcon.setBounds(0, 0, 209, 129);
    	BreuvageSelect.add(BreuvageIcon);
    
    
    	JButton Quitter = new JButton("Quitter");
    	Quitter.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			System.exit(0);
    		}
    	});
    	Quitter.setBounds(33, 552, 160, 50);
    	contentPane.add(Quitter);
    
    
    	Ajouter = new JButton("Ajouter");
    	JButton jButton = new JButton();
    	jButton.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    
    		}});
    	Ajouter.setBounds(234, 552, 160, 50);
    	contentPane.add(Ajouter);
    
    
    
    
    	JButton Payer = new JButton("Payer");
    	Payer.setBounds(431, 552, 160, 50);
    	contentPane.add(Payer);
    
    	Payer.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			Receipt receipt = new Receipt(); 
    			receipt.setVisible(true);  
    			dispose();  
    		}
    	});
    	setVisible(false);
    }
    

    }import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

    import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.border.TitledBorder; import javax.swing.ImageIcon; import javax.swing.JList; import javax.swing.border.LineBorder; import java.awt.Color; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JButton;

    public class Receipt extends JFrame {

    private JPanel contentPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Receipt frame = new Receipt();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Receipt() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 474, 521);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    
        setContentPane(contentPane);
        contentPane.setLayout(null);
    
        JPanel Panel1 = new JPanel();
        Panel1.setBounds(0, 0, 474, 493);
        contentPane.add(Panel1);
        Panel1.setLayout(null);
    
        JLabel TitleCommande = new JLabel("Commande");
        TitleCommande.setFont(new Font("Noteworthy", Font.PLAIN, 25));
        TitleCommande.setBounds(175, 6, 108, 31);
        Panel1.add(TitleCommande);
    
        JList list = new JList();
        list.setBounds(30, 49, 408, 241);
        Panel1.add(list);
    
        JLabel Subtotale = new JLabel("SubTotales: ");
        Subtotale.setBounds(57, 338, 143, 24);
        Panel1.add(Subtotale);
    
        JLabel Totale = new JLabel("Totales: ");
        Totale.setBounds(57, 374, 143, 24);
        Panel1.add(Totale);
    
        JButton ShowTax = new JButton("+Tax(13%)");
        ShowTax.setBounds(199, 337, 117, 29);
        Panel1.add(ShowTax);
    
        JButton ModifyCommande = new JButton("Modifier la Commande");
        ModifyCommande.setBounds(30, 435, 170, 31);
        Panel1.add(ModifyCommande);
        // Add action listener to the "Modifier la Commande" button
        ModifyCommande.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Close the Receipt window
                dispose();
    
                // Show the PizzaOrder window
                PizzaOrder pizzaOrder = new PizzaOrder();
                pizzaOrder.setVisible(true);
            }
        });
        
        
    
        JButton Fermer = new JButton("Fermer");
        Fermer.setBounds(268, 436, 170, 31);
        Panel1.add(Fermer);
    
        // Add action listener to the "Fermer" button
        Fermer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Close the current window
                dispose();
    
                // Terminate the entire application
                System.exit(0);
            }
        });
    
       
    }
    

    } public enum Breuvages { Pop(1.10), Jus(1.35), Eau(1.00);

    private final double value;
    
    private Breuvages(double value) {
        this.value = value;
    }
    
    public double getValue() {
        return value;
    }
    

    } public enum PizzaSize { Small(6.79), Medium(8.29), Large(9.49), ExtraLarge(10.29), Fete(15.99);

    private final double price;
    
    private PizzaSize(double price) {
        this.price = price;
    }
    
    public double getPrice() {
        return price;
    }
    
    static PizzaSize getSelectedItem() {
    	// TODO Auto-generated method stub
    	return null;
    }
    

    }

  2. Assistant

    To add an array list to the Java code, you can do the following:

    1. Add a List variable to the class:
    private List<Double> orderList = new ArrayList<Double>();
    
    1. In the ActionListener for the "Ajouter" button, add the calculated order value to the list:
    Ajouter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double orderValue = (PizzaSize.getSelectedItem().getPrice() * Integer.parseInt(NumPizza.getText())) + 
                (Double.parseDouble(NumTopping.getText()) * ToppingPriceMap.get(PizzaSize.getSelectedItem())) + 
                (Breuvages.valueOf(ChoixBreuvage.getSelectedItem().toString()).getValue() * Integer.parseInt(NumBreuvage.getText()));
            orderList.add(orderValue);
            // Clear the text fields
            NumPizza.setText("");
            NumTopping.setText("");
            NumBreuvage.setText("");
       }
    });
    
    1. In the ActionListener for the "Payer" button, show the order list in the Receipt window:
    Payer.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Receipt receipt = new Receipt(); 
            receipt.setVisible(true); 
            receipt.updateList(orderList);
            dispose();  
        }
    });
    
    1. Modify the Receipt class to accept and display the order list:
    public class Receipt extends JFrame {
        private JPanel contentPane;
        private JList<Double> orderList;
    
        // Constructor
        public Receipt(List<Double> orderList) {
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 474, 521);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            // Initialize the JList
            this.orderList = new JList<Double>(orderList.toArray(new Double[0]));
            this.orderList.setBounds(30, 49, 408, 241);
            contentPane.add(this.orderList);
            // Other code here...
         }
    
        // Method to update the order list
        public void updateList(List<Double> newOrderList) {
            orderList.setListData(newOrderList.toArray(new Double[0]));
        }
    
        // Other code here...
    }
    

    Make sure to update the creation and display of the Receipt window in the PizzaOrder class to pass the order list to the constructor:

    Payer.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Receipt receipt = new Receipt(orderList); 
            receipt.setVisible(true); 
            dispose();  
        }
    });
    

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