Didart

Vending Mashine

Feb 9th, 2021 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 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.  
  7.         String command = scanner.nextLine();
  8.  
  9.         double sum = 0;
  10.         //with 0.1, 0.2, 0.5, 1 and 2
  11.         while (!command.equals("Start")) {
  12.             double money = Double.parseDouble(command);
  13.  
  14.             if (money != 0.1 && money != 0.2 && money != 0.5 && money != 1 && money != 2) {
  15.  
  16.                 System.out.printf("Cannot accept %.2f%n", money);
  17.             } else {
  18.                 sum += money;
  19.             }
  20.             command = scanner.nextLine();
  21.         }
  22.  
  23.         String productName = scanner.nextLine();
  24.         //"Nuts", "Water", "Crisps", "Soda", "Coke". The prices are: 2.0, 0.7, 1.5, 0.8, 1.0
  25.         while (!productName.equals("End")) {
  26.             boolean hasMoney = true;
  27.             switch (productName) {
  28.                 case "Nuts":
  29.                     if (sum < 2) {
  30.                         System.out.println("Sorry, not enough money");
  31.                         hasMoney = false;
  32.                     } else {
  33.                         sum -= 2;
  34.                     }
  35.                     break;
  36.                 case "Water":
  37.                     if (sum < 0.7) {
  38.                         System.out.println("Sorry, not enough money");
  39.                         hasMoney = false;
  40.                     } else {
  41.                         sum -= 0.7;
  42.                     }
  43.                     break;
  44.                 case "Crisps":
  45.                     if (sum < 1.5) {
  46.                         System.out.println("Sorry, not enough money");
  47.                         hasMoney = false;
  48.                     } else {
  49.                         sum -= 1.5;
  50.                     }
  51.                     break;
  52.                 case "Soda":
  53.                     if (sum < 0.8) {
  54.                         System.out.println("Sorry, not enough money");
  55.                         hasMoney = false;
  56.                     } else {
  57.                         sum -= 0.8;
  58.                     }
  59.                     break;
  60.                 case "Coke":
  61.                     if (sum < 1) {
  62.                         System.out.println("Sorry, not enough money");
  63.                         hasMoney = false;
  64.                     } else {
  65.                         sum -= 1;
  66.                     }
  67.                     break;
  68.                 default:
  69.                     System.out.println("Invalid product");
  70.                     hasMoney = false;
  71.                     break;
  72.             }
  73.             if (!hasMoney) {
  74.                 productName = scanner.nextLine();
  75.                 continue;
  76.             }
  77.             System.out.printf("Purchased %s%n", productName);
  78.  
  79.             productName = scanner.nextLine();
  80.         }
  81.         System.out.printf("Change: %.2f", sum);
  82.  
  83.     }
  84. }
Add Comment
Please, Sign In to add comment