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

Part 04 / Topic 8 of 8

Putting it together: an ATM menu with 3 PIN attempts

Every tool from this part, in one program: a PIN check with exactly 3 tries (for + break), then a menu loop (while (true) + when + break) that keeps running until the user exits.

fun main() {
    val correctPin = "2468"
    val maxAttempts = 3
    var balance = 15000.0
    var authenticated = false

    for (attempt in 1..maxAttempts) {
        print("Enter 4-digit PIN (attempt $attempt of $maxAttempts): ")
        val entered = readlnOrNull()?.trim()
        if (entered == correctPin) {
            authenticated = true
            println("PIN accepted.")
            break
        } else {
            println("Incorrect PIN.")
        }
    }

    if (!authenticated) {
        println("Too many incorrect attempts. Card blocked.")
        return
    }

    while (true) {
        println()
        println("1. Check balance")
        println("2. Withdraw")
        println("3. Deposit")
        println("4. Exit")
        print("Choose an option: ")
        val choice = readlnOrNull()?.trim()

        when (choice) {
            "1" -> println("Balance: ₹%.2f".format(balance))
            "2" -> {
                print("Withdraw amount: ")
                val amount = readlnOrNull()?.trim()?.toDoubleOrNull()
                when {
                    amount == null -> println("Enter a valid number.")
                    amount <= 0 -> println("Amount must be positive.")
                    amount > balance -> println("Insufficient balance.")
                    else -> {
                        balance -= amount
                        println("Withdrawn ₹%.2f. New balance: ₹%.2f".format(amount, balance))
                    }
                }
            }
            "3" -> {
                print("Deposit amount: ")
                val amount = readlnOrNull()?.trim()?.toDoubleOrNull()
                if (amount == null || amount <= 0) {
                    println("Enter a valid positive number.")
                } else {
                    balance += amount
                    println("Deposited ₹%.2f. New balance: ₹%.2f".format(amount, balance))
                }
            }
            "4" -> {
                println("Thank you for banking with us.")
                break
            }
            else -> println("Invalid option, choose 1-4.")
        }
    }
}

A few design choices worth noticing:

  • The PIN loop uses a plain break the moment the right PIN is entered — no label needed, since it's the only loop around it. authenticated stays false if all 3 attempts fail, and return exits main() entirely before the menu ever starts.
  • The menu's "4" -> { ... ; break } also uses a plain break — it sits directly inside the while loop's own body, so "nearest enclosing loop" already means exactly this loop. A label here would add nothing; section 7's label is only for when break/continue must skip past a nearer loop.
  • readlnOrNull() (not the deprecated readLine()) returns String?: it's null if the input stream ends, which ?.trim() and the == correctPin / when comparisons all handle safely without !! anywhere, the Part 3 habit carried straight into this part.

readlnOrNull() reads one line from standard input — type at the keyboard when running it yourself. To get a reproducible transcript for this lesson, every run below instead pipes the whole conversation in through printf, one line per answer, so the exact same input runs the exact same way every time:

$ kotlinc 08_atm_menu.kt -include-runtime -d atm.jar
$ printf '0000\n2468\n1\n2\n2500\n4\n' | java -jar atm.jar
Enter 4-digit PIN (attempt 1 of 3): Incorrect PIN.
Enter 4-digit PIN (attempt 2 of 3): PIN accepted.

1. Check balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: Balance: ₹15000.00

1. Check balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: Withdraw amount: Withdrawn ₹2500.00. New balance: ₹12500.00

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

A second run proves the lockout actually locks:

$ printf '1111\n2222\n3333\n' | java -jar atm.jar
Enter 4-digit PIN (attempt 1 of 3): Incorrect PIN.
Enter 4-digit PIN (attempt 2 of 3): Incorrect PIN.
Enter 4-digit PIN (attempt 3 of 3): Incorrect PIN.
Too many incorrect attempts. Card blocked.

And a third exercises the remaining branches — a deposit, an invalid menu choice, and a withdrawal that exceeds the balance:

$ printf '2468\n3\n5000\n9\n2\n999999\n4\n' | java -jar atm.jar
Enter 4-digit PIN (attempt 1 of 3): PIN accepted.

1. Check balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: Deposit amount: Deposited ₹5000.00. New balance: ₹20000.00

1. Check balance
2. Withdraw
3. Deposit
4. Exit
Choose an option: Invalid option, choose 1-4.

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

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

Full program: examples/08_atm_menu.kt.

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

Search the guide