Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyClass {
- public static void main(String args[]) {
- java.util.Scanner scanner = new java.util.Scanner(System.in);
- String input = null;
- double sum = 0.0;
- double insertedCoin = 0.0;
- double nutsPrice = 2.0;
- double waterPrice = 0.7;
- double crispsPrice = 1.5;
- double sodaPrice = 0.8;
- double cokePrice = 1.0;
- while(true) {
- input = scanner.nextLine();
- if (input.equals("Start")) {
- break;
- }
- insertedCoin = Double.valueOf(input);
- if (insertedCoin == 0.1 || insertedCoin == 0.2 || insertedCoin == 0.5 || insertedCoin == 1 || insertedCoin == 2) {
- sum += insertedCoin;
- } else {
- System.out.printf("Cannot accept %.2f\n", insertedCoin);
- }
- }
- while(true) {
- input = scanner.nextLine();
- if (input.equals("End")) {
- System.out.printf("Change : %.2f", sum);
- break;
- }
- switch (input) {
- case "Nuts" : if (sum >= nutsPrice) {sum -= nutsPrice; System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
- case "Water" : if (sum >= waterPrice) {sum -= waterPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
- case "Crisps" : if (sum >= crispsPrice) {sum -= crispsPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
- case "Coke" : if (sum >= cokePrice) {sum -= cokePrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
- case "Soda" : if (sum >= sodaPrice) {sum -= sodaPrice;System.out.println("Purchased " + input);} else { System.out.println("Sorry, not enough money");}break;
- default : System.out.println("Invalid product");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement