-
Notifications
You must be signed in to change notification settings - Fork 40
Quick Start
I advise you to read the following quick start. Each example given below is also available in the package commandLineMenus.examples.
Then, feel free to look at the javadoc.
The framework organizes the user dialog between menus and options. It allows the user to navigate through menus. In each menu, the user chooses an option, the selection of an option triggers an action.
In the following example, we show how to create a menu, and how to include an option in this menu.
// Creates a menu with the title Hello
Menu helloMenu = new Menu("Hello Menu");
// Creates an option with the title "Say Hello"
// the shorcut to select it is "h"
Option sayHelloOption = new Option("Say Hello", "h");
// Adds the option sayHello to the menu.
helloMenu.add(sayHelloOption);
// Adds the option allowing to close the menu.
helloMenu.addQuit("q");
// Creates an action that will be binded to the sayHello option.
Action sayHelloAction = new Action()
{
// optionSelected() is triggered each time
// the "sayHello" option is selected.
@Override
public void optionSelected()
{
System.out.println("Hello!");
}
};
// Binds sayHelloAction to the option sayHelloOption
sayHelloOption.setAction(sayHelloAction);
// Launches the menu
helloMenu.start();
// Must be printed once the menu has been left.
System.out.println("Good bye!"); The previous code displays the following menu.
Hello Menu
h : Say Hello
q : Exit
Select an option :
Inputing the shortcut "h" selects the option "Say Hello", and then inputing "q" closes the application.
You can notice that selecting the option "Say Hello" automatically triggers the method optionSelected().
Since Menu inherits Option, it is possible to nest a menu A inside a menu B. Then A will appear as a sub-menu of B. The user will have the possibility to select A as if it was an option of the menu B, and the action associated with this selection will automatically be the opening of A.
// Creates the root menu of the application
Menu rootMenu = new Menu("Root Menu");
// Creates two options
Option calculatorOption = new Option("Calculator", "c");
Menu sayHelloMenu = new Menu("Say Hello Sub-Menu", "Hello", "h");
// Adds an option to the rootMenu
rootMenu.add(calculatorOption);
// Adds the sub-menu sayHelloMenu to the rootMenu
// Please notice that since Menu extends Option, polymorphism allows us to pass
// the Menu sayHelloMenu where an Option was expected.
rootMenu.add(sayHelloMenu);
// Adds the quit option
rootMenu.addQuit("q");
// Defines the action that will be triggered if the calculator is selected.
calculatorOption.setAction(new Action()
{
// Method triggered if the calculatorOption is selected
public void optionSelected()
{
int a = InOut.getInt("Input the first operand : "),
b = InOut.getInt("Input the second operand : ");
System.out.println("" + a + " + " + b + " = " + (a+b));
}
});
// Please notice that the action can be passed to the constructor of Option
sayHelloMenu.add(
new Option("Say Hello", "h", new Action()
{
public void optionSelected()
{
System.out.println("Hello!");
}
}));
// Adds an option to go back to the rootMenu
sayHelloMenu.addBack("r");
// Once an option has been selected in sayHelloMenu, and the associated action
// is done, we will automatically go back to the rootMenu.
sayHelloMenu.setAutoBack(true);
rootMenu.start();Here is an example of how the program behaves :
Root Menu
c : Calculator
h : Hello
q : Exit
Select an option : c
Input the first operand : 2
Input the second operand : 3
2 + 3 = 5
---------------------------
Root Menu
c : Calculator
h : Hello
q : Exit
Select an option : h
---------------------------
Say Hello Sub-Menu
h : Say Hello
r : Back
Select an option : h
Hello!
---------------------------
Root Menu
c : Calculator
h : Hello
q : Exit
Select an option: q
One can also populate a menu with items from a list. Then you can for instance display in a list items extracted from a database, and ask the user to select one.
// Creates a list containing "Ginette", "Marcel" et "Gisèle"
final ArrayList<String> people = new ArrayList<>();
people.add("Ginette");
people.add("Marcel");
people.add("Gisèle");
// Creates a menu with an option for each people in the list
List<String> menu = new List<String>("People list",
new ListData<String>()
{
// Returns the data needed to refresh the list
// each time it is displayed.
public java.util.List<String> getList()
{
return people;
}
},
new ListAction<String>()
{
// Triggered each time an item is selected
public void itemSelected(int index, String someone)
{
System.out.println("You have selected " + someone
+ ", who has the index " + index);
}
});
menu.addQuit("q");
menu.start();Here is an example
1 : Ginette
2 : Marcel
3 : Gisèle
q : Quitter
2
You have selected Marcel, who has the index 2
The existence of a getList() seems confusing, but since the data can change while the application is running (for instance if you ask a user to select an item for deletion), it is required to refresh the list before displaying it.
And achtung ! In java.util.List indexes start from 0, but in order not to confuse the user, the indexes displayed start from 1.
It is advised to organise the code in functions so that each menu, option and action is in a separate function. Use the return values of the functions to retrieve them.
// Returns the root menu
static Menu getMainMenu()
{
Menu mainMenu = new Menu("Main Menu");
// Adds a submenu or an option is then done
// with a method call
mainMenu.add(getCalculatorOption());
mainMenu.add(getSayHelloMenu());
mainMenu.addQuit("q");
return mainMenu;
}
// Returns the calculator option
static Option getCalculatorOption()
{
Option calculator = new Option("Calculator", "c",
getCalculatorAction());
return calculator;
}
// It is advised to write each action in a separate method
static Action getCalculatorAction()
{
return new Action()
{
public void optionSelected()
{
int a = InOut.getInt("Input the first operand : "),
b = InOut.getInt("Input the second operand : ");
System.out.println("" + a + " + " + b + " = " + (a+b));
}
};
}
static Action getSayHelloAction()
{
return new Action()
{
public void optionSelected()
{
System.out.println("Hello!");
}
};
}
static Option getSayHelloOption()
{
return new Option("Say Hello", "h", getSayHelloAction());
}
// It is also advised to create each menu in a separated method
static Menu getSayHelloMenu()
{
Menu sayHelloMenu = new Menu("Say Hello Menu", "Hello", "h");
sayHelloMenu.add(getSayHelloOption());
sayHelloMenu.addBack("r");;
sayHelloMenu.setAutoBack(true);
return sayHelloMenu;
}
public static void main(String[] args)
{
Menu menu = getMainMenu();
menu.start();
}The code above is the same than in the section "Nesting Menus".
If you want to nest submenus in a list, you can use a ListOption.
public class ListOptions
{
// If you have to manipulate data, use a private non static field.
private java.util.List<String> people ;
ListOptions(java.util.List<String> people)
{
this.people = people;
List<String> list = getPeopleList();
list.start();
}
// Returns the list to print
private List<String> getPeopleList()
{
List<String> liste = new List<>("Select someone",
getListDataPeople(),
getOptionListePeople());
liste.setAutoBack(false);
liste.addQuit("q");
return liste;
}
private ListData<String> getListDataPeople()
{
return new ListData<String>()
{
@Override
public java.util.List<String> getList()
{
return people;
}
};
}
private ListOption<String> getOptionListePeople()
{
return new ListOption<String>()
{
// Each person will become an option
// The following method returns the option
// associated with each one.
public Option getOption(String someone)
{
return getSomeoneMenu(someone);
}
};
}
// Creates an sub-menu for someone.
private Option getSomeoneMenu(final String someone)
{
Menu someoneMenu = new Menu(someone);
someoneMenu.add(getDisplaySomeoneOption(someone));
someoneMenu.add(getDeleteSomeoneOption(someone));
someoneMenu.setAutoBack(true);
return someoneMenu;
}
// Returns the option to display someone
private Option getDisplaySomeoneOption(String someone)
{
return new Option("show", "s", new Action()
{
@Override
public void optionSelected()
{
System.out.println("You must give the man a name : " + someone + ".");
}
});
}
// Returns the option to delete someone
private Option getDeleteSomeoneOption(String someone)
{
return new Option("delete", "d", new Action()
{
@Override
public void optionSelected()
{
people.remove(someone);
System.out.println(someone + " has been deleted.");
System.out.println(people);
}
});
}
// Do not use the main to create the menus, always create
// into functions.
public static void main(String[] args)
{
java.util.List<String> people = new ArrayList<>();
people.add("Ginette");
people.add("Marcel");
people.add("Gisèle");
new ListOptions(people);
}
}Here is an example of how these menus behave :
Select someone
1 : Ginette
2 : Marcel
3 : Gisèle
q : Exit
Select an option: 2
---------------------------
Marcel
s : show
d : delete
Select an option: d
Marcel has been deleted.
---------------------------
Select someone
1 : Ginette
2 : Gisèle
q : Exit
Select an option : Please notice that the people list can change between two displays.
Since the anonymous functions can make the code very hard to read, it is strongly advised to use lambda expressions. For instance, the first example can be rewritten
Menu menu = new Menu("Menu bonjour");
Option direBonjour = new Option("Dire Bonjour", "b");
menu.add(direBonjour);
menu.addQuit("q");
direBonjour.setAction( () -> System.out.println("Bonjour !") );
menu.start();direBonjour.setAction( () -> System.out.println("Bonjour !") ); binds the action System.out.println("Bonjour !") to the option direBonjour.
The ListOptions example was quite hard to read, and is worth being rewritten with lambda expressions :
public class ListOptions
{
java.util.List<String> people;
public ListOptions(java.util.List<String> people)
{
this.people = people;
List<String> list = getPeopleList(people);
list.start();
}
private List<String> getPeopleList(final java.util.List<String> people)
{
List<String> liste = new List<>("Select someone to display his name",
() -> people,
getListOptionSomeone()
);
liste.setAutoBack(false);
liste.addQuit("q");
return liste;
}
private ListOption<String> getListOptionSomeone()
{
return (String someone) -> new Option("Display " + personne, null, () -> System.out.println(someone));
}
public static void main(String[] args)
{
java.util.List<String> people = new ArrayList<>();
people.add("Ginette");
people.add("Marcel");
people.add("Gisèle");
new ListOptions(people);
}
}You will find for each example an implementation using lambda expression in the package commandLineMenus.examples.lambda.