0

I'm trying to create a menu system where the user picks whether there an admin or a customer, and then they can enter there login details.

Once signed in the admin and the customer are presented with different menus. I also want to be able to log off and it will go back to the login menu where they are asked if there a admin or a customer.

This is what i have so far:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){

        login login = new login();
        Scanner in = new Scanner(System.in);

        String user;
        String pwd;


        int loginOption = 0;

        login.databaseConnection();

        System.out.println("======================================");
        System.out.println("|     Please select and option:      |");
        System.out.println("======================================");
        System.out.println("| Options:                           |");
        System.out.println("|        [1] Admin                   |");
        System.out.println("|        [2] Account Holder          |");
        System.out.println("|        [3] Exit                    |");
        System.out.println("======================================");



        do{
            System.out.print("Option:");
            loginOption = in.nextInt();

            // Switch construct
            switch (loginOption) {
                case 1:
                    System.out.println("");
                    System.out.println("Username: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");
                  break;
                case 2:
                    System.out.println("");
                    System.out.println("Account Number: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");

                    if(login.validate_login(user, pwd)){
                         System.out.println("Valid Login");
                         System.out.println("");
                         System.out.println("Hello, Account Holder");
                         System.out.println("");
                         System.out.println("======================================");
                         System.out.println("|     Please select and option:      |");
                         System.out.println("======================================");
                         System.out.println("| Options:                           |");
                         System.out.println("|        [1] Check balence           |");
                         System.out.println("|        [2] Withdraw                |");
                         System.out.println("|        [3] Logout                  |");
                         System.out.println("======================================");

                         int userOption = 0;

                         do{
                             System.out.println("Option");
                             userOption = in.nextInt();
                             switch (userOption){
                                case 1:
                                    break;
                                case 2:
                                    break;
                                case 3:
                                    loginOption = 3;
                                    break;
                             }
                         }while (loginOption != 3);
                    }else{
                         System.out.println("Invalid");
                    }
                  break;
                case 3:
                    System.out.println("Exit selected");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid selection");
                    break; // This break is not really necessary
            }
        }while (loginOption != 3);


        in.close();   
    }
}

The main problem I'm having at them moment is leaving the second switch and back to the first one.

Any help would be appreciated :)

2
  • Its correct in switch (loginOption) { either 1 for admin or 2 for cust or 3 for exit, ll be true. if you select 1 its going asks usarname and owd then again it ll going to main. Commented Jan 31, 2016 at 17:46
  • @Satya yerr, its just once they enter the correct login information a new menu is displayed, and i want there to be an option for them to be able to logout and and go back to where they select whether there and admin or a customer. At the moment the program just terminates Commented Jan 31, 2016 at 17:52

1 Answer 1

1

You can add a boolean variable ext like this:

                      boolean ext = true;
                      do{
                        ...
                            case 3:
                                loginOption = 3;
                                ext = false;
                                break;
                         }
                     }while (loginOption != 3 && ext);

With that you can exit from while loop when yuou have loginOption = 3;

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thcx been stuck on that for a while :'D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.