diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..91063aa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1a0e57f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Test.iml b/Test.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Test.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Test/Main.class b/out/production/Test/Main.class new file mode 100644 index 0000000..b094360 Binary files /dev/null and b/out/production/Test/Main.class differ diff --git a/out/production/Test/Menue.class b/out/production/Test/Menue.class new file mode 100644 index 0000000..c2e9121 Binary files /dev/null and b/out/production/Test/Menue.class differ diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..c5311d3 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,28 @@ + +public class Main { + public Main(String[] args) { + Menue menue = new Menue(); + menue.putMethode("a", Main::addieren, "addiert zwei Zahlen"); + help(args); + menue.replaceHelp(Main::help); + menue.exitMessage("Bis zum nächsten mal!"); + menue.putCommands(); + } + + private static void help(String[] args) { + System.out.println("Functions: "); + System.out.println(" (a) Addieren von zwei Werten"); + System.out.println(" (h) Hilfe. "); + System.out.println(" (e) Exit. "); + } + + private static void addieren(String[] strings) { + int a = 2 + 3; + System.out.println("2 + 3 = " + a); + } + + public static void main(String[] args) { + new Main(args); + } + +} diff --git a/src/Menue.java b/src/Menue.java new file mode 100644 index 0000000..583e032 --- /dev/null +++ b/src/Menue.java @@ -0,0 +1,79 @@ +import java.util.HashMap; +import java.util.Scanner; +import java.util.function.Consumer; + +/** + * This Class is a simple text menu where you can put your methode. + */ +public class Menue { + //one HashMap is for Commands, the other is for the description. + private final HashMap> commands; + private final HashMap description; + private String exitMsg = ""; + private boolean exitMsgSet = false; + + //default Constructor initialize both HashMaps and puts help and exit commands. + public Menue() { + commands = new HashMap<>(); + description = new HashMap<>(); + description.put("h", "help"); + description.put("e", "exit"); + commands.put("h", this::help); + commands.put("e", this::exit); + } + + //while true loop for input (must be at the end of your code) + public void putCommands() { + while (true) { + input(); + System.out.println(); + } + } + + //input Methode (used by only the class) + private void input() { + Scanner scan = new Scanner(System.in); + System.out.print("$ "); + String line = scan.nextLine(); + System.out.println("Input: " + line); + + String[] args = new String[0]; + + if (commands.get(line) != null) { + commands.get(line).accept(args); + } else { + System.err.println("INVALID INPUT"); + } + } + + //default help methode + public void help(String[] args) { + System.out.println("default help, for better feeling implement the Methode. "); + System.out.println("Commands: " + description); + } + + //set exitMessage if u want + public void exitMessage(String txt){ + exitMsg = txt; + exitMsgSet = true; + } + + //exit methode + public void exit(String[] args) { + if (exitMsgSet){ + System.out.println(exitMsg); + } + System.exit(0); + } + + //for replacing the scuffed help Methode + public void replaceHelp(Consumer stringA) { + commands.replace("h", stringA); + } + + //Methode for putting your methode in the Menu + public void putMethode(String string, Consumer stringA, String text) { + commands.put(string, stringA); + description.put(string, text); + } +}