Using Java with basic coding Design a vending machine that t
Solution
import java.util.Scanner;
public class VendingMachine {
public static void main(String[] args)
{
int costOfCoke = 105; // $1.05
int costOfWater = 35; // $0.35
int costOfIceTea = 75; // $0.75
System.out.println(\"What do you want?\");
System.out.print(\"1. Coke $1.05 \ 2. Water $0.35 \ 3. Ice Tea $0.75\ Your choice: \");
Scanner reader = new Scanner(System.in);
int choice = reader.nextInt();
int amount_to_pay = 0;
do {
if(choice == 1)
{
System.out.println(\"You need to pay: $1.05\");
amount_to_pay = costOfCoke;
}
else if(choice == 2){
System.out.println(\"You need to pay: $0.35\");
amount_to_pay = costOfWater;
}
else if(choice == 3)
{
System.out.println(\"You need to pay: $0.75\");
amount_to_pay = costOfIceTea;
}
else if(choice == 4)
{
System.out.println(\"Thank you\");
return;
}
else{
System.out.println(\"Invalid Choice\");
}
} while(choice!=1 && choice!=2 && choice!=3);
System.out.print(\"How many quarters do you have? : \");
int quarters = reader.nextInt();
System.out.print(\"How many dimes do you have? : \");
int dimes = reader.nextInt();
System.out.print(\"How many nikels do you have? : \");
int nikels = reader.nextInt();
System.out.print(\"How many pennies do you have? : \");
int pennies = reader.nextInt();
int amount_paid = quarters * 25 + dimes*10 + nikels*5 + pennies;
if(amount_paid < amount_to_pay)
{
System.out.println(\"You have insufficient funds.\");
}
else
{
System.out.println(\"Enjoy your drink!!!\");
System.out.println(\"Here\'s your change....\");
int coins[] = new int[4];
coins = dispenseMoney(amount_paid - amount_to_pay);
System.out.println(\"No of Quarters : \" + coins[0]);
System.out.println(\"No of Dimes : \" + coins[1]);
System.out.println(\"No of Nickels : \" + coins[2]);
System.out.println(\"No of Pennies : \" + coins[3]);
}
}
public static int[] dispenseMoney(int n)
{
//array for storing coins
int coins[] = new int[4];
//number of quarters at index \'0\'
coins[0] = Math.round(n/25);
n = n-coins[0]*25;
//number of dime at index \'1\'
coins[1] = Math.round(n/10);
n = n-coins[1]*10;
//number of nickels at index \'2\'
coins[2] = Math.round(n/5);
n = n-coins[2]*5;
//number of penny at index \'3\'
coins[3]=n;
return coins;
}
}

