Skip to content

Instantly share code, notes, and snippets.

@Skyl0
Created June 21, 2017 14:34
Show Gist options
  • Save Skyl0/2e48dc81fe1559de4ccc069660d6e03a to your computer and use it in GitHub Desktop.
Save Skyl0/2e48dc81fe1559de4ccc069660d6e03a to your computer and use it in GitHub Desktop.
Why doesn't this implementation of Runnables (want to run it at a later stage) work at all? ( SEE The Important Part at the bottom)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.skyit.consolemenu.helper;
import com.skyit.consolemenu.model.MenuItem;
import java.util.Scanner;
/**
*
* @author skyadmin
*/
public class MainMenuTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Some Threads
MyThread thread = new MyThread();
// Some Functions
Runnable runner1 = () -> {
thread.run();
};
Runnable runner2 = () -> {
System.out.println("Runner2");
};
/**
* Menu Part - Root First
*/
MenuItem root = new MenuItem("Hauptmenü", null, null);
root.setIsRoot(true);
// Set First Level Items
runner1.run();
runner2.run();
MenuItem first_a = new MenuItem("Alpha", root, runner1);
MenuItem first_b = new MenuItem("Bravo", root, runner2);
MenuItem first_c = new MenuItem("Charlie", root, () -> {
System.out.println("RUN3");
});
root.addChild(first_a);
root.addChild(first_b);
root.addChild(first_c);
// Execute Menu
MenuItem current = root;
Scanner s = new Scanner(System.in);
int cursor = -1;
while (current != null) {
current.printMenu();
int plus1 = (current.getChildren().size() + 1);
if (current.isRoot()) {
System.out.println(plus1 + ") Beenden");
} else {
System.out.println(plus1 + ") Voriges Menü");
}
System.out.print("Ihre Wahl: ");
cursor = root.readInt(s, 1, current.getChildren().size() + 1);
if (cursor > current.getChildren().size()) {
current = current.getParent();
} else {
if (current.getRunner() != null) {
/**
* The Important Part
* | | | | |
* V V V V V
**/
current.run();
}
current = current.getChildAt(cursor - 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment