Search code examples
javaoopswitch-statementcase-statement

How to create a nested menu in Java?


I'm trying to create a menu system where the user picks whether there an admin or a customer, and then they can enter there login details.

Once signed in the admin and the customer are presented with different menus. I also want to be able to log off and it will go back to the login menu where they are asked if there a admin or a customer.

This is what i have so far:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){

        login login = new login();
        Scanner in = new Scanner(System.in);

        String user;
        String pwd;


        int loginOption = 0;

        login.databaseConnection();

        System.out.println("======================================");
        System.out.println("|     Please select and option:      |");
        System.out.println("======================================");
        System.out.println("| Options:                           |");
        System.out.println("|        [1] Admin                   |");
        System.out.println("|        [2] Account Holder          |");
        System.out.println("|        [3] Exit                    |");
        System.out.println("======================================");



        do{
            System.out.print("Option:");
            loginOption = in.nextInt();

            // Switch construct
            switch (loginOption) {
                case 1:
                    System.out.println("");
                    System.out.println("Username: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");
                  break;
                case 2:
                    System.out.println("");
                    System.out.println("Account Number: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");

                    if(login.validate_login(user, pwd)){
                         System.out.println("Valid Login");
                         System.out.println("");
                         System.out.println("Hello, Account Holder");
                         System.out.println("");
                         System.out.println("======================================");
                         System.out.println("|     Please select and option:      |");
                         System.out.println("======================================");
                         System.out.println("| Options:                           |");
                         System.out.println("|        [1] Check balence           |");
                         System.out.println("|        [2] Withdraw                |");
                         System.out.println("|        [3] Logout                  |");
                         System.out.println("======================================");

                         int userOption = 0;

                         do{
                             System.out.println("Option");
                             userOption = in.nextInt();
                             switch (userOption){
                                case 1:
                                    break;
                                case 2:
                                    break;
                                case 3:
                                    loginOption = 3;
                                    break;
                             }
                         }while (loginOption != 3);
                    }else{
                         System.out.println("Invalid");
                    }
                  break;
                case 3:
                    System.out.println("Exit selected");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid selection");
                    break; // This break is not really necessary
            }
        }while (loginOption != 3);


        in.close();   
    }
}

The main problem I'm having at them moment is leaving the second switch and back to the first one.

Any help would be appreciated :)


Solution

  • You can add a boolean variable ext like this:

                          boolean ext = true;
                          do{
                            ...
                                case 3:
                                    loginOption = 3;
                                    ext = false;
                                    break;
                             }
                         }while (loginOption != 3 && ext);
    

    With that you can exit from while loop when yuou have loginOption = 3;