Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class vending3tiopit {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input = "";
- double totalSum = 0;
- while (true) {
- input = sc.nextLine();
- switch (input) {
- case "0.1":
- totalSum += 0.1;
- break;
- case "0.2":
- totalSum += 0.2;
- break;
- case "0.5":
- totalSum += 0.5;
- break;
- case "1":
- totalSum += 1;
- break;
- case "2":
- totalSum += 2;
- break;
- case "Start":
- input = sc.nextLine();
- while (!input.equals("End")) {
- switch (input) {
- //"Nuts", "Water", "Crisps", "Soda", "Coke"
- //: 2.0, 0.7, 1.5, 0.8, 1.0
- case "Soda":
- if (totalSum < 0.8) {
- System.out.println("Sorry, not enough money");
- break;
- }
- totalSum -= 0.8;
- System.out.println("Purchased Soda");
- break;
- case "Coke":
- if (totalSum < 1) {
- System.out.println("Sorry, not enough money");
- break;
- }
- totalSum -= 1;
- System.out.println("Purchased Coke");
- break;
- case "Nuts":
- if (totalSum < 2) {
- System.out.println("Sorry, not enough money");
- break;
- }
- totalSum -= 2;
- System.out.println("Purchased Nuts");
- break;
- case "Water":
- if (totalSum < 0.7) {
- System.out.println("Sorry, not enough money");
- break;
- }
- totalSum -= 0.7;
- System.out.println("Purchased Water");
- break;
- case "Crisps":
- if (totalSum < 1.5) {
- System.out.println("Sorry, not enough money");
- break;
- }
- totalSum -= 1.5;
- System.out.println("Purchased Crisps");
- break;
- default:
- System.out.println("Invalid product");
- break;
- }
- input = sc.nextLine();
- }
- System.out.printf("Change: %.2f", totalSum);
- return;
- default:
- double coins = Double.parseDouble(input);
- System.out.printf("Cannot accept %.2f%n", coins);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment