Advertisement
psi_mmobile

Untitled

Aug 27th, 2022
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. public class MyClass {
  2.     public static void main(String args[]) {
  3.         java.util.Scanner scanner = new java.util.Scanner(System.in);
  4.         String input = null;
  5.         double sum = 0.0;
  6.         double insertedCoin = 0.0;
  7.        
  8.         double nutsPrice = 2.0;
  9.         double waterPrice = 0.7;
  10.         double crispsPrice = 1.5;
  11.         double sodaPrice = 0.8;
  12.         double cokePrice = 1.0;
  13.         while(true) {
  14.             input = scanner.nextLine();
  15.             if (input.equals("Start")) {
  16.                 break;
  17.             }
  18.             insertedCoin = Double.valueOf(input);
  19.             if (insertedCoin == 0.1 || insertedCoin == 0.2 || insertedCoin == 0.5 || insertedCoin == 1 || insertedCoin == 2) {
  20.                 sum += insertedCoin;
  21.             } else {
  22.                 System.out.printf("Cannot accept %.2f\n", insertedCoin);
  23.             }
  24.         }
  25.         while(true) {
  26.             input = scanner.nextLine();
  27.             if (input.equals("End")) {
  28.                 System.out.printf("Change : %.2f", sum);
  29.                 break;
  30.             }
  31.             switch (input) {
  32.                 case "Nuts" : if (sum >= nutsPrice) {sum -= nutsPrice; System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
  33.                 case "Water" : if (sum >= waterPrice) {sum -= waterPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
  34.                 case "Crisps" : if (sum >= crispsPrice) {sum -= crispsPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
  35.                 case "Coke" : if (sum >= cokePrice) {sum -= cokePrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
  36.                 case "Soda" : if (sum >= sodaPrice) {sum -= sodaPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
  37.                 default : System.out.println("Invalid product");
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement