mercredi 12 septembre 2018

define navigation on the basis of inputs from cli in java

I am creating a simple game based on Java and taking inputs from cli from user. There are multiple options for navigation as mentioned below. Now, user can enter any option, then he will get another set of options. I wrote this code using the switch cases, then inside of the selected case, there is again switch case, so and so forth. Now it has become messy and it looks ugly.

1 - See all cars
2 - create new user
3 - select a new car
4 - Exit
5 - Go back

Kindly suggest, a better way to achieve this. Is there any design pattern. Please note that I can not use GUI and it is simple cli based in which I have to take number as inputs.

Code sample:

    System.out.println("1 - Start..");
    System.out.println("2 - Exit..");


    int input = scan.nextInt();
    switch (input) {
        case 1: {
            System.out.println("1 - Create new user..");
            System.out.println("2 - Go with default one..");
            System.out.println("3 - View all cars..");
            System.out.println("4 - Exit..");
            int inputTemp = scan.nextInt();
            switch (input) {
                case 1: {
                ///// So on and so forth
           }
           }
        }
        case 2: {
            System.exit(0);
        }
        default: {
            System.out.println("invalid input");
            System.exit(0);
        }
    }

Aucun commentaire:

Enregistrer un commentaire