Design a vending machine that takes coins of 1, 5, 10, and 25 cents. Ask the use
ID: 3797350 • Letter: D
Question
Design a vending machine that takes coins of 1, 5, 10, and 25 cents. Ask the user what he wants to purchase, by selecting {1} for a Coke, {2} for a Water, or {3} for an Ice Tea. A Coke costs 1 Dollar and 5 cents, a water costs 35 cents, and an Ice Tea costs 75 cents. Tell the user how much he has to pay, then ask for the money. The user will input how many quarters he has, how many dimes, how many nickels, and how many pennies. Calculate if the user paid enough money and if so, dispense the drink. If not, let the user know that he has insufficient funds. After dispensing the drink, return the user's change in the fewest amount of coins possible to the user (i.e. output in the console how many of each coin he receives back, if any).Explanation / Answer
package org.students;
import java.text.DecimalFormat;
import java.util.Scanner;
public class VendingMachine {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
//Scanner object is used to get the inputs entered by the user
// Scanner sc=new Scanner(System.in);
//Declaring variables
int itemChoice;
int itemCost;
while(true)
{
System.out.println(" :: Vending Machine ::");
System.out.println("1.Coke");
System.out.println("2.Water");
System.out.println("3.Ice Tea");
System.out.print("Enter choice :");
itemChoice=sc.nextInt();
switch(itemChoice)
{
case 1:{
itemCost=105;
moneymenu(itemCost);
break;
}
case 2:{
itemCost=35;
moneymenu(itemCost);
break;
}
case 3:{
itemCost=75;
moneymenu(itemCost);
break;
}
default :{
System.out.print("Invalid Choice.Must be between 1-3");
continue;
}
}
break;
}
}
private static void moneymenu(int amt) {
int balance=0;
char choice;
System.out.println("You have to pay "+amt+" cents");
while(true)
{
//Displaying menu
System.out.println(" Money:");
System.out.println("Q - Quarter");
System.out.println("D - Dime");
System.out.println("N - Nickel");
System.out.println("P - Penny");
//Getting the choice entered by the user.
System.out.print("Enter choice :");
choice=sc.next(".").charAt(0);
//Based on the users choice the corresponding case will get executed.
switch(choice)
{
case 'Q':
case 'q':
{
//Adding 0.25 to the current balance
balance=balance+25;
//Displaying the balance after adding money
System.out.println("Amount :"+balance+" cents");
if(balance>=amt)
{
System.out.println("Product Dispensed ");
//Subtracting amount from balance
balance=balance-amt;
System.out.println("Balance Amount Returned :"+balance+" cents");
if(balance>=0)
denominations(balance);
break;
}
else
{
System.out.println("Product can't be dispensed");
continue;
}
}
case 'D':
case 'd':{
//Adding 0.10 to the current balance
balance=balance+10;
//Displaying the balance after adding money
System.out.println("Amount :"+balance+" cents");
if(balance>=amt)
{
System.out.println("Product Dispensed ");
//Subtracting amount from balance
balance=balance-amt;
System.out.println("Balance Amount Returned :"+balance+" cents");
if(balance>=0)
denominations(balance);
break;
}
else
{
System.out.println("Product can't be dispensed");
continue;
}
}
case 'N':
case 'n':{
//Adding 0.05 to the current balance
balance=balance+5;
//Displaying the balance after adding money
System.out.println("Amount :"+balance+" cents");
if(balance>=amt)
{
System.out.println("Product Dispensed ");
//Subtracting amount from balance
balance=balance-amt;
System.out.println("Balance Amount Returned :"+balance+" cents");
if(balance>=0)
denominations(balance);
break;
}
else
{
System.out.println("Product can't be dispensed");
continue;
}
}
case 'P':
case 'p':{
//Adding 0.01 to the current balance
balance=balance+1;
//Displaying the balance after adding money
System.out.println("Amount :"+balance+" cents");
if(balance>=amt)
{
System.out.println("Product Dispensed ");
//Subtracting amount from balance
balance=balance-amt;
System.out.println("Balance Amount Returned :"+balance+" cents");
if(balance>=0)
denominations(balance);
break;
}
else
{
System.out.println("Product can't be dispensed");
continue;
}
}
}
break;
}
}
private static void denominations(int balance) {
final int QUARTER=25,DIME=10,NICKEL=5,PENNY=1;
int cntQuarters=0,cntDimes=0,cntNickels=0,cntPennies=0;
int bal=balance;
cntQuarters=bal/QUARTER;
bal=bal-(cntQuarters*QUARTER);
cntNickels=bal/DIME;
bal=bal-(cntNickels*DIME);
cntDimes=bal/NICKEL;
bal=bal-(cntDimes*NICKEL);
cntPennies=bal/PENNY;
System.out.println("No of Quarters :"+cntQuarters);
System.out.println("No of Nickels :"+cntNickels);
System.out.println("No of Dimes :"+cntDimes);
System.out.println("No of Pennies :"+cntPennies);
}
}
__________________
Output:
:: Vending Machine ::
1.Coke
2.Water
3.Ice Tea
Enter choice :1
You have to pay 105 cents
Money:
Q - Quarter
D - Dime
N - Nickel
P - Penny
Enter choice :Q
Amount :25 cents
Product can't be dispensed
Money:
Q - Quarter
D - Dime
N - Nickel
P - Penny
Enter choice :Q
Amount :50 cents
Product can't be dispensed
Money:
Q - Quarter
D - Dime
N - Nickel
P - Penny
Enter choice :Q
Amount :75 cents
Product can't be dispensed
Money:
Q - Quarter
D - Dime
N - Nickel
P - Penny
Enter choice :Q
Amount :100 cents
Product can't be dispensed
Money:
Q - Quarter
D - Dime
N - Nickel
P - Penny
Enter choice :Q
Amount :125 cents
Product Dispensed
Balance Amount Returned :20 cents
No of Quarters :0
No of Nickels :2
No of Dimes :0
No of Pennies :0
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.