Advertisement
ValentinM

VendingMachine

May 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 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.         double sum = 0;
  8.         int total = 0;
  9.  
  10.         String textInput = "";
  11.         while (true) {
  12.             textInput = scanner.nextLine();
  13.             if (textInput.equals("Start")) {
  14.                 break;
  15.             }
  16.             double coin = Double.parseDouble(textInput);
  17.             if (coin == 0.1 || coin == 0.2 || coin == 0.5 || coin == 1 || coin == 2) {
  18.                 sum += coin;
  19.             } else {
  20.                 System.out.printf("Cannot accept %.2f%n" ,coin);
  21.             }
  22.         }
  23.         double price = 0;
  24.         String input;
  25.         boolean flag = false;
  26.         while (true) {
  27.             input = scanner.nextLine();
  28.             switch (input.toLowerCase()) {
  29.                 case "nuts":
  30.                     price = 2;
  31.                     flag = true;
  32.                     break;
  33.                 case "water":
  34.                     price = 0.7;
  35.                     flag = true;
  36.                     break;
  37.                 case "crisps":
  38.                     price = 1.5;
  39.                     flag = true;
  40.                     break;
  41.                 case "soda":
  42.                     price = 0.8;
  43.                     flag = true;
  44.                     break;
  45.                 case "coke":
  46.                     price = 1;
  47.                     flag = true;
  48.                     break;
  49.             }
  50.             if (input.equals("End")) {
  51.                 break;
  52.             }
  53.             if (sum < price) {
  54.                 System.out.println("Sorry, not enough money");
  55.             } else if (flag) {
  56.                 System.out.println("Purchased " + input);
  57.                 sum -= price;
  58.             } else {
  59.                 System.out.println("Invalid product");
  60.             }
  61.         }
  62.         System.out.printf("Change: %.2f" ,sum);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement