grach

Vending Machine - Fundamentals Basic exercise

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