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
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