Now this might sound simple, but I’m a bit mixed up. I am trying to create a menu in my application. Depending on certain conditions, some options of the menu will be enabled/disabled. Since it is console-based, I also want to specify for each “state” of the menu which commands are available to the user.
Example :
EMPTY_STATE has ADD_COMMAND, REMOVE_COMMAND, QUIT_COMMAND
FULL_STATE has ADD_COMMAND, VIEW_COMMAND, REMOVE_COMMAND, QUIT_COMMAND
I’d like my application to be as flexible as possible meaning that I can easily add more states and customize the commands that can be used. That means I would like to avoid doing columns of if’s to know that is the action taken.
Since each state has different commands, I thought of creating an ENUM named State which countains those different states. I also created an ENUM called Commands which contains all of the possible commands the user could do while using my application.
My basic idea was simply :
EMPTY_STATE(Commands.ADD_COMMAND,Commands.REMOVE_COMMAND,Commands.QUIT_COMMAND)
Thus, State countains Commands, and Commands countains their label, which is a string of the command prefix (“-add”)
But then, States can countain many commands and that is the problem. Since I can’t and do not want to do a constructor for each case in the ENUM, I thought about using an ArrayList that would countain all the commands, making it :
EMPTY_STATE(Arraylist array = commands) <-- Just the idea
And that is where my question goes in. As the documentation say, an arraylist can take a collection in parameter to assign its intern content.
Could I have some suggestion of how I should structure this to make it clean and easy to manage ?
What’s wrong with storing the available commands for each state?
After all, you will have to define this somewhere.
You can pass an EnumSet of the available commands to the constructor for each State enum like this:
As an alternative, you could specify for each command in which state it will be available.