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

Part 06 / Topic 8 of 8

Putting it together: safe PIN and amount input for an ATM

This part's build: a PIN and amount reader that never lets bad input crash the ATM, and gives up gracefully — not silently — after too many bad PIN attempts. It uses everything above:

  • try/catch for NumberFormatException (text that isn't a number at all)
  • custom unchecked exceptions for "right type, wrong value" (InvalidPinException, InvalidAtmAmountException, InsufficientFundsException)
  • a limited number of attempts for the PIN, like Part 3's ATM menu, ending in its own custom exception (TooManyPinAttemptsException) if all of them fail
  • finally to guarantee the session always prints "Session ended.", pass or fail
static String readPin(Scanner scanner) {
    for (int attempt = 1; attempt <= MAX_PIN_ATTEMPTS; attempt++) {
        System.out.print("Enter 4-digit PIN (attempt " + attempt + " of " + MAX_PIN_ATTEMPTS + "): ");
        String pinText = scanner.nextLine().trim();
        try {
            Integer.parseInt(pinText);                 // only checks "is this text a number?"
            if (pinText.length() != 4) {
                throw new InvalidPinException("PIN must be exactly 4 digits, got \"" + pinText + "\"");
            }
            return pinText;
        } catch (NumberFormatException e) {
            System.out.println("  Not a number: \"" + pinText + "\"");
        } catch (InvalidPinException e) {
            System.out.println("  " + e.getMessage());
        }
    }
    throw new TooManyPinAttemptsException("Too many incorrect PIN attempts. Card blocked.");
}

readAmount follows the same shape, but loops until the amount is valid instead of giving up, and checks it against the balance with InsufficientFundsException. main wraps both in one try, catches only TooManyPinAttemptsException (the one failure that should end the session), and uses finally to always print "Session ended." and close the Scanner.

Run 1 — everything correct first try:

$ printf '4821\n1500\n' | java SafeAtmInput
Enter 4-digit PIN (attempt 1 of 3): PIN accepted: 4821
Enter amount to withdraw: Dispensing ₹1500
Remaining balance: ₹3500
Session ended.

Run 2 — bad PIN attempts, then a good one; bad amounts, then a good one (balance is ₹5000):

$ printf 'ab12\n12\n4821\nxyz\n250\n9900\n1500\n' | java SafeAtmInput
Enter 4-digit PIN (attempt 1 of 3):   Not a number: "ab12"
Enter 4-digit PIN (attempt 2 of 3):   PIN must be exactly 4 digits, got "12"
Enter 4-digit PIN (attempt 3 of 3): PIN accepted: 4821
Enter amount to withdraw:   Not a number: "xyz"
Enter amount to withdraw:   Amount must be a multiple of 100, got 250
Enter amount to withdraw:   Amount 9900 exceeds balance 5000
Enter amount to withdraw: Dispensing ₹1500
Remaining balance: ₹3500
Session ended.

Each line fed to the program is typed right after its prompt in a real terminal; piping printf input here just lets this transcript be captured exactly as java printed it. Each error message echoes back exactly what was typed ("ab12", "12", 250, 9900), so you can see what caused each rejection.

Run 3 — three bad PINs in a row, card blocked:

$ printf 'ab12\n12\n0000a\n' | java SafeAtmInput
Enter 4-digit PIN (attempt 1 of 3):   Not a number: "ab12"
Enter 4-digit PIN (attempt 2 of 3):   PIN must be exactly 4 digits, got "12"
Enter 4-digit PIN (attempt 3 of 3):   Not a number: "0000a"
Too many incorrect PIN attempts. Card blocked.
Session ended.

readPin throws TooManyPinAttemptsException after the loop runs out, main's catch prints its message, and — because it's in finally — "Session ended." still prints even though the session failed.

Full program: examples/05_safe_atm_input.java.

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

Search the guide