Advertisement
Guest User

vendingMachine

a guest
Jan 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class vendingMachine {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         double moneySum = 0;
  7.  
  8.         boolean isTrue = true;
  9.         while (isTrue) {
  10.             String moneyInput = sc.nextLine();
  11.             double inputMoney = 0;
  12.  
  13.             switch (moneyInput) {
  14.                 case "0.1":
  15.                 case "0.2":
  16.                 case "0.5":
  17.                 case "1":
  18.                 case "2":
  19.                     inputMoney = Double.parseDouble(moneyInput);
  20.                     moneySum += inputMoney;
  21.                     break;
  22.                 case "Start":
  23.                     break;
  24.                 default:
  25.                     inputMoney = Double.parseDouble(moneyInput);
  26.                     System.out.printf("Cannot accept %.2f",inputMoney);
  27.                     System.out.println();
  28.                     break;
  29.             }
  30.  
  31.             if (moneyInput.equals("Start")) {
  32.                 boolean orderIsFinished = true;
  33.                 while (orderIsFinished) {
  34.                     String foodInput = sc.nextLine();
  35.  
  36.                     if (foodInput.equals("End")) {
  37.                         isTrue = false;
  38.                         orderIsFinished = false;
  39.                         System.out.printf("Change: %.2f", moneySum);
  40.                     }
  41.  
  42.  
  43.                     switch (foodInput) {
  44.                         case "End":
  45.                             break;
  46.                         case "Nuts":
  47.                             if (moneySum >= 2.0) {
  48.                                 moneySum -= 2.0;
  49.                                 System.out.printf("Purchased %s", foodInput);
  50.                                 System.out.println();
  51.                             } else {
  52.                                 System.out.println("Sorry, not enough money");
  53.                                 System.out.println();
  54.                             }
  55.                             break;
  56.                         case "Water":
  57.                             if (moneySum >= 0.7) {
  58.                                 moneySum -= 0.7;
  59.                                 System.out.printf("Purchased %s", foodInput);
  60.                                 System.out.println();
  61.                             } else {
  62.                                 System.out.println("Sorry, not enough money");
  63.                                 System.out.println();
  64.                             }
  65.                             break;
  66.                         case "Crisps":
  67.                             if (moneySum >= 1.5) {
  68.                                 moneySum -= 1.5;
  69.                                 System.out.printf("Purchased %s", foodInput);
  70.                                 System.out.println();
  71.                             } else {
  72.                                 System.out.println("Sorry, not enough money");
  73.                                 System.out.println();
  74.                             }
  75.                             break;
  76.                         case "Soda":
  77.                             if (moneySum >= 0.8) {
  78.                                 moneySum -= 0.8;
  79.                                 System.out.printf("Purchased %s", foodInput);
  80.                                 System.out.println();
  81.                             } else {
  82.                                 System.out.println("Sorry, not enough money");
  83.                                 System.out.println();
  84.                             }
  85.                             break;
  86.                         case "Coke":
  87.                             if (moneySum >= 1.0) {
  88.                                 moneySum -= 1.0;
  89.                                 System.out.printf("Purchased %s", foodInput);
  90.                                 System.out.println();
  91.                             } else {
  92.                                 System.out.println("Sorry, not enough money");
  93.                                 System.out.println();
  94.                             }
  95.                             break;
  96.                         default:
  97.                             System.out.println("Invalid product");
  98.                             System.out.println();
  99.                             break;
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement