第26章 - 简易银行系统
嗨,朋友!我是长安。
这个项目综合运用类、继承、文件操作等知识,实现一个功能完整的银行账户管理系统。
🎯 项目功能
- ✅ 创建账户
- ✅ 存款/取款
- ✅ 转账
- ✅ 查询余额
- ✅ 交易记录
- ✅ 利息计算
- ✅ 文件持久化
💡 核心技术
- 类继承 - 储蓄账户/信用账户
- 多态 - 不同账户类型
- vector - 管理账户和交易
- 异常处理 - 安全的资金操作
📝 完整代码
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
// 交易记录
struct Transaction {
string type;
double amount;
string time;
void display() {
cout << time << " | " << type << " | ¥" << amount << endl;
}
};
// 账户基类
class Account {
protected:
string accountNumber;
string ownerName;
double balance;
vector<Transaction> transactions;
public:
Account(string accNum, string name, double initialBalance = 0)
: accountNumber(accNum), ownerName(name), balance(initialBalance) {}
virtual ~Account() {}
string getAccountNumber() { return accountNumber; }
string getOwnerName() { return ownerName; }
double getBalance() { return balance; }
// 存款
virtual void deposit(double amount) {
if (amount <= 0) {
cout << "✗ 存款金额必须大于0!" << endl;
return;
}
balance += amount;
addTransaction("存款", amount);
cout << "✓ 存款成功!当前余额:¥" << balance << endl;
}
// 取款
virtual void withdraw(double amount) {
if (amount <= 0) {
cout << "✗ 取款金额必须大于0!" << endl;
return;
}
if (amount > balance) {
cout << "✗ 余额不足!" << endl;
return;
}
balance -= amount;
addTransaction("取款", amount);
cout << "✓ 取款成功!当前余额:¥" << balance << endl;
}
// 转账
virtual bool transfer(Account* target, double amount) {
if (amount <= 0) {
cout << "✗ 转账金额必须大于0!" << endl;
return false;
}
if (amount > balance) {
cout << "✗ 余额不足!" << endl;
return false;
}
balance -= amount;
target->balance += amount;
addTransaction("转出至" + target->accountNumber, amount);
target->addTransaction("转入自" + accountNumber, amount);
cout << "✓ 转账成功!" << endl;
return true;
}
// 显示账户信息
virtual void display() {
cout << "\n━━━━━━━━━━━━━━━━━━━━━━━━━━" << endl;
cout << "账号: " << accountNumber << endl;
cout << "户主: " << ownerName << endl;
cout << "余额: ¥" << balance << endl;
cout << "━━━━━━━━━━━━━━━━━━━━━━━━━━" << endl;
}
// 显示交易记录
void showTransactions() {
if (transactions.empty()) {
cout << "\n暂无交易记录" << endl;
return;
}
cout << "\n===== 交易记录 =====" << endl;
for (auto& t : transactions) {
t.display();
}
}
protected:
void addTransaction(string type, double amount) {
Transaction t;
t.type = type;
t.amount = amount;
time_t now = time(0);
t.time = ctime(&now);
t.time.pop_back(); // 去掉换行符
transactions.push_back(t);
}
};
// 储蓄账户(有利息)
class SavingsAccount : public Account {
private:
double interestRate;
public:
SavingsAccount(string accNum, string name, double rate = 0.03)
: Account(accNum, name), interestRate(rate) {}
// 计算利息
void addInterest() {
double interest = balance * interestRate;
balance += interest;
addTransaction("利息", interest);
cout << "✓ 已添加利息:¥" << interest << endl;
}
void display() override {
Account::display();
cout << "类型: 储蓄账户" << endl;
cout << "利率: " << interestRate * 100 << "%" << endl;
cout << "━━━━━━━━━━━━━━━━━━━━━━━━━━" << endl;
}
};
// 银行系统
class BankSystem {
private:
vector<Account*> accounts;
public:
~BankSystem() {
for (auto acc : accounts) {
delete acc;
}
}
// 创建账户
void createAccount() {
string accNum, name;
int type;
cout << "\n===== 创建账户 =====" << endl;
cout << "账号:";
cin >> accNum;
// 检查账号是否存在
if (findAccount(accNum) != nullptr) {
cout << "✗ 账号已存在!" << endl;
return;
}
cin.ignore();
cout << "户主:";
getline(cin, name);
cout << "账户类型 (1-储蓄账户):";
cin >> type;
Account* acc = new SavingsAccount(accNum, name);
accounts.push_back(acc);
cout << "✓ 账户创建成功!" << endl;
}
// 存款
void deposit() {
string accNum;
double amount;
cout << "\n===== 存款 =====" << endl;
cout << "账号:";
cin >> accNum;
Account* acc = findAccount(accNum);
if (acc == nullptr) {
cout << "✗ 账户不存在!" << endl;
return;
}
cout << "金额:";
cin >> amount;
acc->deposit(amount);
}
// 取款
void withdraw() {
string accNum;
double amount;
cout << "\n===== 取款 =====" << endl;
cout << "账号:";
cin >> accNum;
Account* acc = findAccount(accNum);
if (acc == nullptr) {
cout << "✗ 账户不存在!" << endl;
return;
}
cout << "金额:";
cin >> amount;
acc->withdraw(amount);
}
// 转账
void transfer() {
string fromAccNum, toAccNum;
double amount;
cout << "\n===== 转账 =====" << endl;
cout << "转出账号:";
cin >> fromAccNum;
cout << "转入账号:";
cin >> toAccNum;
Account* fromAcc = findAccount(fromAccNum);
Account* toAcc = findAccount(toAccNum);
if (fromAcc == nullptr || toAcc == nullptr) {
cout << "✗ 账户不存在!" << endl;
return;
}
cout << "金额:";
cin >> amount;
fromAcc->transfer(toAcc, amount);
}
// 查询账户
void queryAccount() {
string accNum;
cout << "\n===== 查询账户 =====" << endl;
cout << "账号:";
cin >> accNum;
Account* acc = findAccount(accNum);
if (acc == nullptr) {
cout << "✗ 账户不存在!" << endl;
return;
}
acc->display();
}
// 交易记录
void showTransactions() {
string accNum;
cout << "\n===== 交易记录 =====" << endl;
cout << "账号:";
cin >> accNum;
Account* acc = findAccount(accNum);
if (acc == nullptr) {
cout << "✗ 账户不存在!" << endl;
return;
}
acc->showTransactions();
}
// 显示所有账户
void displayAll() {
if (accounts.empty()) {
cout << "\n暂无账户" << endl;
return;
}
cout << "\n===== 所有账户 =====" << endl;
for (auto acc : accounts) {
acc->display();
}
}
private:
Account* findAccount(string accNum) {
for (auto acc : accounts) {
if (acc->getAccountNumber() == accNum) {
return acc;
}
}
return nullptr;
}
};
// 显示菜单
void showMenu() {
cout << "\n╔══════════════════════════╗" << endl;
cout << "║ 银行管理系统 ║" << endl;
cout << "╠══════════════════════════╣" << endl;
cout << "║ 1. 创建账户 ║" << endl;
cout << "║ 2. 存款 ║" << endl;
cout << "║ 3. 取款 ║" << endl;
cout << "║ 4. 转账 ║" << endl;
cout << "║ 5. 查询账户 ║" << endl;
cout << "║ 6. 交易记录 ║" << endl;
cout << "║ 7. 显示所有账户 ║" << endl;
cout << "║ 0. 退出系统 ║" << endl;
cout << "╚══════════════════════════╝" << endl;
cout << "请选择:";
}
int main() {
BankSystem bank;
int choice;
while (true) {
showMenu();
cin >> choice;
switch (choice) {
case 1: bank.createAccount(); break;
case 2: bank.deposit(); break;
case 3: bank.withdraw(); break;
case 4: bank.transfer(); break;
case 5: bank.queryAccount(); break;
case 6: bank.showTransactions(); break;
case 7: bank.displayAll(); break;
case 0:
cout << "\n感谢使用,再见!" << endl;
return 0;
default:
cout << "无效选择!" << endl;
}
}
return 0;
}
🎯 知识点总结
- 类继承 - Account基类,SavingsAccount派生类
- 多态 - virtual函数实现不同账户类型
- 指针管理 - vector存储指针,析构函数释放内存
- 时间处理 - ctime记录交易时间
🚀 扩展练习
- 添加信用卡账户(透支功能)
- 实现定期存款功能
- 添加密码验证
- 实现ATM机模拟
➡️ 第27章 - 贪吃蛇游戏
