0

I am trying to make a Coffee Machine, however my program doesn't go back to the first switch loop after inputting something.

I have not encountered any error messages, and have tried to add break; statements.

All I need this program to do is return to the main switch loop so the user can keep using the program, instead of being stuck inside the second switch loop.

package machine;

import javax.crypto.spec.PSource;
import java.util.Scanner;

public class CoffeeMachine {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        int waterAmount = 400;
        int milkAmount = 540;
        int beanAmount = 120;
        int cupAmount = 9;
        int moneyAmount = 550;

        int fillWater = 0;
        int fillMilk = 0;
        int fillBeans = 0;
        int fillCups = 0;

        int takeCase = 0;

        boolean exitBoolean = false;


        String actionList = "Write action (buy, fill, take, remaining, exit):";
        String machineHas = "The coffee machine has:";
        String writeWater = "Write how many ml of water do you want to add:";
        String writeMilk = "Write how many ml of milk do you want to add: ";
        String writeBeans = "Write how many grams of coffee beans do you want to add:";
        String writeCups = "Write how many disposable cups of coffee do you want to add:";
        String action1;
        String action2;

        System.out.println(machineHas);
        System.out.println(waterAmount + " " + "of water");
        System.out.println(milkAmount + " " + "of milk");
        System.out.println(beanAmount + " " + "of coffee beans");
        System.out.println(cupAmount + " " + "of disposable cups");
        System.out.println(moneyAmount + " " + "of money");

        System.out.printf(actionList);
        action1 = s.next();

        while (!exitBoolean) {
            switch (action1) {
                case "buy":
                    System.out.printf("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    action2 = s.next();
                    switch (action2) {
                        case "3":
                            waterAmount = waterAmount - 200;
                            milkAmount = milkAmount - 100;
                            beanAmount = beanAmount - 12;
                            cupAmount = cupAmount - 1;
                            moneyAmount = moneyAmount + 6;

                            System.out.println(machineHas);
                            System.out.println(waterAmount + " " + "of water");
                            System.out.println(milkAmount + " " + "of milk");
                            System.out.println(beanAmount + " " + "of coffee beans");
                            System.out.println(cupAmount + " " + "of disposable cups");
                            System.out.println(moneyAmount + " " + "of money");
                            break;
                        case "2":
                            waterAmount = waterAmount - 350;
                            milkAmount = milkAmount - 75;
                            beanAmount = beanAmount - 20;
                            cupAmount = cupAmount - 1;
                            moneyAmount = moneyAmount + 7;

                            System.out.println(machineHas);
                            System.out.println(waterAmount + " " + "of water");
                            System.out.println(milkAmount + " " + "of milk");
                            System.out.println(beanAmount + " " + "of coffee beans");
                            System.out.println(cupAmount + " " + "of disposable cups");
                            System.out.println(moneyAmount + " " + "of money");
                            break;
                        case "1":
                            waterAmount = waterAmount - 250;
                            beanAmount = beanAmount - 16;
                            cupAmount = cupAmount - 1;
                            moneyAmount = moneyAmount + 4;

                            System.out.println(machineHas);
                            System.out.println(waterAmount + " " + "of water");
                            System.out.println(milkAmount + " " + "of milk");
                            System.out.println(beanAmount + " " + "of coffee beans");
                            System.out.println(cupAmount + " " + "of disposable cups");
                            System.out.println(moneyAmount + " " + "of money");
                            break;
                    }
                    break;
                case "fill":
                    System.out.println(writeWater);
                    fillWater = s.nextInt();

                    System.out.println(writeMilk);
                    fillMilk = s.nextInt();

                    System.out.println(writeBeans);
                    fillBeans = s.nextInt();

                    System.out.println(writeCups);
                    fillCups = s.nextInt();

                    waterAmount = waterAmount + fillWater;
                    milkAmount = milkAmount + fillMilk;
                    beanAmount = beanAmount + fillBeans;
                    cupAmount = cupAmount + fillCups;
                    moneyAmount = moneyAmount;

                    System.out.println(machineHas);
                    System.out.println(waterAmount + " " + "of water");
                    System.out.println(milkAmount + " " + "of milk");
                    System.out.println(beanAmount + " " + "of coffee beans");
                    System.out.println(cupAmount + " " + "of disposable cups");
                    System.out.println(moneyAmount + " " + "of money");

                case "take":
                    System.out.println("I gave you $" + moneyAmount);

                    moneyAmount = moneyAmount - moneyAmount;

                    System.out.println(machineHas);
                    System.out.println(waterAmount + " " + "of water");
                    System.out.println(milkAmount + " " + "of milk");
                    System.out.println(beanAmount + " " + "of coffee beans");
                    System.out.println(cupAmount + " " + "disposable cups");
                    System.out.println(moneyAmount + " of money");


                case "remaining":
                    System.out.println(machineHas);
                    System.out.println(waterAmount + " " + "of water");
                    System.out.println(milkAmount + " " + "of milk");
                    System.out.println(beanAmount + " " + "of coffee beans");
                    System.out.println(cupAmount + " " + "disposable cups");
                    System.out.println(moneyAmount + " of money");
                default:
                    System.out.println("Unknown Input!");
                case "exit":
                    exitBoolean = true;

            }
        }
    }

}

micgd
  • 51
  • 7
  • 1
    Does this answer your question? [Why can't I use 'continue' inside a switch statement in Java?](https://stackoverflow.com/questions/2521721/why-cant-i-use-continue-inside-a-switch-statement-in-java) – William Andrés Bernal Jul 15 '20 at 02:11
  • Take a look to this question https://stackoverflow.com/questions/2521721/why-cant-i-use-continue-inside-a-switch-statement-in-java – William Andrés Bernal Jul 15 '20 at 02:11
  • Hey William, Thanks for giving me that question, however that didn't solve my problem. Perhaps I should be a bit more clearer in my question? I just need the nested switch loop to go back into the main switch loop. – micgd Jul 15 '20 at 02:13
  • Get rid of the continues, use break statements and retest – Hovercraft Full Of Eels Jul 15 '20 at 02:14
  • @HovercraftFullOfEels, this still keeps the program stuck in the nested Switch. EDIT: I don't think this is a duplicate question as the other is asking about continue statements, whereas mine is stuck in a nested switch. – micgd Jul 15 '20 at 02:16
  • You've edited your question but have shown **no code** that gets rid of the continue statements and uses break statements correctly. -1 – Hovercraft Full Of Eels Jul 15 '20 at 02:23
  • Also, you have `case "exit"` *after the `default` statement – Hovercraft Full Of Eels Jul 15 '20 at 02:24
  • 1
    @micgd move the part: ```System.out.printf(actionList); action1 = s.next();``` inside if the while loop and use **break** instead of **continue**. – William Andrés Bernal Jul 15 '20 at 02:27
  • @WilliamAndrésBernal thanks, it works now. Mark it as solved – micgd Jul 15 '20 at 02:31
  • 1
    There is no such thing as a switch-loop; a switch is sequential code that is a compact way of expressing certain kinds of if-then-else statements. Your nested switch statements are embedded in a while-loop, however. – NomadMaker Jul 15 '20 at 03:55

0 Answers0