Using Java with basic coding Design a vending machine that t

Using Java with basic coding: Design a vending machine that takes coins of 1, 5, 10, and 25 cents. Ask the user what he wants to purchase, by selecting for a Coke, for a Water, or 13 for an lce Tea A Coke costs 1 Dollar and 5 cents, a water costs 35 cents, and an Ice Tea costs 75 cents. he has, how many Tell the user how much he has to pay, then ask for the money. The user will input how many quarters 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 e. output in the console how many of each coin he receives back, if any

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

 Using Java with basic coding: Design a vending machine that takes coins of 1, 5, 10, and 25 cents. Ask the user what he wants to purchase, by selecting for a C
 Using Java with basic coding: Design a vending machine that takes coins of 1, 5, 10, and 25 cents. Ask the user what he wants to purchase, by selecting for a C

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site