I am a Spanish programming teacher in a vocational training course and I want my students to make a CRUD exercise on arrays. I want them to start using OOP as well and as an example, I'm going to give them this example of OO Console Menu. We haven't seen Java 8 so far. Any comments or improvements would be very appreciate.
MenuOption.java
package es.primerok.menu;
/**
* Created by Miguel-David Orrico on 18/02/2017.
*/
public abstract class MenuOption {
private String option;
private String menuLine;
public MenuOption(String option, String menuLine) {
this.option = option;
this.menuLine = menuLine;
}
@Override
public String toString() {
return this.option+" - "+this.menuLine;
}
public String getOption() {
return option;
}
public abstract void doAction();
}
Menu.java
package es.primerok.menu;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Created by Miguel-David Orrico on 18/02/2017.
*/
public class Menu {
private static final String OPTION_EXIT="0";
private static final String LEVEL_INDENTATION =" ";
private List<MenuOption> menuOptions=new ArrayList<>();
private int level=0;
public Menu(){}
public Menu(int level){
this.level=level;
}
public void add(MenuOption m){
this.menuOptions.add(m);
}
public void loopUntilExit(){
String input;
try(Scanner sc=new Scanner(System.in)) {
while (true) {
System.out.println(this);
input = sc.nextLine();
if (input.equals(OPTION_EXIT)) {
return;
}
boolean foundOption = false;
for (MenuOption m : menuOptions) {
if (m.getOption().equalsIgnoreCase(input)) {
m.doAction();
foundOption = true;
}
}
if (!foundOption) {
System.out.println("Sorry, unrecognized option");
}
}
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Choose an option, "+OPTION_EXIT+" to exit.\n");
for(MenuOption m:menuOptions){
for(int i=0;i<level;i++){
sb.append(LEVEL_INDENTATION);
}
sb.append(m).append("\n");
}
return sb.toString();
}
}
MenuTest.java
package es.primerok.menu;
/**
* Created by Miguel-David Orrico on 18/02/2017.
*/
public class MenuTest {
public static void main(String[] args){
System.out.println("es.primerok.menu.Menu Test");
Menu mainMenu=new Menu();
Menu subMenu=new Menu(1);
subMenu.add(new MenuOption("+","Add numbers") {
@Override
public void doAction() {
System.out.println("5+7=12");
}
});
subMenu.add(new MenuOption("-","Subtract some numbers") {
@Override
public void doAction() {
System.out.println("we're subtracting 6 and 5. The result is 1");
}
});
mainMenu.add(new MenuOption("1","Option number 1") {
@Override
public void doAction() {
System.out.println("This is number one");
}
});
mainMenu.add(new MenuOption("A","Print an A") {
@Override
public void doAction() {
System.out.println("I'm in Option A");
}
});
mainMenu.add(new MenuOption("sub","This has a submenu") {
@Override
public void doAction() {
subMenu.loopUntilExit();
}
});
mainMenu.loopUntilExit();
}
}
Menu
is a class already provided by the JVM. Do not hide JVM classes by Naming your own the same! – Timothy Truckle Mar 5 at 17:37