2

I want to create a CLI interface that asks for user input, and then have a switch statement on that input. Like this:

    MyDatabase db = new MyDatabase();
    Scanner sc = new Scanner(System.in);
    String menu;
    boolean flag = false;
    System.out.println("Starting database management system . . .\n");
    while(!flag){
        System.out.println("Enter one of the following opcodes and the letter 'q' to navigate through the system.");
        System.out.println("press 0 to create students table\n" +
                "press 1 to create courses table\n" +
                "press 2 to create classes table\n" +
                "press 3 to drop students table\n" +
                "press 4 to drop courses table\n" +
                "press 5 to drop classes table\n" +
                "press 6 to input students\n" +
                "press 7 to input courses\n" +
                "press 8 to input classes\n" +
                "press 9 to print histogram of top n students in course\n" +
                "press A to print histogram of all students in course\n" +
                "press B to exit");
        menu = sc.next();
        switch (menu){
            case "0":
                db.createStudentsTable();
                break;
            case "1":
                db.createCoursesTable();
                break;
            case "2":
                db.createClassesTable();
                break;
            case "3":
                break;
            case "4":
                break;
            case "5":
                break;
            case "6":
                db.userInputStudents();
                break;
            case "7":
                db.userInputCourses();
                break;
            case "8":
                db.userInputClasses();
                break;
            case "9":

    Application.launch(Engine.class, args);


                break;
            case "A":
                break;
            case "B":
                System.out.println("Exiting . . .");
                flag = true;
        }
        if(!flag) System.out.println("Finished process. Check results above.\n");
    }

Please notice case "9". I want to start a javafx application that will create a pie chart, and then when the user closes the window it will return back to this loop where the user can input "9" and close the application again and again. Case A will be also be very similar, just a different pie chart. I am obviously running into the "Can only launch application once" error, because I do not fully understand the javafx application life cycle and entry/exit points. Can someone please point me in the right direction? Here is the code for the Engine (Javafx application) class.

public class Engine extends Application {

@Override
public void start(Stage PS) {
    try {
        PS.setTitle("CSc221 Lab 4");
        Pane P = new Pane();

        Canvas CV = addCanvas(1200, 900);
        P.getChildren().add(CV);
        Scene SC = new Scene(P, 1200, 900, MyColor.WHITE.getColor());
        PS.setScene(SC);

        PS.show();

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
}

public Canvas addCanvas(int cWidth, int cHeight) {
    Canvas CV = new Canvas(cWidth, cHeight);
    GraphicsContext GC = CV.getGraphicsContext2D();

    HistogramAlphaBet h = new HistogramAlphaBet(GC, cWidth, cHeight, 6);
    h.drawConfig();

    //MyPieChart p = new MyPieChart(GC,cWidth,cHeight);
    //p.drawConfig();
    return CV;
}}
  • See the wumpus example in the answer to [How to call launch() more than once in java](https://stackoverflow.com/questions/24320014/how-to-call-launch-more-than-once-in-java) – jewelsea Nov 23 '20 at 22:03
  • 3
    My advice is just to make the whole thing into a single JavaFX app. Instead of having numeric choices, just have a bunch of buttons in the application that the user can press to perform each of the operations. That may not make sense depending on your requirements (perhaps the exercise description asks you to write it this way), so ignore this comment if it is not relevant, but a GUI only approach appears to be a reasonable solution based on the code you have provided. – jewelsea Nov 23 '20 at 22:19
  • 1
    java naming conventions please – kleopatra Nov 24 '20 at 06:51
  • why ............? – kleopatra Nov 24 '20 at 11:09

0 Answers0