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!
-
1navigating a CLI can be seen as traversing a state machine, with each state a menu and the edges the commands at each menuratchet freak– ratchet freak2013年03月27日 10:38:27 +00:00Commented Mar 27, 2013 at 10:38
-
yeah..but should do it with a while loop? or what..im stuck with itaclowkay– aclowkay2013年03月27日 11:10:18 +00:00Commented 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 thingyaclowkay– aclowkay2013年03月27日 11:27:17 +00:00Commented Mar 27, 2013 at 11:27
1 Answer 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;
}
-
But what If I want to encapsulate the whole loop inside a seperate menu class,How should I do that?aclowkay– aclowkay2013年03月27日 11:58:28 +00:00Commented 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– Ben3132013年03月27日 16:33:42 +00:00Commented Mar 27, 2013 at 16:33