BapBapuHa

Vending Machine

May 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String input = scanner.nextLine();
  9.         double sum = 0;
  10.         double coin = 0;
  11.         while (!input.equals("Start")) {
  12.             if (input.equals("0.1")) {
  13.                 coin = Double.parseDouble(input);
  14.                 sum += coin;
  15.             } else if (input.equals("0.2")) {
  16.                 coin = Double.parseDouble(input);
  17.                 sum += coin;
  18.             } else if (input.equals("0.5")) {
  19.                 coin = Double.parseDouble(input);
  20.                 sum += coin;
  21.             } else if (input.equals("1")) {
  22.                 coin = Double.parseDouble(input);
  23.                 sum += coin;
  24.             } else if (input.equals("2")) {
  25.                 coin = Double.parseDouble(input);
  26.                 sum += coin;
  27.             } else {
  28.                 coin = Double.parseDouble(input);
  29.                 System.out.printf("Cannot accept %.2f%n", coin);
  30.             }
  31.             input = scanner.nextLine();
  32.         }
  33.         String product = scanner.nextLine();
  34.         double price = 0;
  35.         while (!product.equals("End")) {
  36.             if (product.equals("Nuts")) {
  37.                 price = 2;
  38.             } else if (product.equals("Water")) {
  39.                 price = 0.7;
  40.             } else if (product.equals("Crisps")) {
  41.                 price = 1.5;
  42.             } else if (product.equals("Soda")) {
  43.                 price = 0.8;
  44.             } else if (product.equals("Coke")) {
  45.                 price = 1.0;
  46.             } else {
  47.                 System.out.println("Invalid product");
  48.                 sum += price;
  49.             }
  50.             sum -= price;
  51.             if (sum >= 0) {
  52.                 System.out.printf("Purchased %s%n", product);
  53.             } else {
  54.                 System.out.println("Sorry, not enough money");
  55.                 sum += price;
  56.             }
  57.             product = scanner.nextLine();
  58.         }
  59.         System.out.printf("Change: %.2f", sum);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment