Advertisement
desislava_topuzakova

07. Vending Machine

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