Advertisement
Helena12

Vending Machine

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