Translated Menu to English
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -6,80 +6,81 @@ import java.io.InputStreamReader;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Kleine Helferklasse für Menüs
|
* Small helper class for menus.
|
||||||
* Zuvor müssen mit addEntry Menüoptionen hinzugefügt werden. Mit
|
*
|
||||||
* der Methode show() wird das Menü angezeigt und die mit der Option
|
* First menu entries have to be created with addEntry. Afterwards the menu can be shown
|
||||||
* angegebene Konstante zurückgeliefert.
|
* on the console with show(). show() also returns the constant number of the selected menu entry.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* Menu m = new Menu("Main menu");
|
||||||
|
* m.addEntry("Work hard", 0);
|
||||||
|
* m.addEntry("Rest", 1);
|
||||||
|
* m.addEntry("Go home", 2);
|
||||||
|
* int chosenOption = m.show();
|
||||||
*
|
*
|
||||||
* Beispiel:
|
* This results in the following output on the console:
|
||||||
* Menu m = new Menu("Hauptmenü");
|
* Main menu:
|
||||||
* m.addEntry("Hart arbeiten", 0);
|
* [1] Work hard
|
||||||
* m.addEntry("Ausruhen", 1);
|
* [2] Rest
|
||||||
* m.addEntry("Nach Hause gehen", 2);
|
* [3] Go home
|
||||||
* int wahl = m.show();
|
|
||||||
*
|
|
||||||
* Angezeigt wird dann das Menü:
|
|
||||||
* Hauptmenü:
|
|
||||||
* [1] Hart arbeiten
|
|
||||||
* [2] Ausruhen
|
|
||||||
* [3] Nach Hause gehen
|
|
||||||
* --
|
* --
|
||||||
*/
|
*/
|
||||||
public class Menu {
|
class Menu {
|
||||||
private String title;
|
private String _title;
|
||||||
private ArrayList<String> labels = new ArrayList<String>();
|
private ArrayList<String> _labels = new ArrayList<>();
|
||||||
private ArrayList<Integer> returnValues = new ArrayList<Integer>();
|
private ArrayList<Integer> _returnValues = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Konstruktor.
|
* Initializes the menu object.
|
||||||
* @param title Titel des Menüs z.B. "Hauptmenü"
|
*
|
||||||
|
* @param title Title of the menu (e.g. "Main menu")
|
||||||
*/
|
*/
|
||||||
public Menu(String title) {
|
Menu(String title) {
|
||||||
super();
|
super();
|
||||||
this.title = title;
|
_title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fügt einen Menüeintrag zum Menü hinzu
|
* Adds a new menu entry
|
||||||
* @param label Name des Eintrags
|
*
|
||||||
* @param returnValue Konstante, die bei Wahl dieses Eintrags zurückgegeben wird
|
* @param label Name of the entry
|
||||||
|
* @param returnValue constant number which is returned upon selection this entry
|
||||||
*/
|
*/
|
||||||
public void addEntry(String label, int returnValue) {
|
void addEntry(String label, int returnValue) {
|
||||||
this.labels.add(label);
|
_labels.add(label);
|
||||||
this.returnValues.add(new Integer(returnValue));
|
_returnValues.add(returnValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zeigt das Menü an
|
* Shows the menu.
|
||||||
* @return Die Konstante des ausgewählten Menüeintrags
|
*
|
||||||
|
* @return The constant number of the selected menu entry.
|
||||||
*/
|
*/
|
||||||
public int show() {
|
int show() {
|
||||||
int selection = -1;
|
int selection = -1;
|
||||||
|
|
||||||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
while(selection == -1) {
|
while(selection == -1) {
|
||||||
System.out.println(title+":");
|
System.out.println(_title+":");
|
||||||
|
|
||||||
for(int i = 0; i < labels.size(); ++i) {
|
for(int i = 0; i < _labels.size(); ++i) {
|
||||||
System.out.println("["+(i+1)+"] "+labels.get(i));
|
System.out.println("[" + (i + 1) + "] " + _labels.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print("-- ");
|
System.out.print("-- ");
|
||||||
try {
|
try {
|
||||||
selection = Integer.parseInt(stdin.readLine());
|
selection = Integer.parseInt(stdin.readLine());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException | IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(selection < 1 || selection > returnValues.size()) {
|
if(selection < 1 || selection > _returnValues.size()) {
|
||||||
System.err.println("Ungültige Eingabe!");
|
System.err.println("Invalid input!");
|
||||||
selection = -1;
|
selection = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnValues.get(selection-1);
|
return _returnValues.get(selection-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user