C++简易银行账户管理系统,面向对象练习

## 一、项目介绍

这是一个用C++面向对象思想编写的简易银行账户管理系统,专门给刚学完C++类和对象的同学练手用的。只需要一个cpp文件,就能实现完整的银行账户管理功能。

**功能特点:**
– 纯C++标准库实现,无需第三方依赖
– 面向对象设计,封装性好
– 支持开户(创建账户)
– 支持存款
– 支持取款
– 支持查询余额
– 支持账户间转账
– 支持查看所有账户
– 控制台界面,操作简单直观

**适合人群:**
– 刚学完C++类和对象的新手
– 想练习面向对象编程的同学
– 需要做课程设计的学生
– 想了解封装、继承、多态的初学者

二、效果展示

cpp_bank_screenshot1

cpp_bank_screenshot2

/*
 * C++简易银行账户管理系统
 * 功能:面向对象练习,存款取款查询等
 * 用类和对象实现
 */
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <ctime>
using namespace std;

// 交易记录结构体
struct Transaction {
    string type;      // 交易类型:存款、取款、转账
    double amount;    // 金额
    double balance;   // 交易后余额
    string time;      // 交易时间
    string description; // 备注
};

// 银行账户类
class BankAccount {
private:
    string accountNumber;  // 账号
    string ownerName;      // 户主姓名
    double balance;        // 账户余额
    string password;       // 账户密码
    vector<Transaction> transactions; // 交易记录
    
    // 获取当前时间字符串
    string getCurrentTime() {
        time_t now = time(0);
        char* dt = ctime(&now);
        string timeStr(dt);
        // 去掉换行符
        if (!timeStr.empty() && timeStr.back() == '\n') {
            timeStr.pop_back();
        }
        return timeStr;
    }
    
    // 添加交易记录
    void addTransaction(string type, double amount, string description = "") {
        Transaction t;
        t.type = type;
        t.amount = amount;
        t.balance = balance;
        t.time = getCurrentTime();
        t.description = description;
        transactions.push_back(t);
    }
    
public:
    // 构造函数
    BankAccount(string accNum, string name, double initialBalance, string pwd) 
        : accountNumber(accNum), ownerName(name), balance(initialBalance), password(pwd) {
        // 添加开户记录
        addTransaction("开户", initialBalance, "账户开立");
    }
    
    // 验证密码
    bool verifyPassword(string pwd) {
        return password == pwd;
    }
    
    // 存款
    bool deposit(double amount) {
        if (amount <= 0) {
            cout << "存款金额必须大于0!" << endl;
            return false;
        }
        
        balance += amount;
        addTransaction("存款", amount);
        cout << "存款成功!存入 " << fixed << setprecision(2) << amount << " 元" << endl;
        return true;
    }
    
    // 取款
    bool withdraw(double amount) {
        if (amount <= 0) {
            cout << "取款金额必须大于0!" << endl;
            return false;
        }
        
        if (amount > balance) {
            cout << "余额不足,取款失败!" << endl;
            return false;
        }
        
        balance -= amount;
        addTransaction("取款", amount);
        cout << "取款成功!取出 " << fixed << setprecision(2) << amount << " 元" << endl;
        return true;
    }
    
    // 转账
    bool transfer(BankAccount& toAccount, double amount) {
        if (amount <= 0) {
            cout << "转账金额必须大于0!" << endl;
            return false;
        }
        
        if (amount > balance) {
            cout << "余额不足,转账失败!" << endl;
            return false;
        }
        
        // 从本账户扣除
        balance -= amount;
        addTransaction("转账支出", amount, "转给 " + toAccount.getOwnerName());
        
        // 转入对方账户
        toAccount.balance += amount;
        toAccount.addTransaction("转账收入", amount, "来自 " + ownerName);
        
        cout << "转账成功!转出 " << fixed << setprecision(2) << amount << " 元给 " << toAccount.getOwnerName() << endl;
        return true;
    }
    
    // 查询余额
    void checkBalance() {
        cout << "\n账户余额:" << fixed << setprecision(2) << balance << " 元" << endl;
    }
    
    // 显示账户信息
    void showAccountInfo() {
        cout << "\n========== 账户信息 ==========" << endl;
        cout << "账号:" << accountNumber << endl;
        cout << "户主:" << ownerName << endl;
        cout << "余额:" << fixed << setprecision(2) << balance << " 元" << endl;
        cout << "交易记录:" << transactions.size() << " 条" << endl;
        cout << "================================" << endl;
    }
    
    // 显示交易记录
    void showTransactions() {
        if (transactions.empty()) {
            cout << "暂无交易记录!" << endl;
            return;
        }
        
        cout << "\n========== 交易记录 ==========" << endl;
        cout << left << setw(12) << "交易类型" 
             << setw(12) << "金额" 
             << setw(12) << "余额" 
             << setw(25) << "时间" << endl;
        cout << string(60, '-') << endl;
        
        for (const auto& t : transactions) {
            cout << left << setw(12) << t.type 
                 << setw(12) << (fixed << setprecision(2) << t.amount)
                 << setw(12) << (fixed << setprecision(2) << t.balance)
                 << setw(25) << t.time << endl;
        }
        cout << "================================" << endl;
    }
    
    // 修改密码
    bool changePassword(string oldPwd, string newPwd) {
        if (!verifyPassword(oldPwd)) {
            cout << "原密码错误,修改失败!" << endl;
            return false;
        }
        
        if (newPwd.length() < 4) {
            cout << "新密码长度不能少于4位!" << endl;
            return false;
        }
        
        password = newPwd;
        cout << "密码修改成功!" << endl;
        return true;
    }
    
    // Getter方法
    string getAccountNumber() { return accountNumber; }
    string getOwnerName() { return ownerName; }
    double getBalance() { return balance; }
};

// 银行系统类
class BankSystem {
private:
    vector<BankAccount*> accounts;  // 账户列表
    BankAccount* currentAccount;    // 当前登录的账户
    
public:
    // 构造函数
    BankSystem() : currentAccount(nullptr) {
        // 初始化一些示例账户
        initSampleAccounts();
    }
    
    // 析构函数
    ~BankSystem() {
        for (auto acc : accounts) {
            delete acc;
        }
    }
    
    // 初始化示例账户
    void initSampleAccounts() {
        accounts.push_back(new BankAccount("6222021234567890001", "张三", 5000.0, "123456"));
        accounts.push_back(new BankAccount("6222021234567890002", "李四", 3200.5, "123456"));
        accounts.push_back(new BankAccount("6222021234567890003", "王五", 10000.0, "123456"));
    }
    
    // 开户
    void openAccount() {
        cout << "\n--- 开户 ---" << endl;
        string name, pwd;
        double initialDeposit;
        
        cout << "请输入您的姓名:";
        cin >> name;
        cout << "请设置密码(至少4位):";
        cin >> pwd;
        cout << "请输入初始存款金额:";
        cin >> initialDeposit;
        
        if (pwd.length() < 4) {
            cout << "密码长度不足4位,开户失败!" << endl;
            return;
        }
        
        if (initialDeposit < 0) {
            cout << "初始存款不能为负数,开户失败!" << endl;
            return;
        }
        
        // 生成账号(简单模拟)
        string accNum = "622202" + to_string(1234567890000 + accounts.size() + 1);
        
        BankAccount* newAcc = new BankAccount(accNum, name, initialDeposit, pwd);
        accounts.push_back(newAcc);
        
        cout << "\n开户成功!" << endl;
        cout << "您的账号是:" << accNum << endl;
        cout << "请牢记您的密码!" << endl;
    }
    
    // 登录
    bool login() {
        cout << "\n--- 登录 ---" << endl;
        string accNum, pwd;
        
        cout << "请输入账号:";
        cin >> accNum;
        cout << "请输入密码:";
        cin >> pwd;
        
        for (auto acc : accounts) {
            if (acc->getAccountNumber() == accNum) {
                if (acc->verifyPassword(pwd)) {
                    currentAccount = acc;
                    cout << "登录成功!欢迎您," << acc->getOwnerName() << endl;
                    return true;
                } else {
                    cout << "密码错误,登录失败!" << endl;
                    return false;
                }
            }
        }
        
        cout << "账号不存在,登录失败!" << endl;
        return false;
    }
    
    // 登出
    void logout() {
        currentAccount = nullptr;
        cout << "已退出登录!" << endl;
    }
    
    // 显示主菜单(登录后)
    void showAccountMenu() {
        cout << "\n" << string(40, '=') << endl;
        cout << "      银行账户管理系统" << endl;
        cout << "      当前用户:" << currentAccount->getOwnerName() << endl;
        cout << string(40, '=') << 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 << string(40, '=') << endl;
        cout << "请输入您的选择:";
    }
    
    // 处理账户操作
    void handleAccountOperations() {
        int choice;
        while (true) {
            showAccountMenu();
            cin >> choice;
            
            switch (choice) {
                case 1: {
                    // 存款
                    double amount;
                    cout << "\n请输入存款金额:";
                    cin >> amount;
                    currentAccount->deposit(amount);
                    break;
                }
                
                case 2: {
                    // 取款
                    double amount;
                    cout << "\n请输入取款金额:";
                    cin >> amount;
                    currentAccount->withdraw(amount);
                    break;
                }
                
                case 3: {
                    // 转账
                    cout << "\n--- 转账 ---" << endl;
                    string toAccNum;
                    double amount;
                    
                    cout << "请输入对方账号:";
                    cin >> toAccNum;
                    cout << "请输入转账金额:";
                    cin >> amount;
                    
                    // 查找对方账户
                    BankAccount* toAcc = nullptr;
                    for (auto acc : accounts) {
                        if (acc->getAccountNumber() == toAccNum) {
                            toAcc = acc;
                            break;
                        }
                    }
                    
                    if (toAcc == nullptr) {
                        cout << "对方账号不存在,转账失败!" << endl;
                    } else if (toAcc == currentAccount) {
                        cout << "不能给自己转账!" << endl;
                    } else {
                        currentAccount->transfer(*toAcc, amount);
                    }
                    break;
                }
                
                case 4: {
                    // 查询余额
                    currentAccount->checkBalance();
                    break;
                }
                
                case 5: {
                    // 查看账户信息
                    currentAccount->showAccountInfo();
                    break;
                }
                
                case 6: {
                    // 查看交易记录
                    currentAccount->showTransactions();
                    break;
                }
                
                case 7: {
                    // 修改密码
                    string oldPwd, newPwd;
                    cout << "\n请输入原密码:";
                    cin >> oldPwd;
                    cout << "请输入新密码:";
                    cin >> newPwd;
                    currentAccount->changePassword(oldPwd, newPwd);
                    break;
                }
                
                case 0: {
                    logout();
                    return;
                }
                
                default:
                    cout << "输入有误,请重新选择!" << endl;
            }
        }
    }
    
    // 显示欢迎菜单
    void showWelcomeMenu() {
        cout << "\n" << string(40, '=') << endl;
        cout << "   欢迎使用简易银行账户管理系统" << endl;
        cout << string(40, '=') << endl;
        cout << "  1. 登录账户" << endl;
        cout << "  2. 开立新账户" << endl;
        cout << "  0. 退出系统" << endl;
        cout << string(40, '=') << endl;
        cout << "请输入您的选择:";
    }
    
    // 运行系统
    void run() {
        int choice;
        while (true) {
            showWelcomeMenu();
            cin >> choice;
            
            switch (choice) {
                case 1:
                    if (login()) {
                        handleAccountOperations();
                    }
                    break;
                    
                case 2:
                    openAccount();
                    break;
                    
                case 0:
                    cout << "\n感谢使用银行账户管理系统,再见!" << endl;
                    return;
                    
                default:
                    cout << "输入有误,请重新选择!" << endl;
            }
        }
    }
};

int main() {
    BankSystem bank;
    bank.run();
    return 0;
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容