Question: Write a Java class that mimics the operations of a vending machines. More specifically, the program simulates what happens when the user chooses one of

Write a Java class that mimics the operations of a vending machines. More specifically, the program simulates what happens when the user chooses one of the vending machines, inputs money and picks an item from the vending machine.

Assume there are two vending machines: one for drinks and one for snacks. The machine only accepts quarters, nickels, dimes and pennies. Each vending machine contains one or more items. The name and price of each item is given as a string in the constructor. The format of the input values is comma-separated item name followed by the item price, example: "Coke , 1.25, Water , 1.00, Gatoraid , 1.50".

If enough money is deposited that covers the cost of an item, the item should be despensed and the

Your VendorMachine class should have ONLY ONE constructor. The constructor takes two strings, the first is the type of items vended (Drinks or Snacks), the second string contains the names and prices of the actual items dispensed by the machine.

The name and price of each item is given as a string in the constructor. The format of the input values is comma-separated item name followed by the item price, example: "Coke , 1.25, Water , 1.00, Gatoraid , 1.50".

Your VendorMachine class should have private array variables for item description and price. The only way to add item descriptions and prices is to use the constructor. A vending machine can only sell 3 items.

Your VendorMachine class should have private variables for the number of quarters, nickels, dimes and pennies inserted.

Output must be formatted to display currency in 2 precision.

Your VendorMachine class should have public methods as follows:

public VendingMachine(String name, String listsOfItems) //Constructor

public void listItems() // list item location, item description and item price

public void selectItem(int itemNum) // uses itemNum to purchase item if enough money deposited, gives the user their change and adds cost of item to total sales. If not enough money was deposited for the item they wish to purchase, give a message, then name of the item, the cost of the item and the amount they deposited. If item number is invalid, give a message.

public static double getTotalsSales() // gives the total sales for all vending machines for the day

public void addQuarters(int numOfQuarters) // increases the count of number of quarters deposited

public void addDimes(int numOfDimes) // increases the count of number of dimes deposited

public void addNickels(int numOfNickels) // increases the count of number of nickels deposited

public void addPennies(int numOfPennies) // increases the count of number of pennies deposited

public void cancelItem() { // returns the user's deposit without any purchase. Does not add to total sales.

private double finalizeSaleAndReturnChange(int itemNum) // updates total sales for all instances of vendingMachine based on price of item purchased and returns change

private double depositedAmt() // computes the dollars and cent value of the current coins.

WE HAVE BEEN Given the Main class already defined as:

public static void main(String[] args) { //Create 2 instances of VendingMachine VendingMachine drinksVender = new VendingMachine("Drinks","Coke,1.25,Water,1.00,Gatoraid,1.50"); VendingMachine snacksVender = new VendingMachine("Snacks", "Cheetos,1.25,Snickers,1.00,Pizza,1.50");

//add $1.25 in quarters, list drink items and select a water //should return change of $0.25 drinksVender.addQuarters(5); drinksVender.listItems(); drinksVender.selectItem(1);

//add $1.50, list snack items and select Cheetos //should return change of $0.25 snacksVender.addQuarters(3); snacksVender.addDimes(6); snacksVender.addNickels(3); snacksVender.listItems(); snacksVender.selectItem(0); //sum of all sales for all machines //should display $2.25 //System.out.println("Total Sales for both machines today $"+VendingMachine.getTotalsSales()); //Should give message not enough money to purchase items drinksVender.addQuarters(3); drinksVender.addPennies(2); drinksVender.listItems(); drinksVender.selectItem(0); //cancel order and refund drinksVender.addQuarters(4); drinksVender.listItems(); drinksVender.cancelItem();

//invalid item selected, display message snacksVender.addQuarters(3); snacksVender.addDimes(6); snacksVender.addNickels(3); snacksVender.listItems(); snacksVender.selectItem(-1); //correct item selection, displense pizza snacksVender.selectItem(2); //should not be able to add negative coins snacksVender.addNickels(-5);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!