Introduction
An ATM is a small state machine full of rules: three PIN attempts, withdrawals in multiples of 100, a daily limit, dispensing the fewest notes. Simulating one is an excellent exercise in menus, validation and keeping state in an object.
Problem statement
Objectives
- Drive a program with a menu loop and switch
- Keep account state in a class
- Use ArrayDeque as a bounded history
- Implement a greedy note-selection algorithm
Features
- Three PIN attempts and card blocking
- Withdrawal rules: multiples of 100, balance, daily limit
- Greedy note breakdown (500/200/100)
- Validated deposits and 4-digit PIN change
- Mini statement of the last five transactions
Concepts demonstrated
- Menu-driven programs
- Encapsulated state
- Validation
- Greedy algorithms
- Deque as a bounded list
System architecture
A nested Account class holds the holder's name, PIN, balance, amount withdrawn today and a Deque<String> history. record adds an entry at the front and drops the oldest when there are more than five. main handles PIN entry, then loops over the menu with an arrow-form switch that calls withdraw, deposit or changePin. readAmount centralises parsing and validation of amounts. notesFor divides the amount by each note value in turn — the greedy method, which is optimal for these denominations.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- The
Accountclass, PIN check, menu loop, withdrawal/deposit/PIN-change logic and the note breakdown.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
1111 4321 1 2 2550 2 4700 3 abc 3 1500 2 30000 2 9000 5 4321 98 5 4321 2468 4 7 0
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - The first PIN is wrong and the second is right, so two attempts are reported.
- Withdrawing 2550 is rejected (not a multiple of 100); 4700 is paid as 9 × 500 and 1 × 200.
- After 4700, a request for 30,000 exceeds the ₹25,000 daily limit, and 9000 succeeds.
- The mini statement lists the four transactions, newest first.
Common errors
The menu reads an empty choice right after a number
Read every input with nextLine() and parse it, so no leftover newline remains in the buffer.
Mini statement keeps growing
Remove the oldest entry (removeLast) whenever the deque holds more than five.
Possible improvements
- Support several accounts selected by card number
- Reset the daily limit at midnight using java.time
- Keep a fixed stock of notes in the machine and refuse amounts it can't pay
Additional challenges
- 1Pay withdrawals with at most 10 notes, preferring 200s when 500s run out.
- 2Add a fund-transfer option between two accounts.
- 3Lock the card for 24 hours instead of permanently.
Built and tested it yourself? Mark it to update your dashboard.