Untitled
package order1; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class Order { public static void main(String[] args) { JFrame frame = new JFrame("Ethan Nhielzen T."); JPanel panel = new JPanel(); JLabel header = new JLabel("FoodCourt Sa Gedli", SwingConstants.CENTER); JCheckBox chk1 = new JCheckBox("Hotdog"); JCheckBox chk2 = new JCheckBox("Adobo"); JCheckBox chk3 = new JCheckBox("Egg"); JCheckBox chk4 = new JCheckBox("Tapa"); JCheckBox chk5 = new JCheckBox("Fried Chicken"); JLabel price1 = new JLabel("Php. 20"); JLabel price2 = new JLabel("Php. 50"); JLabel price3 = new JLabel("Php. 10"); JLabel price4 = new JLabel("Php. 60"); JLabel price5 = new JLabel("Php. 40"); // Quantity text fields JTextField qty1 = new JTextField("0"); JTextField qty2 = new JTextField("0"); JTextField qty3 = new JTextField("0"); JTextField qty4 = new JTextField("0"); JTextField qty5 = new JTextField("0"); JButton order = new JButton("Order now"); JLabel totalPrice = new JLabel("Total Price: Php 0"); panel.setBounds(0, 0, 500, 500); panel.setBackground(Color.darkGray); header.setFont(new Font("Calibri", Font.PLAIN, 35)); header.setForeground(Color.white); header.setBackground(Color.darkGray); header.setBounds(0, 20, 500, 50); header.setOpaque(true); chk1.setForeground(Color.white); chk1.setBackground(Color.darkGray); chk1.setBounds(50, 80, 200, 30); price1.setBounds(380, 80, 100, 30); qty1.setBounds(300, 80, 50, 30); chk2.setForeground(Color.white); chk2.setBackground(Color.darkGray); chk2.setBounds(50, 120, 200, 30); price2.setBounds(380, 120, 100, 30); qty2.setBounds(300, 120, 50, 30); chk3.setForeground(Color.white); chk3.setBackground(Color.darkGray); chk3.setBounds(50, 160, 200, 30); price3.setBounds(380, 160, 100, 30); qty3.setBounds(300, 160, 50, 30); chk4.setForeground(Color.white); chk4.setBackground(Color.darkGray); chk4.setBounds(50, 200, 200, 30); price4.setBounds(380, 200, 100, 30); qty4.setBounds(300, 200, 50, 30); chk5.setForeground(Color.white); chk5.setBackground(Color.darkGray); chk5.setBounds(50, 240, 200, 30); price5.setBounds(380, 240, 100, 30); qty5.setBounds(300, 240, 50, 30); totalPrice.setForeground(Color.white); totalPrice.setBounds(200, 350, 200, 50); totalPrice.setFont(new Font("Calibri", Font.BOLD, 20)); order.setBounds(180, 300, 120, 40); order.setBackground(Color.white); order.setFont(new Font("Calibri", Font.BOLD, 18)); order.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int hotdog = 20, adobo = 50, egg = 10, tapa = 60, chicken = 40; int total = 0; // Helper method to get a valid quantity int qtyHotdog = getValidQuantity(qty1.getText()); int qtyAdobo = getValidQuantity(qty2.getText()); int qtyEgg = getValidQuantity(qty3.getText()); int qtyTapa = getValidQuantity(qty4.getText()); int qtyChicken = getValidQuantity(qty5.getText()); if (chk1.isSelected()) total += hotdog * qtyHotdog; if (chk2.isSelected()) total += adobo * qtyAdobo; if (chk3.isSelected()) total += egg * qtyEgg; if (chk4.isSelected()) total += tapa * qtyTapa; if (chk5.isSelected()) total += chicken * qtyChicken; totalPrice.setText("Total Price: Php " + total); } // Method to ensure valid integer quantity private int getValidQuantity(String text) { if (text.matches("\\d+")) { // Only numbers allowed return Integer.parseInt(text); } return 0; // Default to 0 if invalid } }); frame.setSize(500, 500); frame.setVisible(true); frame.setLayout(null); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(header); frame.add(chk1); frame.add(qty1); frame.add(price1); frame.add(chk2); frame.add(qty2); frame.add(price2); frame.add(chk3); frame.add(qty3); frame.add(price3); frame.add(chk4); frame.add(qty4);
Leave a Comment