teodor_deks

Java fundamentals - Vending Machine

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