ChonoChonovUk

VendingMachineNew

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