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