1

All my methods work fine when being called singely.

I have two menus one Main and for the Customer.

public void getMainMenuChoice() {
    Scanner kb =new Scanner(System.in);
    boolean check =  true;

    while(check) {
        Menu.mainMenu();

        int choice = kb.nextInt(); 

        switch(choice) {
            case 0:
                System.out.println("Goodbye");
                check = false;
                break;
            case 1:
                getCustomerMenuChoice();
            case 2:
                Menu.mainMenu();
        }
    }


}

//Employee Choice from the Menu 
public  void getCustomerMenuChoice() {
    Scanner kb =new Scanner(System.in); 
    Menu.customerMenu();

    int choice = kb.nextInt(); 

    switch(choice) {
        case 0:
            Menu.mainMenu();
        case 1:
            customerWithdraw();
        case 2:
            System.out.println("In progress");
        case 3:
            createCustomerAccount();
        case 4:
            System.out.println("Enter Customer Passport");
            String passportNumber = kb.nextLine();
            findCustomer(passportNumber);
            System.out.println("Waiting");

     }


    }

In main when I call the getMainMenuChoice() I can select the customer menu

When I am in this menu and for example call 4 findCustomer() It just reloads the the mainMenu and prints waiting at the top while prompting me to enter the passport number.

This is a run through of what happens

Enter:

  1. Customer Menu

  2. Employee Menu

  3. Quit Application.

Selection -> 1

Enter:

  1. Customer Withdraw

  2. Customer Deposit

  3. Create Account

  4. Delete Customer

  5. Find Customer

  6. Display All Customers

  7. Back

Selection -> 4

Enter Customer Passport

//It just ignores the method and prints the menu below as well Enter Customer Passport

Enter:

  1. Customer Menu

  2. Employee Menu

  3. Quit Application.

Selection ->

//Finding a customer
public Customer findCustomer(String passportNumber) {

    for(Customer currentCustomer : listCustomer)

        if(currentCustomer.getPassportNumber().equals(passportNumber)) {
            System.out.println("Customer Found: " + currentCustomer);
            return currentCustomer;
        }

        return null;
}

I am not sure why this happens I have been trying to debug for a few hours now.

Is it something to do with the while loop in the getMainMenuChoice()?

Liam
  • 568
  • 2
  • 12
  • 1
    When you run this with a debugger, what is the *first* thing it does incorrectly? – Scott Hunter Jan 28 '20 at 21:09
  • 3
    Switch cases will fall through without a ```break``` statement. That means it will execute the correct case but also all cases afterwards. – Michael Bianconi Jan 28 '20 at 21:10
  • @ScottHunter: I have updates with the full log of what happens. – Liam Jan 28 '20 at 21:31
  • @MichaelBiaconi: I have put a break after every case, but the same result occurs??? – Liam Jan 28 '20 at 21:32
  • I have discovered that when i call kb.next() it waits but the function is not being called? – Liam Jan 28 '20 at 21:35
  • Where do you call `kb.next()`? I see calls to `kb.nextInt()`, but don't those require that you type something? – Scott Hunter Jan 28 '20 at 21:42
  • You are using `scanner.nextInt()` and `scanner.nextLine()` together, which leads to problems - see https://stackoverflow.com/a/13102066/5646962 for an explanation. – Thomas Kläger Jan 28 '20 at 21:44
  • Yeah sorry I put that in just now, Ill update the getCustomerMenuChoic() now. This was just to see what was happening so it waits for me to push a key and then just loads the getMainMenuChoice(). It looks like it is just ignoring the function findCustomer()? – Liam Jan 28 '20 at 21:45
  • No, if you use `scanner.nextInt()` followed by `scanner.nextLine()` and type "4" followed by the enter key, then `scanner.nextInt()` will read the number "4" and `scanner.nextLine()` will read the remainder of that line - an empty string. – Thomas Kläger Jan 28 '20 at 21:49
  • You need to put in breaks after each block in the switch statements. – NomadMaker Jan 28 '20 at 21:51
  • @ Thomas Kläger: Thanks for the link perfect sorted it out it was to do with using the same scanner object twice – Liam Jan 28 '20 at 21:53

0 Answers0