Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Vending Machine - GUI In this assignment, you are to vend six different snacks.

ID: 3793262 • Letter: V

Question

Vending Machine - GUI

In this assignment, you are to vend six different snacks. The Vending Machine has a capacity of storing up to 100 packs of snacks. Therefore, emulating the real vending machine, it is appropriate to allocate a fixed number of packages to each type of snacks. For example, you can stock 20 packs of Chips, 20 packs of M&Ms, 16 packs of Popcorn, 16 packs of Snickers, 14 packs of Gum, 14 packs of Crackers for a total of 100 packs of snacks. The following is one possible solution, and it is *not* the only solution. Your application can be different from this write up.

Your classes should implement various snacks including “M&Ms”, “Popcorn”, “Snickers”, “Gum”, “Crackers”, and “Chips”. Implement the toString method to display the state of the snack.

Let’s presume that each snack has these properties: “name”, “count”, “limit” and “cost”. Name is of course the name of the snack, count is an integer that represents how many pieces of the snack is in vending machine, limit represents the maximum number of the snack vending machine can hold, and cost is a floating-point number that represents how much the snack costs to buy.

Has to be in GUI and one panel

Explanation / Answer

public class VendingMachine { private int Snackcount; private double moneyCredit; private double Snackcost; private final double Snickers = .95; private final double Gum = .75; private final double Mnms = .85; private final double Popcorn = 1.25; private final double Crackers = 1.00; private final double Chips = 1.15; private double returnAmount; public VendingMachine() { Snackcount = 100; moneyCredit = 0.0; returnAmount = 0.0; } public void restock() { Snackcount = 100; moneyCredit = 0.0; } public void addMoney ( double amount ) { moneyCredit += amount; System.out.println ( "Total inserted: " + moneyCredit ); } public void vend() { double returnAmount = 0.0; if ( moneyCredit >= Snackcost() ) { System.out.println ( "Here is your snack" ); returnAmount -= ( moneyCredit - Snackcost ); if ( returnAmount >= 0.0 ) System.out.println ( "Your change is: " + ( moneyCredit - Snackcost ) + " cents." ); moneyCredit = 0.0; } else System.out.println( "Vending machine only has " + moneyCredit + " cents. Please insert more." ); } } class VendingMachineTester { public static void main( String[] args ) { boolean doAgain = true; char choice; VendingMachine myMachine = new VendingMachine(); Snack mySnack = new Snack(); while ( doAgain ) { JOptionPane.showMessageDialog(null, "Enter your choice of snack!: " + "1) Exit /n2) Incert Money /n3) Snickers" + "4) Gum /n5) M&ms /n6) PopCorn" + "7) Crackers /n8)Chips /n9) Restock" + /*System.out.println(); System.out.println( "Options:" ); System.out.println(); System.out.println( "1] Exit" ); System.out.println( "2] Insert money" ); System.out.println( "3] Vend Snickers" ); System.out.println( "4] Vend Gum" ); System.out.println( "5] Vend M&ms" ); System.out.println( "6] Vend Popcorn" ); System.out.println( "7] Vend Crackers" ); System.out.println( "8] Vend Chips" ); System.out.println( "9] Restock vending machine" ); System.out.println(); System.out.print( "Choice: " ); */ choice = Keyboard.readChar() if(choice == '1') doAgain = false; if( choice == '2' ) { System.out.print( "Enter amount of cents to insert: " ); double cents = Keyboard.readDouble(); myMachine.addMoney ( cents ); } if ( choice == '3' ) { System.out.println( "Vend a Snickers and get change." ); myMachine.vend(); } if ( choice == '4' ) { System.out.println ( "Vend a Gum and get change." ); myMachine.vend(); } if ( choice == '3' ) { System.out.println( "Vend M&ms and get change." ); myMachine.vend(); } if ( choice == '4' ) { System.out.println ( "Vend PopCorn and get change." ); myMachine.vend(); } if ( choice == '3' ) { System.out.println( "Vend Crackers and get change." ); myMachine.vend(); } if ( choice == '8' ) { System.out.println ( "Vend Chips and get change." ); myMachine.vend(); } if ( choice == '9' ) { System.out.println ( "Restock vending machine." ); myMachine.restock(); } } System.out.println(); System.out.println(); System.out.println( "Thanks for using my vending machine." ); } } abstract class Snack { int calories; double cost; public Snack() { calories = 0; cost = 0.0; } public double Snackcost() { return cost; } public int calories() { return calories; } } class Salty extends Snack { public Salty(int cal, double amt) { super(); calories = cal; cost = amt; } } class Sugary extends Snack { public Sugary(int cal, double amt) { super(); calories = cal; cost = amt; } } class Snickers extends Sugary { public Snickers() { setCalories(15); setCost(.95); } } class Gum extends Sugary { public Gum() { setCalories(5); setCost(.75); } } class Mnms extends Sugary { public Mnms() { setCalories(20); setCost(.85); } } class Popcorn extends Salty { public Popcorn() { setCalories(50); setCost(1.25); } } class Crackers extends Salty { public Crackers() { setCalories(25); setCost(1.00); } } class Chips extends Salty { public Chips() { setCalories(30); setCost(1.15); } }