Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 03 / Topic 8 of 8

Putting it together: ATM menu with 3 PIN attempts

This is the part's capstone. It uses only what Parts 1–3 have covered so far: Scanner, if/else, a classic switch, a while loop and break/continue — no arrays beyond the preview in Section 6, no collections, no methods beyond main (Part 5), and no try/catch (Part 6, which hardens this exact program against bad input like typing letters where a number is expected).

The PIN phase reuses the retry loop from Section 4, exactly:

final String correctPin = "4821";
int attemptsLeft = 3;
boolean unlocked = false;

System.out.println("=== Welcome to Pune Sahakari ATM ===");
while (attemptsLeft > 0 && !unlocked) {
    System.out.print("Enter 4-digit PIN: ");
    String typed = scanner.nextLine();
    if (typed.equals(correctPin)) {
        unlocked = true;
    } else {
        attemptsLeft--;
        if (attemptsLeft > 0) {
            System.out.println("Wrong PIN. Attempts left: " + attemptsLeft);
        } else {
            System.out.println("Wrong PIN. Card blocked.");
        }
    }
}

if (!unlocked) {
    return;
}

If all 3 attempts are wrong, the program stops right there — the menu is never shown. Real transcript (typed 0000, 1111, 2222):

=== Welcome to Pune Sahakari ATM ===
Enter 4-digit PIN: Wrong PIN. Attempts left: 2
Enter 4-digit PIN: Wrong PIN. Attempts left: 1
Enter 4-digit PIN: Wrong PIN. Card blocked.

Once unlocked, a menu loop takes over. It uses a classic switch, not an arrow expression, because each case does something (prints, asks for more input, changes balance) instead of only computing a value — exactly the choice the table in Section 3 points to. Exiting uses the running flag from the warning in Section 7, not a bare break:

System.out.println("PIN correct. Welcome, Asha.");
double balance = 15000.0;
boolean running = true;

while (running) {
    System.out.println();
    System.out.println("1. Check balance");
    System.out.println("2. Deposit");
    System.out.println("3. Withdraw");
    System.out.println("4. Exit");
    System.out.print("Choose an option: ");
    int choice = Integer.parseInt(scanner.nextLine());

    switch (choice) {
        case 1:
            System.out.println("Balance: Rs " + balance);
            break;

        case 2:
            System.out.print("Deposit amount: Rs ");
            double depositAmount = Double.parseDouble(scanner.nextLine());
            if (depositAmount <= 0) {
                System.out.println("Deposit must be positive. Ignored.");
            } else {
                balance += depositAmount;
                System.out.println("Deposited Rs " + depositAmount + ". New balance: Rs " + balance);
            }
            break;

        case 3:
            System.out.print("Withdraw amount: Rs ");
            double withdrawAmount = Double.parseDouble(scanner.nextLine());
            if (withdrawAmount <= 0) {
                System.out.println("Withdrawal must be positive. Ignored.");
            } else if (withdrawAmount > balance) {
                System.out.println("Insufficient balance. Current balance: Rs " + balance);
            } else {
                balance -= withdrawAmount;
                System.out.println("Withdrew Rs " + withdrawAmount + ". New balance: Rs " + balance);
            }
            break;

        case 4:
            System.out.println("Thank you for banking with us. Goodbye.");
            running = false; // break here would only exit the switch, not this while
            break;

        default:
            System.out.println("Invalid option, try again.");
    }
}

Real transcript (typed 0000, 1111, 4821, then 1, 2 → 5000, 3 → 3000, 3 → 50000, 4):

=== Welcome to Pune Sahakari ATM ===
Enter 4-digit PIN: Wrong PIN. Attempts left: 2
Enter 4-digit PIN: Wrong PIN. Attempts left: 1
Enter 4-digit PIN: PIN correct. Welcome, Asha.

1. Check balance
2. Deposit
3. Withdraw
4. Exit
Choose an option: Balance: Rs 15000.0

1. Check balance
2. Deposit
3. Withdraw
4. Exit
Choose an option: Deposit amount: Rs Deposited Rs 5000.0. New balance: Rs 20000.0

1. Check balance
2. Deposit
3. Withdraw
4. Exit
Choose an option: Withdraw amount: Rs Withdrew Rs 3000.0. New balance: Rs 17000.0

1. Check balance
2. Deposit
3. Withdraw
4. Exit
Choose an option: Withdraw amount: Rs Insufficient balance. Current balance: Rs 17000.0

1. Check balance
2. Deposit
3. Withdraw
4. Exit
Choose an option: Thank you for banking with us. Goodbye.

Everything that made it into this program came from earlier sections: the retry while (Section 4), the classic switch and the rule for choosing it over an arrow expression (Sections 2–3), and the "use a flag, not break, to end a loop from inside a switch" warning (Section 7).

Full program: examples/07_atm_menu.java.

Done here? The practice set for Part 3 — including the solutions — is on the part page.

Search the guide