Create a new multi-class Java program which implements a vending machine simulat
ID: 3916692 • Letter: C
Question
Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality:
A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup, money should be loaded into the vending machine. Money should consist of different monetary objects for the specified currency for example in USD $1 bills, $5 bills, quarters, dimes, etc. Your program should be designed to use different national currencies easily (for example the Euro) without changing source code. Money should be maintained as paper bills and coins, not just amounts.
B) A menu of commands must be provided. The menu should consists of the following commands using the command numbers indicated:
1. Display the list of commands
2. Display the vending machine inventory. For each item, this command should result in displaying a description and current quantity.
3. Display the money currently held in the vending machine.
4. Purchase an item. The result of this selection should be the following actions:
a) Prompt the user to indicate what item to purchase
b) Prompt the user to specify what monetary items are being used for payment (the actual items for example quarters, dimes, etc.), not a money amount
c) If the user specified enough money to purchase the selected item, the item is purchased (deducted from inventory), supplied money is added to the vending machine, and any change is returned in the form of monetary items (quarters, dimes, etc.).
d) If the user did not specify enough money for the selected item, the transaction is aborted with the supplied money not added to the machine (not accepted) and the product not purchased (i.e. the state of the vending machine is unchanged).
5. Exit – exits the program displaying a departing message.
Additional points to consider:
A) You can use the Java Standard Edition (SE) API library as supplied by Oracle (AKA Sun) except the collection classes (i.e. not ArrayList, Map, Vector, etc.).
B) When developing complex classes, consider creating a main() method to test them out. Once tested successfully, delete the main() method.
C) You should generate error messages when appropriate, for example on invalid input values or not enough money supplied for the selected item to purchase.
Explanation / Answer
Implemented java source code:-
======================
package Tasks;
import java.util.Arrays;
import java.util.Scanner;
class InventoryItem
{
String item;
String type;
double cost;
int rate;
public InventoryItem(String item,String type,double cost,int rate)
{
this.item=item;
this.type=type;
this.cost=cost;
this.rate=rate;
}
}
class CurrencyUnit
{
String item;
String type;
double cost;
int rate;
public CurrencyUnit(String item,String type,double cost,int rate)
{
this.item=item;
this.type=type;
this.cost=cost;
this.rate=rate;
}
}
public class Vending_Machine
{
static InventoryItem [] inventory = {new InventoryItem("Soda", "Bottle", 1.0, 10),
new InventoryItem("Orange Juice", "Bottle", 1.0, 10),
new InventoryItem("Water", "Bottle", 1.0, 10),
new InventoryItem("Popcorn", "Bag", 0.5, 10) };
static CurrencyUnit [] currency = {
new CurrencyUnit("10 Dollar Bill", "Paper", 10.0, 10),
new CurrencyUnit("5 Dollar Bill", "Paper", 5.0, 15),};
public static void menu()
{
System.out.println("Available Commands");
System.out.println();
System.out.println("0. Show commands");
System.out.println("1. Display inventory");
System.out.println("2. Display currency");
System.out.println("3. Purchase item");
System.out.println("4. Exit");
System.out.println();
}
public static void loadProducts()
{
for(int i=0;i<inventory.length;i++)
{
System.out.println(i +inventory[i].toString());
}
System.out.println();
}
public static void loadCurrency()
{
for(int i=0;i<currency.length;i++)
{
System.out.println("CurrencyInventory: " + currency[i].toString());
}
System.out.println();
}
public static void purchaseItem()
{
System.out.print("Item #: ");
int item = input.nextInt();
System.out.print("How many different currency items?: ");
int x = input.nextInt();
System.out.println("Available currency denominations");
for(int i=0;i<currency.length;i++)
{
System.out.println("Currency #: " + i + ": " + currency[i].toString());
}
int[] currencyTypeArray = new int[x];
for(int j=0;j<currencyTypeArray.length;j++)
{
System.out.print("Currency type: ");
currencyTypeArray[j] = input.nextInt();
}
System.out.println("You entered: " + Arrays.toString(currencyTypeArray));
double totalCurrencyInput = 0;
double change = 0;
if(item==0||item==1||item==2)
{
if(totalCurrencyInput >= 1.0)
{
change=(totalCurrencyInput - 1.0);
}
else
{
System.out.println("Insufficient funds");
}
}
else if(item==3||item==4)
{
if(totalCurrencyInput>=0.75)
{
change=(totalCurrencyInput - 0.75);
}
else
{
System.out.println("Insufficient funds");
}
}
else if (item == 5)
{
if (totalCurrencyInput >= 0.5)
{
change = (totalCurrencyInput - 0.5);
}
else
{
System.out.println("Insufficient funds");
}
}
int dollars = (int) (change / 1.0);
change = change % 1.0;
int quarters = (int) (change / 0.25);
change = change % 0.25;
int dimes = (int) (change / 0.1);
change = change % 0.1;
int nickels = (int) (change / 0.5);
change = change % 0.5;
System.out.println("Change amount:");
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Item purchased");
System.out.println();
}
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
int userChoice=0;
while(true)
{
System.out.println("Available Commands");
System.out.println();
System.out.println("0. Display the list of commands");
System.out.println("1. Display the vending machine inventory. ");
System.out.println("2. Display currency");
System.out.println("3.Purchase an item.");
System.out.println("4. Exit");
System.out.println(" Please select any option");
userChoice = input.nextInt();
switch (userChoice)
{
case 0:
menu();
break;
case 1:
loadProducts();
break;
case 2:
loadCurrency();
break;
case 3:
purchaseItem();
break;
case 4:
System.out.println("End!");
System.exit(0);
default:
System.out.println("!invalid Option");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.