I need to do some basic Java console application with menus and submenus. The structure should look something like this:
Main menu:
(1) Menu 1
(2) Menu 2
(0) Exit
Menu 1
(1) Submenu 1
(2) Submenu 2
(3) Submenu 3
(4) Return to menu 1
So, when user enters 1, Menu 1 will be printed, etc…
My question is, can I achieve this kind of thing with some pattern, and create more dynamic approach, or am I stuck with if switch statements ?
Thanks in advance
Instead of using switch statements, you could create an object which contains an array of strings for the submenus, and an array of submenu objects numbered corresponding to their keypress value. For this you can use a method like this to return the submenu:
I’ll leave the rest of the implementation up to you.