Project: Bank Management System
What you'll build
A menu-driven bank system where users can open accounts, deposit, withdraw, transfer and print statements.
Features checklist
- Open account with owner name and opening deposit (minimum balance rule)
- Deposit / withdraw with validation
- Transfer between accounts: both sides change or neither does
- Transaction history per account with running balance
- Find account by number; list all accounts
- Savings accounts earn interest (inheritance +
virtual) - Menu loop reading commands from
std::cin
Class design
Transaction { type, amount, balance_after }
Account (base) — number, owner, balance (paisa), history; virtual monthly_update()
SavingsAccount : Account — adds interest
Bank — owns accounts: std::map<int, std::unique_ptr<Account>>
Concepts practised
Classes & encapsulation, constructors with validation, inheritance & virtual functions, std::unique_ptr in a std::map, exceptions for invalid operations, enum class, integer money (paisa), formatted output with std::format.
Rules that must always hold (invariants)
- Balance never below the minimum
- Every change records a transaction
- Money is never created or lost by a transfer
Extensions
PIN with limited attempts, save/load accounts to a file (Unit 16), monthly statements as CSV, loan accounts with EMI schedules, admin reports (total deposits, richest account).
Example
#include <format>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
enum class TxType { Open, Deposit, Withdraw, TransferIn, TransferOut, Interest };
std::string_view name(TxType t) {
switch (t) {
case TxType::Open: return "Open";
case TxType::Deposit: return "Deposit";
case TxType::Withdraw: return "Withdraw";
case TxType::TransferIn: return "Transfer in";
case TxType::TransferOut: return "Transfer out";
case TxType::Interest: return "Interest";
}
return "?";
}
std::string money(long long paisa) { return std::format("Rs. {}.{:02}", paisa / 100, paisa % 100); }
struct Transaction { TxType type; long long amount, balance_after; };
class Account {
public:
static constexpr long long MIN_BALANCE = 50000; // Rs. 500
Account(int number, std::string owner, long long opening) : number_(number), owner_(std::move(owner)) {
if (opening < MIN_BALANCE) throw std::invalid_argument("opening deposit below minimum balance");
apply(TxType::Open, opening);
}
virtual ~Account() = default;
void deposit(long long amount) { check_positive(amount); apply(TxType::Deposit, amount); }
void withdraw(long long amount) { check_positive(amount); check_funds(amount); apply(TxType::Withdraw, -amount); }
virtual void monthly_update() {}
virtual std::string kind() const { return "Current"; }
int number() const { return number_; }
const std::string& owner() const { return owner_; }
long long balance() const { return balance_; }
void print_statement() const {
std::cout << std::format("Statement #{} {} ({})\n", number_, owner_, kind());
for (const auto& t : history_)
std::cout << std::format(" {:<13}{:>14}{:>16}\n", name(t.type), money(t.amount), money(t.balance_after));
}
protected:
void apply(TxType type, long long delta) {
balance_ += delta;
history_.push_back({ type, delta < 0 ? -delta : delta, balance_ });
}
void check_funds(long long amount) const {
if (balance_ - amount < MIN_BALANCE) throw std::runtime_error("insufficient funds");
}
static void check_positive(long long amount) {
if (amount <= 0) throw std::invalid_argument("amount must be positive");
}
friend class Bank;
private:
int number_;
std::string owner_;
long long balance_ = 0;
std::vector<Transaction> history_;
};
class SavingsAccount : public Account {
public:
SavingsAccount(int number, std::string owner, long long opening, int basis_points)
: Account(number, std::move(owner), opening), rate_bp_(basis_points) {}
void monthly_update() override { apply(TxType::Interest, balance() * rate_bp_ / 10000); }
std::string kind() const override { return "Savings"; }
private:
int rate_bp_;
};
class Bank {
public:
Account& open(const std::string& owner, long long opening, bool savings) {
int no = next_number_++;
std::unique_ptr<Account> acc = savings ? std::make_unique<SavingsAccount>(no, owner, opening, 50)
: std::make_unique<Account>(no, owner, opening);
return *(accounts_[no] = std::move(acc));
}
Account& find(int number) {
auto it = accounts_.find(number);
if (it == accounts_.end()) throw std::out_of_range("no account #" + std::to_string(number));
return *it->second;
}
void transfer(int from, int to, long long amount) {
Account& a = find(from);
Account& b = find(to);
Account::check_positive(amount);
a.check_funds(amount); // validate everything before changing anything
a.apply(TxType::TransferOut, -amount);
b.apply(TxType::TransferIn, amount);
}
void month_end() { for (auto& [no, acc] : accounts_) acc->monthly_update(); }
long long total_deposits() const {
long long t = 0;
for (const auto& [no, acc] : accounts_) t += acc->balance();
return t;
}
private:
std::map<int, std::unique_ptr<Account>> accounts_;
int next_number_ = 1001;
};
int main() {
Bank bank;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream cmd(line);
std::string op;
cmd >> op;
try {
if (op == "open" || op == "save") {
std::string owner; double rupees;
cmd >> owner >> rupees;
Account& a = bank.open(owner, static_cast<long long>(rupees * 100), op == "save");
std::cout << std::format("> opened #{} for {} ({})\n", a.number(), a.owner(), a.kind());
} else if (op == "dep" || op == "wd") {
int no; double rupees;
cmd >> no >> rupees;
Account& a = bank.find(no);
long long p = static_cast<long long>(rupees * 100);
op == "dep" ? a.deposit(p) : a.withdraw(p);
std::cout << std::format("> #{} balance {}\n", no, money(a.balance()));
} else if (op == "tr") {
int from, to; double rupees;
cmd >> from >> to >> rupees;
bank.transfer(from, to, static_cast<long long>(rupees * 100));
std::cout << std::format("> transferred {} from #{} to #{}\n", money(static_cast<long long>(rupees * 100)), from, to);
} else if (op == "month") {
bank.month_end();
std::cout << "> month end processed\n";
} else if (op == "stmt") {
int no; cmd >> no;
bank.find(no).print_statement();
} else if (op == "total") {
std::cout << "> money held by the bank: " << money(bank.total_deposits()) << '\n';
}
} catch (const std::exception& e) {
std::cout << "> error: " << e.what() << '\n';
}
}
return 0;
}
/* Output:
> opened #1001 for Asha (Savings)
> opened #1002 for Ravi (Current)
> error: opening deposit below minimum balance
> #1002 balance Rs. 3500.00
> error: insufficient funds
> transferred Rs. 5000.00 from #1001 to #1002
> error: no account #9999
> month end processed
Statement #1001 Asha (Savings)
Open Rs. 20000.00 Rs. 20000.00
Transfer out Rs. 5000.00 Rs. 15000.00
Interest Rs. 75.00 Rs. 15075.00
Statement #1002 Ravi (Current)
Open Rs. 1000.00 Rs. 1000.00
Deposit Rs. 2500.00 Rs. 3500.00
Transfer in Rs. 5000.00 Rs. 8500.00
> money held by the bank: Rs. 23575.00
*/