Advertisement
Guest User

Vending Machine

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