请综合利用本课程中C++学习的面向对象编程相关技术,如继承、派生、多态、文件处理、容器等。实现一个银行账户管理系统。银行账户管理系统中包括用户及银行职员两类人员。用户及银行职员通过简易的文字菜单式系统界面登录后进行相应操作。
时间: 2024-03-18 11:44:45 浏览: 59
好的,我可以为您提供一份简单的银行账户管理系统的代码示例,该系统包括用户和银行职员两类人员,实现了基本的账户管理功能,包括开户、存款、取款、查询余额等操作。以下是示例代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 基类:账户
class Account {
public:
Account(string n, string p, double m) : name(n), password(p), money(m) {}
virtual ~Account() {}
virtual void show_info() const = 0; // 显示账户信息
virtual void deposit(double m) = 0; // 存款
virtual bool withdraw(double m) = 0; // 取款
virtual double get_balance() const = 0; // 查询余额
virtual bool validate(string p) const { return password == p; } // 验证密码是否正确
protected:
string name; // 账户名
string password; // 密码
double money; // 账户余额
};
// 派生类:用户账户
class UserAccount : public Account {
public:
UserAccount(string n, string p, double m) : Account(n, p, m) {}
~UserAccount() {}
void show_info() const override {
cout << "账户名:" << name << endl;
cout << "账户余额:" << money << endl;
}
void deposit(double m) override {
money += m;
cout << "存款成功,当前余额:" << money << endl;
}
bool withdraw(double m) override {
if (money < m) {
cout << "余额不足,取款失败!" << endl;
return false;
}
money -= m;
cout << "取款成功,当前余额:" << money << endl;
return true;
}
double get_balance() const override {
return money;
}
};
// 派生类:银行职员账户
class StaffAccount : public Account {
public:
StaffAccount(string n, string p, double m) : Account(n, p, m) {}
~StaffAccount() {}
void show_info() const override {
cout << "账户名:" << name << endl;
cout << "账户余额:" << money << endl;
}
void deposit(double m) override {
money += m;
cout << "存款成功,当前余额:" << money << endl;
}
bool withdraw(double m) override {
if (money < m) {
cout << "余额不足,取款失败!" << endl;
return false;
}
money -= m;
cout << "取款成功,当前余额:" << money << endl;
return true;
}
double get_balance() const override {
return money;
}
};
// 容器:账户列表
class AccountList {
public:
AccountList() {}
~AccountList() {
for (Account* p : accounts) {
delete p;
}
}
void add_account(Account* p) {
accounts.push_back(p);
}
Account* find_account(string name) const {
for (Account* p : accounts) {
if (p->name == name) {
return p;
}
}
return nullptr;
}
private:
vector<Account*> accounts;
};
// 文件处理:读取账户信息
void load_accounts(AccountList& account_list) {
ifstream ifs("accounts.txt");
if (!ifs) {
cout << "文件 accounts.txt 不存在,无法读取账户信息!" << endl;
return;
}
string type, name, password;
double money;
while (ifs >> type >> name >> password >> money) {
Account* p;
if (type == "user") {
p = new UserAccount(name, password, money);
} else if (type == "staff") {
p = new StaffAccount(name, password, money);
} else {
cout << "账户类型错误,无法读取账户信息!" << endl;
continue;
}
account_list.add_account(p);
}
ifs.close();
}
// 文件处理:保存账户信息
void save_accounts(const AccountList& account_list) {
ofstream ofs("accounts.txt");
if (!ofs) {
cout << "文件 accounts.txt 无法创建,无法保存账户信息!" << endl;
return;
}
for (Account* p : account_list.accounts) {
if (dynamic_cast<UserAccount*>(p)) {
ofs << "user ";
} else if (dynamic_cast<StaffAccount*>(p)) {
ofs << "staff ";
} else {
continue;
}
ofs << p->name << " " << p->password << " " << p->get_balance() << endl;
}
ofs.close();
}
// 程序入口
int main() {
AccountList account_list;
load_accounts(account_list);
while (true) {
cout << "请选择登录类型:1.用户 2.银行职员 0.退出" << endl;
int type;
cin >> type;
if (type == 0) {
break;
}
cout << "请输入账户名和密码(以空格分隔):" << endl;
string name, password;
cin >> name >> password;
Account* p = account_list.find_account(name);
if (!p || !p->validate(password)) {
cout << "账户名或密码错误,请重新输入!" << endl;
continue;
}
if (dynamic_cast<UserAccount*>(p)) {
// 用户菜单
while (true) {
cout << "请选择操作类型:1.查询余额 2.存款 3.取款 0.退出" << endl;
int op;
cin >> op;
if (op == 0) {
break;
}
switch (op) {
case 1:
p->show_info();
break;
case 2:
cout << "请输入存款金额:" << endl;
double m;
cin >> m;
p->deposit(m);
break;
case 3:
cout << "请输入取款金额:" << endl;
cin >> m;
p->withdraw(m);
break;
default:
cout << "操作类型错误,请重新输入!" << endl;
break;
}
}
} else if (dynamic_cast<StaffAccount*>(p)) {
// 银行职员菜单
while (true) {
cout << "请选择操作类型:1.查询余额 2.存款 3.取款 4.添加账户 0.退出" << endl;
int op;
cin >> op;
if (op == 0) {
break;
}
switch (op) {
case 1:
p->show_info();
break;
case 2:
cout << "请输入存款金额:" << endl;
double m;
cin >> m;
p->deposit(m);
break;
case 3:
cout << "请输入取款金额:" << endl;
cin >> m;
p->withdraw(m);
break;
case 4:
cout << "请输入账户类型(user/staff):" << endl;
string type;
cin >> type;
cout << "请输入账户名和密码(以空格分隔):" << endl;
cin >> name >> password;
cout << "请输入初始金额:" << endl;
cin >> m;
Account* p_new;
if (type == "user") {
p_new = new UserAccount(name, password, m);
} else if (type == "staff") {
p_new = new StaffAccount(name, password, m);
} else {
cout << "账户类型错误,无法创建账户!" << endl;
continue;
}
account_list.add_account(p_new);
cout << "账户创建成功!" << endl;
break;
default:
cout << "操作类型错误,请重新输入!" << endl;
break;
}
}
}
}
save_accounts(account_list);
cout << "程序已退出,账户信息已保存。" << endl;
return 0;
}
```
以上代码仅作为参考,具体实现可能还需要根据实际需求进行修改和完善。
阅读全文