Question
Adjust GUI for VendingMachineV2 (Given in class folder) which includes and Admin button. Write a ButtonHandler that will display the current balance of the Register
Adjust GUI for VendingMachineV2 (Given in class folder) which includes and Admin button. Write a ButtonHandler that will display the current balance of the Register when clicked (use JOptionPane to display) and the remaining Inventory of each Dispenser. Hour 14 Vending Machine GUI already provided below to help with question (please make sure it works before you reply. Also "Resgister" is spelled wrong but please leave it alone so there are no issues. )
Hour 14 VendingMachineGUI
/* * Emulate vending machine (chapter 14-15) * Interface class */ package vendingmachine;
import javax.swing.*; //pg. 733 import java.awt.*; //pg.741 import java.awt.event.*; import javax.swing.JOptionPane;
public class VendingMachineGUI extends JFrame { //Window Constant private static final int WIDTH = 500; private static final int HEIGHT = 500; //JLabel private JLabel selectionL; //JButton private JButton candyB, gumB, chipsB, cookiesB, exitB; //Button Handler private ButtonHandler bHandler; //Register static Resgister2 myRegister = new Resgister2(); //Dispenser static Dispenser2 candy = new Dispenser2(); static Dispenser2 gum = new Dispenser2("Gum", 0, 2); static Dispenser2 chips = new Dispenser2("Chips", 5, 1); static Dispenser2 cookies = new Dispenser2("Cookies", 15, 3); //GUI CONSTRUCTOR public VendingMachineGUI() { //Layout Window pg. 732 setTitle("Vending Machine"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); //pg. 734 setVisible(true); //Create Container Container pane = getContentPane(); pane.setLayout(new GridLayout (6,1)); //pg. 743 //Create JLabel selectionL; selectionL = new JLabel ("Make a Selection", JLabel.CENTER); //JButton candyB, gumB, chipsB, cookiesB, exitB; bHandler = new ButtonHandler(); candyB = new JButton("Candy"); candyB.addActionListener(bHandler); gumB = new JButton("Gum"); gumB.addActionListener(bHandler); chipsB = new JButton("Chips"); chipsB.addActionListener(bHandler); cookiesB = new JButton("Cookies"); cookiesB.addActionListener(bHandler); exitB = new JButton("Exit"); exitB.addActionListener(bHandler); //Add GUI form objects pane.add(selectionL); pane.add(candyB); pane.add(chipsB); pane.add(cookiesB); pane.add(exitB); }//end constructor public static void main(String[] args) { //GUI Interface VendingMachineGUI myVending = new VendingMachineGUI();
//Variable int selection; //Display Dispensers showInventory(); //Display Cash Register //System.out.println("cash balance is: " + myRegister.getCashBalance()); //Process Selection //makeSelection(selection); //Display Cash Register //System.out.println("cash balance is: " + myRegister.getCashBalance()); }//end main //Private ButtonHandler Class Definition private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Exit")) { System.exit(0); }//end if else if(e.getActionCommand().equals("Candy")) { makeSelection(candy); }//end else if else if(e.getActionCommand().equals("Gum")) { makeSelection(gum); }//end else if else if(e.getActionCommand().equals("Chips")) { makeSelection(chips); }//end else if else { makeSelection(cookies); }//end else if }//end actionPerformed }//end ButtonHandler //Display item Inventory() public static void showInventory() { System.out.println(); System.out.println("Your dispenser is:"); System.out.println(candy.getItemType()); System.out.println(candy.getItemCount()); System.out.println(candy.getItemCost()); System.out.println(); System.out.println("Your dispenser is:"); System.out.println(gum.getItemType()); System.out.println(gum.getItemCount()); System.out.println(gum.getItemCost()); System.out.println(); System.out.println("Your dispenser is:"); System.out.println(chips.getItemType()); System.out.println(chips.getItemCount()); System.out.println(chips.getItemCost()); System.out.println(); System.out.println("Your dispenser is:"); System.out.println(cookies.getItemType()); System.out.println(cookies.getItemCount()); System.out.println(cookies.getItemCost()); }//end showInventory() //Identify and select user's item choice public static void makeSelection(Dispenser2 itemDispenser) { if(itemDispenser.sellItem()) { myRegister.recieveCash(itemDispenser.getItemCost()); }//end if JOptionPane.showMessageDialog(null, "Take your " + itemDispenser.getItemType()); }//end makeSelection() }//end class
Dispenser2
/**************************** * Manage the items in the vending machine * * Dispense Class Definition */ package vendingmachine;
public class Dispenser2 { //Instance Variables private String itemType; private int itemCount; private int itemCost; //Constructor
public Dispenser2() { itemType = "Candy"; itemCount = 10; itemCost = 1; }//end constructor //Overloaded Constructor public Dispenser2(String itemType, int itemCount, int itemCost) { this.itemType = itemType; this.itemCount = itemCount; this.itemCost = itemCost; }//end overloaded constructor //Getter Methods public String getItemType() { return itemType; }//end getItemType() public int getItemCount() { return itemCount; }//end getItemCount()
public int getItemCost() { return itemCost; }//end get ItemCount()
//Decrease Inventory public boolean sellItem() { if(getItemCount()>0) { itemCount--; return true; }//end if else { return false; }//end else }//end sellItem }//end class
Resgister2
/*************************** * Manage cash collections * * Register class definistion */ package vendingmachine;
public class Resgister2 { //Instance Variable private int cashBalance; //Constructor public Resgister2() { cashBalance = 100; }//end constructor
//Overloaded Constructor public Resgister2(int cashBalance) { this.cashBalance = cashBalance; }//end constructor //Getter Method public int getCashBalance() { return cashBalance; }//end getCashBalance //Recieved cash public void recieveCash(int amount) { cashBalance += amount; }//end recieveCash }//end class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started