Introduction
Almost every programming course has a bank account example, but most of them store money in a double and quietly accumulate rounding errors. This version models an Account class with private state, keeps money in whole cents and parses amounts like 19.99 itself, so the balances are always exact.
Problem statement
Objectives
- Design a class whose data can only change through validated member functions
- Represent money safely with integer cents
- Parse commands with std::istringstream
- Keep a list of objects in std::vector and look them up by id
Features
- open, deposit, withdraw, transfer, show, history, list, help and quit commands
- Amounts accepted as 25, 25.5 or 25.50 and rejected if they have more than two decimals
- Withdrawals and transfers refuse to overdraw an account
- Per-account transaction history and a bank-wide total
Concepts demonstrated
- Encapsulation
- Classes and constructors
- std::vector
- String parsing
- Integer money representation
System architecture
Account owns an id, an owner name, a balance in cents and a history vector; deposit and withdraw are the only ways to change the balance, and withdraw returns false instead of going negative. Bank stores accounts in a std::vector<Account>, hands out ids starting at 1001 and finds accounts by id. main reads one line at a time, splits it with std::istringstream and dispatches on the first word. parse_amount converts text to cents digit by digit and money formats cents back as $12.34.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
parse_amountandmoney(cent conversion), theAccountandBankclasses, and the command loop inmain.- 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.
open Asha 500 open Ravi 120.5 deposit 1001 250.75 withdraw 1002 200 withdraw 1002 20.50 transfer 1001 1002 100 deposit 1001 abc show 1003 history 1001 list quit
Testing
- Run the scripted session in
tests/sample-input.txtand compare it withtests/expected-output.txt. - Try
withdraw 1001 999999: the balance must not change and an 'Insufficient funds' message must appear. - Try amounts such as
12.345,-5andabc; each must be rejected. - Transfer money between two accounts and check both histories.
Common errors
Balances like $0.30000000000000004
That is what happens with double. Keep money in integer cents and only format it as dollars when printing.
A pointer returned by Bank::find stops being valid
Opening a new account can make the vector reallocate. Don't keep Account* across an open call; look the account up again.
Possible improvements
- Save and load accounts from a file
- Add interest for savings accounts using an inheritance hierarchy
- Add a PIN per account and lock it after three wrong attempts
Additional challenges
- 1Store accounts in a std::map<int, Account> instead and compare the lookup code.
- 2Add an
undocommand that reverses the last transaction. - 3Print a monthly statement grouped by transaction type.
Built and tested it yourself? Mark it to update your dashboard.