Tyrx
Tyrx

Reputation: 101

Best practice to create two-level deep selection menu in Java console

I'm about to create an application that runs off the java console as opposed to the GUI, and I had a question about the best way to approach displaying menus with another level of a submenu. I've quickly typed up the below code to sort of give you an idea of what I'm attempting to achieve.

I'm aiming for the menus of modify account and access account to both open up sub-menus with further choices. Essentially I'm trying to de-clutter the application from having too many choices (e.g. Change Account ID, Change Account Balance, Change Account Nickname, etc).

I've seen some places around the internet that seem to dislike this type of "tree-like" structure. Is there any clean way of doing something like this, or would I be forced to do something like create individual methods (i.e. AccountMenu) which would display different prompts and essentially create yet another do while loop.

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

System.out.println("Welcome to the console application");

int selection =0;

do
{
    System.out.println("[1] Create New Account");
    System.out.println("[2] Modify Account");
    System.out.println("[3] Access Account");
    System.out.println("[4] Quit"); 

    System.out.print("Insert selection: "); 
    selection = ReadConsole.nextInt();

    switch (selection)
    {
        case 1: dothislater; break;
        case 2: dothislater; break;
        case 3: dothislater; break;
        case 4: System.out.println("Application has been shut down") 
        break;
        default:
        System.out.println("The selection was invaild!");
    }
}while (selection != 4);
}

}

I'm new to java, and I'm simply doing this to fool around with what it's like. Suggestions / ideas? I should also mention that I don't want to implement a "proper" GUI like swing elements.

Upvotes: 4

Views: 5366

Answers (1)

W vd L
W vd L

Reputation: 625

You could make for every (sub)menu another function. The reason why this aint prefered is because it aint in the spirit of OOP.

public class Console {

    private int testint = 1;
    /**
     * @param args
     */
    public static void main(String[] args) {
        Console console = new Console();
        console = console.mainMenu(console);
        System.out.println("Application has been shut down");
    }

    private Console mainMenu(Console console) {
        System.out.println("Welcome to the console application");

        int selection = 0;

        do {
            System.out.println("[1] Create New Account");
            System.out.println("[2] Modify Account");
            System.out.println("[3] Access Account");
            System.out.println("[4] Quit");

            System.out.print("Insert selection: ");
            // selection = testint++;
            selection = ReadConsole.nextInt();
            switch (selection) {
            case 1: return console.submenu1(console);
            case 2: return console.submenu1(console);
            case 3: return console.submenu1(console);
            case 4: return console;
            default:
                System.out.println("The selection was invalid!");
            }
        } while (selection != 4);
        return console;
    }

    private Console submenu1(Console console) {
        System.out.println("Welcome to the SUBMENU");

        int selection = 0;

        do {
            System.out.println("[1] SUBMENU_1");
            System.out.println("[2] SUBMENU_2");
            System.out.println("[3] SUBMENU_3");
            System.out.println("[4] Return");

            System.out.print("Insert selection: ");
            //selection = ++testint;
            selection = ReadConsole.nextInt();

            switch (selection) {
            case 1: return console.submenu1(console);
            case 2: return console.submenu1(console);
            case 3: return console.submenu1(console);
            case 4: return console.mainMenu(console);
            default:
                System.out.println("The selection was invalid!");
            }
        } while (selection != 4);
        return console;
    }
}

Might need a bit more tweaking, but you might get the idea

Upvotes: 4

Related Questions