Vending Machine - GUI In this assignment, you are to vend six different snacks.
ID: 3792738 • 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.
Use the following screenshots to create your interface and functions:
input What would you like to do? 1 Chips 1.95 (20 avalible) 2- M&Ms; 1.25 (20 avalible) Popcorn 3.95 (16 avalible) 4-Snickers 1.15 (16 avalible) 5-Gum 0.75 (14 avalible) 6-Crackers 1.75 (14 avalible) Restock this machine. Q-Quit. Money in machine -$10.00 Enter a menu number or letter: Cancel OK What would you like to do? 1 Chips 1.95 (20 avalible) 2-M&Ms; 1.25 (20 avalible) 3-popcorn 395 (16 avalible) 4-Snickers 1.15 (16 avalible) 5-Gum 0.75 (14 avalible) 6-Crackers 1.75 (14 avalible) R-Restock this machine. Q-Quit. Money in machine $10.00 Enter a menu number or letter: OK CancelExplanation / Answer
package vendingMachine;
public class Snack {
// Instance variables
private String name;
private int count;
private int limit;
private double cost;
/**
* Constructor
* @param name
* @param cost
*/
public Snack(String name, double cost) {
this(name, 0, 0, cost);
}
/**
* Constructor
* @param name
* @param count
* @param limit
* @param cost
*/
public Snack(String name, int count, int limit, double cost) {
this.name = name;
this.count = count;
this.limit = limit;
this.cost = cost;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @return the limit
*/
public int getLimit() {
return limit;
}
/**
* @return the cost
*/
public double getCost() {
return cost;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param count the count to set
*/
public void setCount(int count) {
this.count = count;
}
/**
* @param limit the limit to set
*/
public void setLimit(int limit) {
this.limit = limit;
}
/**
* @param cost the cost to set
*/
public void setCost(double cost) {
this.cost = cost;
}
@Override
public String toString() {
return getCount() + " packs of " + getName();
}
}
package vendingMachine;
import java.util.Scanner;
public class VendingMachine {
private static final int CAPACITY = 100;
// Instance variables
private Snack[] snacks;
/**
* Constructor
*/
public VendingMachine() {
init();
}
/**
* Initializes the vending machine with some snacks
*/
void init() {
this.snacks = new Snack[6];
// Add 20 packs of Chips
this.snacks[0] = new Snack("Chips", 20, 20, 1.0);
// 20 packs of M&Ms
this.snacks[1] = new Snack("M&Ms", 20, 20, 2.0);
// 16 packs of Popcorn
this.snacks[2] = new Snack("Popcorn", 16, 16, 4.5);
// 16 packs of Snickers
this.snacks[3] = new Snack("Snickers", 16, 16, 1.24);
// 14 packs of Gum
this.snacks[4] = new Snack("Gum", 14, 14, 2.0);
// 14 packs of Crackers
this.snacks[5] = new Snack("Crackers", 14, 14, 5.0);
}
/**
* Get the total number of snacks in the machine
*
* @return
*/
int getTotalPacks() {
int count = 0;
for (Snack snack : snacks) {
count += snack.getCount();
}
return count;
}
/**
* Gets a pack of snack from the vending machine
*
* @param i
* @return
*/
Snack getSnack(int i) {
Snack snack = this.snacks[i];
// If packets of snack is available
if (snack.getCount() > 0) {
snack.setCount(snack.getCount() - 1);
return snack;
}
return null;
}
/**
* Removes a pack of snack from the macine
* @param in
*/
void removeSnack(Scanner in) {
// Display the snack list
displaySnack();
System.out.print(" Enter the snack to get: ");
int i = in.nextInt();
// Get snack
Snack snack = getSnack(i - 1);
if (snack == null)
System.out.print("Sorry this snack is not available");
}
/**
* Add a packets of snack to the machine if the capacity is not full then
* add (limit - count) or count packs of the snack, whichever if applicable
*
* @param i
* @return - The number of packs added or returns -1 is no packs are added
*/
int addSnack(int i, int count) {
if (getTotalPacks() < CAPACITY) {
Snack snack = this.snacks[i];
if (snack.getLimit() > snack.getCount()) {
if (snack.getLimit() >= (snack.getCount() + count)) {
count = snack.getCount() + count;
snack.setCount(snack.getCount() + count);
} else {
count = snack.getLimit() - snack.getCount();
snack.setCount(snack.getCount() + count);
}
return count;
}
}
return 0;
}
/**
* Adds packs of snack to the macine
* @param in
*/
void addSnack(Scanner in) {
// Display the snack list
displaySnack();
int i = 0;
do {
System.out.print(" Enter the choice of snack to add: ");
i = in.nextInt();
if ((i < 1) || (i > this.snacks.length)) {
System.out.print(" Invalid choice. Please try again");
}
} while ((i < 1) || (i > this.snacks.length));
// Ask for the number of packs to add
int noOfPacks = 0;
do {
System.out.print(" Enter the number of packs to add: ");
noOfPacks = in.nextInt();
if (noOfPacks < 0) {
System.out.print(" Invalid choice. Please try again");
}
} while (noOfPacks < 0);
// Add packs
System.out.print(" Packs added: " + addSnack(i - 1, noOfPacks));
}
/**
* Displays the list of Snack
*/
void displaySnack() {
int i = 1;
for (Snack snack : snacks) {
System.out.print(" " + i + " " + snack.getName());
i += 1;
}
}
/**
* Displays the available number of packs of each Snack
*/
void displayMachineState() {
for (Snack snack : snacks) {
System.out.print(" " + snack);
}
System.out.println();
}
/**
* Displays the menu
*/
static void menu() {
System.out.print(" 1. Add snacks");
System.out.print(" 2. Remove snacks");
System.out.print(" 3. Display machine status");
System.out.print(" 4. Exit");
System.out.print(" Enter your choice: ");
}
public static void main(String[] args) {
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Create Vending machine object
VendingMachine machine = new VendingMachine();
while (true) {
// Display menu
VendingMachine.menu();
int choice = in.nextInt();
switch (choice) {
case 1: // Add snack
machine.addSnack(in);
break;
case 2: // Remove snack
machine.removeSnack(in);
break;
case 3: // Display machine state
machine.displayMachineState();
break;
case 4: // Exit
System.exit(0);
// Close scanner
in.close();
default: // Invalid choice
System.out.print(" Invalid choice. Please try again");
}
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.