/** * Program Name: EF_DrinkMachineGUI.java * Author: Essam Fahmy * Date: August 15, 2022 * Purpose: A soft drink vending machine GUI that * uses the SoftDrink object to create drink options * with their prices and quantities then calculate * total price, change, and leftover quantity * when selling them * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.event.*; public class EF_DrinkMachineGUI extends JFrame { private JPanel optionsPanel, paymentPanel, completePanel; private ButtonClick detector = new ButtonClick(); private ArrayList<SoftDrink> list; private JRadioButton cokeBtn, spriteBtn, fantaBtn, mountainBtn, rootBtn, redBtn; private JTextField amountText, changeText, message; private JButton deliverBtn; private JLabel header1, header2; private ButtonGroup btnGroup; private JFrame frame; private int index; public EF_DrinkMachineGUI(ArrayList <SoftDrink> list){ super("Essam's Soft Drink Vending Machine"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new GridLayout(3, 1)); this.setSize(500, 400); this.setLocationRelativeTo(null); this.setResizable(false); optionsBuild(); paymentBuild(); completeBuild(); this.add(optionsPanel); this.add(paymentPanel); this.add(completePanel); this.setVisible(true); } private void optionsBuild() { optionsPanel = new JPanel(); optionsPanel.setLayout(new GridLayout(2, 3)); cokeBtn = new JRadioButton("Coke - $1.25", true); cokeBtn.setActionCommand("coke"); spriteBtn = new JRadioButton("Sprite - $2.20", false); spriteBtn.setActionCommand("sprite"); fantaBtn = new JRadioButton("Fanta Orange - $1.20", false); fantaBtn.setActionCommand("fanta"); mountainBtn = new JRadioButton("Mountain Dew - $2.35", false); mountainBtn.setActionCommand("mountain"); rootBtn = new JRadioButton("Root Beer - $1.35", false); rootBtn.setActionCommand("root"); redBtn = new JRadioButton("Red Pop - $1.85", false); redBtn.setActionCommand("red"); btnGroup = new ButtonGroup(); btnGroup.add(cokeBtn); btnGroup.add(spriteBtn); btnGroup.add(fantaBtn); btnGroup.add(mountainBtn); btnGroup.add(rootBtn); btnGroup.add(redBtn); cokeBtn.addActionListener(detector); spriteBtn.addActionListener(detector); fantaBtn.addActionListener(detector); mountainBtn.addActionListener(detector); rootBtn.addActionListener(detector); redBtn.addActionListener(detector); optionsPanel.add(cokeBtn); optionsPanel.add(spriteBtn); optionsPanel.add(fantaBtn); optionsPanel.add(mountainBtn); optionsPanel.add(rootBtn); optionsPanel.add(redBtn); } private void paymentBuild() { amountText = new JTextField(); changeText = new JTextField(); changeText.setEditable(false); paymentPanel = new JPanel(); paymentPanel.setLayout(new GridLayout(2, 2)); header1 = new JLabel("Enter Payment Amount:"); header2 = new JLabel("Change Due:"); paymentPanel.add(header1); paymentPanel.add(amountText); paymentPanel.add(header2); paymentPanel.add(changeText); } private void completeBuild(){ ButtonClick detector = new ButtonClick(); completePanel = new JPanel(); message = new JTextField(); message.setEditable(false); deliverBtn = new JButton("Deliver Selection"); deliverBtn.setActionCommand("deliver"); deliverBtn.addActionListener(detector); completePanel.setLayout(new GridLayout(2, 1)); completePanel.add(deliverBtn); completePanel.add(message); } private class ButtonClick implements ActionListener { @Override public void actionPerformed(ActionEvent ev) { if (ev.getActionCommand().equals("coke")) { } else if (ev.getActionCommand().equals("coke")) { if(cokeBtn.isSelected()) { index = 0; checkDrinkStatus(); } } else if (ev.getActionCommand().equals("sprite")) { if(spriteBtn.isSelected()) { index = 1; checkDrinkStatus(); } } else if (ev.getActionCommand().equals("fanta")) { if(fantaBtn.isSelected()) { index = 2; checkDrinkStatus(); } } else if (ev.getActionCommand().equals("mountain")) { if(mountainBtn.isSelected()) { index = 3; checkDrinkStatus(); } } else if (ev.getActionCommand().equals("root")) { if(rootBtn.isSelected()) { index = 4; checkDrinkStatus(); } } else if (ev.getActionCommand().equals("red")) { if(redBtn.isSelected()) { index = 5; checkDrinkStatus(); } } } } private void checkDrinkStatus() { if(list.get(index).getQuantity() == 0) { if(index == 0) { cokeBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } else if(index == 1) { spriteBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } else if(index == 2) { fantaBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } else if(index == 3) { mountainBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } else if(index == 4) { rootBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } else if(index == 5) { redBtn.setEnabled(false); message.setText("Sorry! this drink is out of stock, please choose another one."); } } if(list.get(index).getQuantity() == 1) { list.get(index).setQuantity(0); message.setText("Please enter the payment amount"); if(index == 0) { cokeBtn.setEnabled(false); } else if(index == 1) { spriteBtn.setEnabled(false); } else if(index == 2) { fantaBtn.setEnabled(false); } else if(index == 3) { mountainBtn.setEnabled(false); } else if(index == 4) { rootBtn.setEnabled(false); } else if(index == 5) { redBtn.setEnabled(false); } } } public static void main (String [] args) { ArrayList<SoftDrink> list = new ArrayList<SoftDrink>(); list.add( new SoftDrink("Coke", 1.25, 1) ); list.add( new SoftDrink("Sprite", 2.2, 1) ); list.add( new SoftDrink("Fanta Orange", 1.2, 1) ); list.add( new SoftDrink("Mountain Dew", 2.35, 1) ); list.add( new SoftDrink("Root Beer", 1.35, 1) ); list.add( new SoftDrink("Red Pop", 1.85, 1) ); EF_DrinkMachineGUI frame = new EF_DrinkMachineGUI(list); }//End of main method }