0

I want to build a command line interface with menus and sub-menus and eventually the leafs of the tree should be operations like.. bank management -> account management -> add acount (insert account info) And I'm looking for a good way which works with the OO principles...

Thanks for any piece of advice!

3
  • 1
    navigating a CLI can be seen as traversing a state machine, with each state a menu and the edges the commands at each menu Commented Mar 27, 2013 at 10:38
  • yeah..but should do it with a while loop? or what..im stuck with it
    – aclowkay
    Commented Mar 27, 2013 at 11:10
  • I tried making a "menunude" abstract class which has the sub classes menu and operation..menu has a list of menus and operation has an execute function which does whatever it's supposed to do...the problem is what to put in the main thingy
    – aclowkay
    Commented Mar 27, 2013 at 11:27

1 Answer 1

1

if you want OO I suggest a Menu interface with methods to write out the options and parsing the input and returning the next Menu

so your main loop becomes:

Menu menu = new StartMenu();
    while(menu.isExit){
    menu.writeOptions(stdOut);
    Menu res = menu.parseInput(stdIn.readline());
    if(res==null)
        writeError(stdOut);
    else
        menu=res;
}
2
  • But what If I want to encapsulate the whole loop inside a seperate menu class,How should I do that?
    – aclowkay
    Commented Mar 27, 2013 at 11:58
  • You could put this code inside a static method in the base class if you really wanted to. Call it RunMenu().
    – Ben313
    Commented Mar 27, 2013 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.