Introduction
An ATM enforces many small rules: three PIN attempts, withdrawals in multiples of 100, a daily limit and a minimum balance. Implementing them teaches careful condition ordering and clear error messages.
Problem statement
Objectives
- Enforce business rules with ordered conditions
- Store recent transactions in a circular buffer
- Use structs and compound literals
- Split behaviour into small functions
Features
- PIN check with 3 attempts
- Deposit and withdrawal with all validation rules
- Mini statement of the last 5 transactions, newest first
- PIN change with confirmation and 4-digit check
Concepts demonstrated
- Structs
- Circular buffer
- Validation rules
- Static globals
System architecture
Account state is kept in file-scope static variables. Each menu option is a function. Transactions are stored in a fixed array of 5 Txn structs used as a circular buffer: history_next is where the next record goes and wraps with % HISTORY; the statement walks backwards from the newest entry.
Folder structure
Complete source code
Makefile
What each file does
- src/main.c
- Account state,
verify_pin,deposit,withdraw(rule checks in order),change_pin, the circular-bufferrecord/statement, and the menu loop. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
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 1234 1 2 2500 3 750 3 1000 3 7000 4 5 4321 4321 0
Testing
- Compare with
tests/expected-output.txt: one wrong PIN, a deposit, an invalid amount (750), a valid withdrawal, a withdrawal blocked by the minimum balance, the statement and a PIN change. - Enter three wrong PINs and confirm the card is blocked.
- Withdraw repeatedly to hit the Rs 20,000 daily limit.
Common errors
Statement shows transactions in the wrong order
Walk backwards: index = (history_next − 1 − i + HISTORY) % HISTORY.
Amounts like 150.5 pass the multiple-of-100 check
Also require that the amount equals its integer part before using % 100.
Possible improvements
- Persist the account to a file so the balance survives restarts
- Support multiple accounts with card numbers
- Add a fund transfer option
Additional challenges
- 1Store the PIN as a hash instead of plain text.
- 2Reset the daily limit when the date changes (use <time.h>).
- 3Add a denomination breakdown (how many 500/200/100 notes are dispensed).
Built and tested it yourself? Mark it to update your dashboard.