Search code examples
javamenuswitch-statementtext-basedsystem.in

How can I make multiple text based java menus work?


I have a main menu for a movie kiosk, I can type a number (in this case 5) and it will take me to an admin menu. The problem is the admin menu has a different set of cases and when I enter a number to access a case from the admin menu it just takes me back to the main menu.

I am not sure what I could change in the code to make it work, I expected that when I enter a number in the admin menu, it will execute the method specified.

At the beginning I call the use() method from the Kiosk class Which executes this menu:

When I press 2 it takes me to the main menu instead of executing addCustomer().


Solution

  • I think you want to implement it like this:

    while ((choice = readChoice()) != 'X') {
            switch (choice) {
            case '1': catalogue(); break;
            case '2': useAdmin(); break;
            }
        }
    
    private void useAdmin() {
        char choice = admin(); // instead of assigning 5 to this option
            switch (choice) {
            case '1': listCustomer(); break;
            case '2': addCustomer(); break;
            case 'R': use();
        }
    }
    

    In your current implementation you call the admin() function which returns a char.

     switch (choice) {
            case '1': catalogue(); break;
            case '2': admin(); break;
            }
    

    note that you don't do anything with the char the admin() function returns. Later on you just assign the character 5 to the char choice.