Skip to content
C++ project #01Beginner

Bank Account Manager

Open accounts, deposit, withdraw and transfer money through a command prompt — with exact cent arithmetic and per-account history.

Estimated time
~3 hours to build
Files
5 files · 364 lines
Technologies
C++17, <iostream>, <sstream>, <vector>, <iomanip>

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

Build a command-driven program that manages several bank accounts. Users can open an account with an opening balance, deposit, withdraw (never below zero), transfer between accounts, view one account or all of them, and see a history of every transaction on an account. Invalid amounts, unknown accounts and malformed commands must be reported clearly without crashing.

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.

TextArchitecture diagram
stdin line -> istringstream -> command word
|
+------------+-----------+-----------+
v v v v
open deposit withdraw transfer
| \ | /
v \ v /
Bank::open() Account::deposit/withdraw()
| |
+------> std::vector<Account> <-----+

Folder structure

TextProject tree
bank-account-manager/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
parse_amount and money (cent conversion), the Account and Bank classes, and the command loop in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Bank Account Manager ===
Commands:
open <name> <amount> deposit <id> <amount>
withdraw <id> <amount> transfer <from> <to> <amount>
show <id> history <id> list help quit
> Opened account 1001 for Asha with $500.00
> Opened account 1002 for Ravi with $120.50
> New balance: $750.75
> Insufficient funds: balance is $120.50
> New balance: $100.00
> Moved $100.00 from 1001 to 1002
> Enter a positive amount like 25 or 19.99
> No account 1003
> 1. opened with $500.00
2. deposit $250.75
3. withdraw $100.00
4. transfer to 1002
> ID Owner Balance
1001 Asha $650.75
1002 Ravi $200.00
Total held: $850.75
>
Goodbye.
Input
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.txt and compare it with tests/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, -5 and abc; 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

  1. 1Store accounts in a std::map<int, Account> instead and compare the lookup code.
  2. 2Add an undo command that reverses the last transaction.
  3. 3Print a monthly statement grouped by transaction type.

Built and tested it yourself? Mark it to update your dashboard.