Advertisement
Guest User

Untitled

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