Advertisement
Guest User

Untitled

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