JAVA Program Specification: 1. Create a new multi-class Java program which imple
ID: 3682515 • Letter: J
Question
JAVA Program Specification:
1. Create a new multi-class Java program which implements a vending machine simulator whichcontains the following functionality:
A) At program startup, the vending machine is loaded with a variety of products in a variety ofpackaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Alsoincluded is the cost of each item. The program should be designed to easily load a different setof products easily (for example, from a file).Also at program startup, money should be loaded into the vending machine. Money shouldconsist of different monetary objects for the specified currency for example $1 bills, $5 bills,quarters, dimes, etc. Your program should be designed to use different national currencieseasily (for example the Euro). Money should be maintained as paper bills and coins, not justamounts.
B) A menu of commands must be provided. At a minimum the menu should consists of thefollowing commands:1. Display the list of commands
2. Display the vending machine inventory. For each item, this command should result indisplaying 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:
1. Prompt the user to indicate what item to purchase
2. Prompt the user to specify what monetary items are being used for payment (the actualitems for example quarters, dimes, etc.), not a money amount
3. 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 anychange is returned in the form of monetary items (quarters, dimes, etc.)
.4. If the user did not specify enough money for the selected item, the transaction is abortedwith the supplied money not added to the machine (not accepted) and the product notpurchased (i.e. the state of the vending machine is unchanged).
5. Exit – exits the program displaying a departing message.
2. 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 other than String and standard arrays (i.e. not ArrayList, Map,Vector, etc.). These other collections will be covered later in the course.
B) When developing complex classes, consider creating a main() method to test them out. Oncetested successfully, delete the main() method.
C) You should generate error messages when appropriate, for example on invalid input values ornot enough money supplied for the selected item to purchase. Exceptions will be covered laterin the course so for this program displaying appropriate messages on the console is fine.
D) Code to input data from the console will be supplied. Java I/O programming will be coveredlater in the course.
Explanation / Answer
Hi,
It is similar to my collage level small project.
I had written it for my secifications (With the help of google indeed). Please make necessory changes as per your requirements and you can complete the program and work as per you.
import.java.util.scanner;
public class VendingMachine
{
public static void main (String args[])
{
int[4] coin = {5,5,5,5};//create an array to hold coin values
int[4] drink = {5,5,5,5}; //create an array to hold drink values
int choice;
char cont;
//String input1, input2, input3;
double price = 1.2, balance = 0, coinInsert = 0, sumCoininsert = 0;
DisplayMenu(coin, drink);
sumCoininsert = InsertCoin(coinInsert);
displayDrinks(choice);
showbalace(sumCoininsert, price);
contmethod(cont);
}
/*****************DisplayMenu method*****************/
public static int DisplayMenu (int coin, int drink)
{
int coin;
int drink;
System.out.println("[Coins available : RM 1 [" + coin[0] + "] RM0.50 [ " + coin[1] + "] RM 0.20 [ " + coin[2] + "] RM0.10 [" +coin[3] "] ]");
System.out.println("[Drinks available : Pepsi [" + drink[0] + "] Coke [" + drink[1] + "] Mirinda Orange [" + drink[2] + "] 7up [" + drink[3] + "] ]");
}
/********************DisplayMenu method*****************/
/**************InsertCoin method *******************/
public static double InsertCoin (double coinInsert)
{
double coinInsert = 0, sumCoininsert = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
while (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1')
{
Insertedcoin(coinInsert);
sumCoininsert = sumCoininsert + coinInsert;
while (sumCoinInsert < 1.20)
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
}
}
System.out.println (" Total :" +sumCoinInsert);
}
/********************InsertCoin method*******************/
/***************Insertedcoin method************************/
public static double Insertedcoin(int coinInsert)
{
double coinInsert;
Scanner sc = new Scanner(System.in);
switch (coinInsert) {
case coinInsert == '1':
coin[0]++;
break;
case coinInsert == '0.5':
coin[1]++;
break;
case coinInsert == '0.2':
coin[2]++;
break;
case coinInsert == '0.1':
coin[3]++;
break;
default:
System.out.println("Invalid coin");
sumCoininsert = 0;
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
break;
}
}
/******************Insertedcoin method*********************/
/*****************displayDrinks method **************/
public static int displayDrinks (int choice)
{
int choice;
System.out.println("Drinks [RM1.20]");
System.out.println("1.Pepsi" " 2.Coke" " 3.Mirinda Orange" " 4.7up");
Scanner sc = new Scanner(System.in);
System.out.println("Choice==>");
int choice = sc.nextInt();
while (choice == '1' || choice == '2' || choice == '3' || choice == '4')
{
if (choice =='1')
{
drink[0]--;
}else if (choice=='2')
{
drink[1]--;
}else if (choice =='3')
{
drink[2]--;
}else (choice == '4')
{
drink[3]--;
}
}
System.out.println("Invalid choice");
System.out.println("Choice==>");
int choice = sc.nextInt();
}
/*****************displayDrinks method **************/
/****************displayBalance method*******************/
public static double displayBalance(double sumCoininsert, double price)
{
double price = 1.2, balance = 0, sumCoininsert;
balance = sumCoininsert - price;
if (balance != 0.00)
{
countbalance (balance);
System.out.println(" Your balance is :" +balance)
System.out.println(" :" +fifty+ "x50" " " +twenty+ "x20" " " +tenth+ "x10" );
}else //if (balance == 0.00)
{
System.out.println("Your balance is :" +balance);
}
}
/****************displayBalance method*******************/
/****************countbalance method******************/
public static double countbalance(double balance)
{
double balance;
int ibalance = (int)(balance);
int fifty = ibalance/50;
int cfifty = ibalance%50;
int twenty = cfifty/20;
int ctwenty = cfifty%20;
int tenth = ctwenty/10;
int ctenth = ctwenty%10;
if ( fifty != 0)
{
coin[1]--;
}
if (twenty != 0)
{
coin[2]--;
}
if (tenth != 0)
{
coin[3]--;
}
}
/*************countbalance method*********************/
/************************contmethod*****************************/
public static String contmethod (char cont)
{
char cont;
Scanner sc = new Scanner(System.in);
System.out.println ("Do you want to continue? y/n :");
//input3 = in.readline();
cont = sc.charAt(0);
while (cont == 'y' || cont == 'Y')
{
continue ;
}
System.out.println("Thank You, Please come again.");
System.exit(0);
}
}
/****************contmethod*******************/
Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.