1

the code that i have written is for an app that is similar to a pseudo ATM, where if the user inputs the specified pin they get access to the account details. This is very early stages but the main issue i am running into is that when the correct pin is put in and the user is given prompt for what they want to do next through the Please select what you want to do line if the user is to select for example Inquire Balance they are presented with the current balance being 10000.0. The java then loops back to the original loop meaning the user has to put in the pin again. What i want to do is for the user to be presented with the menu again inquiring what they want to do after they are shown the information they need.

This is the code i have written.

class app {
    public static void main(String[] args) {
        long pin = 2927942074l;
        double balance = 10000.0;
        int attempts = 3;

        System.out.println("Please enter your pin.");

        while (attempts > 0) {
            Scanner keyboardpin = new Scanner(System.in);

            long input = keyboardpin.nextLong();

            if (input == pin) {
                System.out.println("Correct");
                System.out.println("Welcome to your ATM");
                int a = 1;
                int b = 2;
                int c = 3;
                int d = 0;
                System.out.println(a + " - Inquire Balance");
                System.out.println(b + " - Withdraw");
                System.out.println(c + " - Deposit");
                System.out.println(d + " - Quit");
                System.out.println("Please select what you want to do.");
                Scanner menuselect = new Scanner(System.in);
                int menuinput = menuselect.nextInt();
                if (menuinput == a) {
                    System.out.println(balance);
                    continue;
                }
                if (menuinput == b) {
                    System.out.println("Please enter a withdrawal amount.");
                    Scanner withdrawamount = new Scanner(System.in);
                    float withdrawbalace = withdrawamount.nextFloat();
                    double balancex = (balance - withdrawbalace);
                    System.out.println("Youre new balance is " + balancex);
                    if (balancex < balance)
                        System.out.println("Insufficient balance");
                }
            } else {
                System.out.println("Wrong");
                attempts--;
                System.out.println("You have " + attempts + " attempts remaining.");
            }
            if (attempts == 0) {
                System.out.println("Maximum number of attempts exceeded");
            }
        }
    }
}

I've tried messing around with the continue function but am not sure if i am using it right. All feedback is appreciated.

1
  • 2
    I strongly recommend to you to use the auto-format/auto-indentation function of your development IDE more often. The original code you posed was nearly unreadable because of improper indentation. Regarding your problem: Why not using two loops? First for entering the PIN (once the correct pin has been entered exit the loop via break) and second for the "main menu". Commented Aug 29, 2020 at 8:24

2 Answers 2

1

What I want to do is for the user to be presented with the menu again inquiring what they want to do after they are shown the information they need.

Just change your if (input == pin) block as follows:

       if (input == pin) {
            System.out.println("Correct");
            System.out.println("Welcome to your ATM");
            while(true){ //keep printing your options unless "Quit" is chosen.
                int a = 1;
                int b = 2;
                int c = 3;
                int d = 0;
                System.out.println(a + " - Inquire Balance");
                System.out.println(b + " - Withdraw");
                System.out.println(c + " - Deposit");
                System.out.println(d + " - Quit");
                System.out.println("Please select what you want to do.");
                Scanner menuselect = new Scanner(System.in);
                int menuinput = menuselect.nextInt();
                if (menuinput == a) {
                    System.out.println(balance);
                    continue;
                }
                if (menuinput == b) {
                    System.out.println("Please enter a withdrawal amount.");
                    Scanner withdrawamount = new Scanner(System.in);
                    float withdrawbalace = withdrawamount.nextFloat();
                    double balancex = (balance - withdrawbalace);
                    System.out.println("Youre new balance is " + balancex);
                    if (balancex < balance)
                        System.out.println("Insufficient balance");
                }

                //You were missing "c", "d", and default (neither) implementations.
                //adding "d" for the exit functionality.

                if (menuinput == d) { 
                    break;
                }
            }
        } 
Sign up to request clarification or add additional context in comments.

Comments

0

I think this would be better suited for solving your problem. I've added an extra if condition for checking when input is not equal to pin

 while (attempts > 0) {
            long input = 0;
            if (input != pin) {
                Scanner keyboardpin = new Scanner(System.in);
                input = keyboardpin.nextLong();
            }
            if (input == pin) {
            System.out.println("Correct");
            System.out.println("Welcome to your ATM");
            int a = 1;
            int b = 2;
            int c = 3;
            int d = 0;

Comments

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.