SophiYo

VendingMachine

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