Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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!

share|improve this question
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 –  ratchet freak Mar 27 '13 at 10:38
    
yeah..but should do it with a while loop? or what..im stuck with it –  user1050389 Mar 27 '13 at 11:10
    
what have you tried? –  gnat Mar 27 '13 at 11:23
    
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 –  user1050389 Mar 27 '13 at 11:27
add comment

1 Answer

up vote 1 down vote accepted

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;
}
share|improve this answer
    
Thank you, that's exactly what I've been looking for!! –  user1050389 Mar 27 '13 at 11:30
    
But what If I want to encapsulate the whole loop inside a seperate menu class,How should I do that? –  user1050389 Mar 27 '13 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 Mar 27 '13 at 16:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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