Advertisement
ChonoChonovUk

VendingMachine

Sep 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class VendingMachine {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String toCoin = scanner.nextLine();
  7.         double tot = 0;
  8.         double coin = 0;
  9.         while (!toCoin.equals("Start")){
  10.             coin = Double.parseDouble(toCoin);
  11.             if (coin == 0.1){
  12.                 tot += 0.1;
  13.             }else if (coin == 0.2){
  14.                 tot += 0.2;
  15.             }else if (coin == 0.5){
  16.                 tot += 0.5;
  17.             }else if (coin == 1){
  18.                 tot += 1;
  19.             }else if (coin == 2){
  20.                 tot += 2;
  21.             }else{
  22.                 System.out.printf("Cannot accept %.2f",coin);
  23.             }
  24.             toCoin = scanner.nextLine();
  25.         }
  26.           coin = tot;
  27.         String toDrink = scanner.nextLine();
  28.  
  29.         while (!toDrink.equals("End")){
  30.             switch (toDrink){
  31.                 case "Nuts":
  32.                     if (coin > 2){
  33.                         coin -= 2;
  34.                     }else {
  35.                         System.out.println("Sorry, not enough money");
  36.                         toDrink = scanner.nextLine();
  37.                         continue;
  38.                     }
  39.                     break;
  40.                 case "Water":
  41.                     if (coin > 0.7){
  42.                         coin -= 0.7;
  43.                     }else {
  44.                         System.out.println("Sorry, not enough money");
  45.                        
  46.                     }
  47.                     break;
  48.                 case "Crisps":
  49.                     if (coin > 1.5){
  50.                         coin -= 1.5;
  51.                     }else {
  52.                         System.out.println("Sorry, not enough money");
  53.                        
  54.                     }
  55.                     break;
  56.                 case "Soda":
  57.                     if (coin > 0.8){
  58.                         coin -= 0.8;
  59.                     }else {
  60.                         System.out.println("Sorry, not enough money");
  61.                        
  62.                     }
  63.                     break;
  64.                 case "Coke":
  65.                     if (coin > 1){
  66.                         coin -= 1;
  67.                     }else {
  68.                         System.out.println("Sorry, not enough money");
  69.                        
  70.  
  71.                     }
  72.                     break;
  73.             }
  74.  
  75.             toDrink = scanner.nextLine();
  76.  
  77.         }
  78.  
  79.         if (toDrink.equals("End")){
  80.             System.out.printf("Change: %.2f",coin);
  81.         }
  82.  
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement