设计一个银行账户管理系统,系统用户包括:银行管理员、客户两大类,其中银行管理员有两级设置:最高管理员和普通银行职员, 最高管理员可以设置普通管理人员的工号、密码等基本信息;普通管理员可以进行登录、开户、挂失、销户等功能。客户可以进行开户申请,存款、取款、修改密码、转账、查询操作。 系统设计基本功能要求如下: 1、用文件操作录入及保存用户的信息; 2、一个客户可以有多个账户,账户分为多种类型,如储蓄账户(存款有利息、不能透支)和信用卡账户(可以透支,透支有限额和还款相关设置,存款无利息),信用卡账户还可以进一步划分消费等级,如普通账户、VIP账户,此外,还可以设置复合卡((兼具储蓄和透支功能) 3、客户进行账户操作时,有三次机会输入密码,当输入错误时,提醒重新输入,如果三次机会结束则自动锁定账户24小时,可凭个人身份验证及绑定手机的验证码申请解锁; 4、分别实现管理员和客户的相关功能,并可按日期对银行所有的账户金额进行查询、统计、利息计算等;
#include #include #include #include #include #include
using namespace std;
class Account { protected: string accountNumber; string password; double balance;
public: Account(const string& accNum, const string& pwd, double bal) : accountNumber(accNum), password(pwd), balance(bal) {}
virtual ~Account() {}
const string& getAccountNumber() const {
return accountNumber;
}
double getBalance() const {
return balance;
}
virtual void deposit(double amount) = 0;
virtual bool withdraw(double amount) = 0;
virtual void display() const = 0;
virtual string serialize() const {
stringstream ss;
ss << accountNumber << "," << password << "," << balance;
return ss.str();
}
virtual void deserialize(const string& data) {
stringstream ss(data);
getline(ss, accountNumber, ',');
getline(ss, password, ',');
string balanceStr;
getline(ss, balanceStr, ',');
balance = stod(balanceStr);
}
};
class SavingsAccount : public Account { double interestRate;
public: SavingsAccount(const string& accNum, const string& pwd, double bal, double rate) : Account(accNum, pwd, bal), interestRate(rate) {}
void deposit(double amount) override {
balance += amount + (amount * interestRate);
}
bool withdraw(double amount) override {
if (balance >= amount) {
balance -= amount;
return true;
}
return false;
}
void display() const override {
cout << "储蓄账户 " << accountNumber << " 余额: " << balance << endl;
}
string serialize() const override {
stringstream ss;
ss << "S," << Account::serialize() << "," << interestRate;
return ss.str();
}
void deserialize(const string& data) override {
stringstream ss(data);
Account::deserialize(data);
string rateStr;
getline(ss, rateStr, ',');
interestRate = stod(rateStr);
}
};
class CreditAccount : public Account { double creditLimit; double interestRate;
public: CreditAccount(const string& accNum, const string& pwd, double bal, double limit, double rate) : Account(accNum, pwd, bal), creditLimit(limit), interestRate(rate) {}
void deposit(double amount) override {
balance += amount;
}
bool withdraw(double amount) override {
if (balance + creditLimit >= amount) {
balance -= amount;
return true;
}
return false;
}
void display() const override {
cout << "信用卡账户 " << accountNumber << " 余额: " << balance << endl;
}
string serialize() const override {
stringstream ss;
ss << "C," << Account::serialize() << "," << creditLimit << "," << interestRate;
return ss.str();
}
void deserialize(const string& data) override {
stringstream ss(data);
Account::deserialize(data);
string limitStr, rateStr;
getline(ss, limitStr, ',');
getline(ss, rateStr, ',');
creditLimit = stod(limitStr);
interestRate = stod(rateStr);
}
};
class BankAdmin { string employeeID; string password;
public: BankAdmin() = default; BankAdmin(const string& empID, const string& pwd) : employeeID(empID), password(pwd) {}
const string& getEmployeeID() const {
return employeeID;
}
const string& getPassword() const {
return password;
}
void setEmployeeID(const string& empID) {
employeeID = empID;
}
void setPassword(const string& pwd) {
password = pwd;
}
void display() const {
cout << "银行管理员 工号: " << employeeID << endl;
}
string serialize() const {
stringstream ss;
ss << employeeID << "," << password;
return ss.str();
}
void deserialize(const string& data) {
stringstream ss(data);
getline(ss, employeeID, ',');
getline(ss, password, ',');
}
};
class BankStaff { string employeeID; string password;
public: BankStaff(const string& empID, const string& pwd) : employeeID(empID), password(pwd) {}
const string& getEmployeeID() const {
return employeeID;
}
const string& getPassword() const {
return password;
}
void display() const {
cout << "银行职员 工号: " << employeeID << endl;
}
void login() {
string empID, pwd;
int attempts = 3;
while (attempts > 0) {
cout << "请输入工号和密码进行登录:" << endl;
cout << "工号: ";
cin >> empID;
cout << "密码: ";
cin >> pwd;
if (empID == employeeID && pwd == password) {
cout << "登录成功!" << endl;
return;
}
cout << "登录失败,请重新输入!" << endl;
attempts--;
}
cout << "登录失败次数过多,账户已锁定,请联系管理员解锁。" << endl;
unlockAccount();
}
void unlockAccount() {
string verificationCode;
cout << "请输入绑定手机收到的验证码进行解锁:" << endl;
cout << "验证码: ";
cin >> verificationCode;
// 进行解锁逻辑判断,验证成功后解锁账户
// ...
}
string serialize() const {
stringstream ss;
ss << employeeID << "," << password;
return ss.str();
}
void deserialize(const string& data) {
stringstream ss(data);
getline(ss, employeeID, ',');
getline(ss, password, ',');
}
};
class Customer { string customerID; string password; vector<Account*> accounts;
public: Customer(const string& custID, const string& pwd) : customerID(custID), password(pwd) {}
~Customer() {
for (Account* account : accounts) {
delete account;
}
}
const string& getCustomerID() const {
return customerID;
}
const string& getPassword() const {
return password;
}
void setCustomerID(const string& custID) {
customerID = custID;
}
void setPassword(const string& pwd) {
password = pwd;
}
void applyAccount() {
string accType;
double initialDeposit;
cout << "请选择账户类型(储蓄账户/信用卡账户): ";
cin >> accType;
cout << "请输入初始存款金额: ";
cin >> initialDeposit;
Account* account;
if (accType == "储蓄账户") {
account = new SavingsAccount(generateAccountNumber(), password, initialDeposit, 0.05);
}
else if (accType == "信用卡账户") {
account = new CreditAccount(generateAccountNumber(), password, initialDeposit, 1000.0, 0.1);
}
else {
cout << "无效的账户类型!" << endl;
return;
}
accounts.push_back(account);
cout << "账户开户成功!" << endl;
}
void deposit(double amount) {
string accNum;
cout << "请输入要存款的账户号码: ";
cin >> accNum;
Account* account = findAccount(accNum);
if (account != nullptr) {
account->deposit(amount);
cout << "存款成功!" << endl;
}
else {
cout << "找不到指定的账户!" << endl;
}
}
void withdraw(double amount) {
string accNum;
cout << "请输入要取款的账户号码: ";
cin >> accNum;
Account* account = findAccount(accNum);
if (account != nullptr) {
if (account->withdraw(amount)) {
cout << "取款成功!" << endl;
}
else {
cout << "余额不足!" << endl;
}
}
else {
cout << "找不到指定的账户!" << endl;
}
}
void changePassword() {
string newPassword;
cout << "请输入新密码: ";
cin >> newPassword;
password = newPassword;
cout << "密码修改成功!" << endl;
}
void transfer(double amount) {
string sourceAccNum, targetAccNum;
cout << "请输入转出账户号码: ";
cin >> sourceAccNum;
cout << "请输入转入账户号码: ";
cin >> targetAccNum;
Account* sourceAcc = findAccount(sourceAccNum);
Account* targetAcc = findAccount(targetAccNum);
if (sourceAcc != nullptr && targetAcc != nullptr) {
if (sourceAcc->withdraw(amount)) {
targetAcc->deposit(amount);
cout << "转账成功!" << endl;
}
else {
cout << "余额不足!" << endl;
}
}
else {
cout << "找不到指定的账户!" << endl;
}
}
void displayAccounts() const {
for (Account* account : accounts) {
account->display();
}
}
string serialize() const {
stringstream ss;
ss << customerID << "," << password << "," << accounts.size();
for (Account* account : accounts) {
ss << "," << account->serialize();
}
return ss.str();
}
void deserialize(const string& data) {
stringstream ss(data);
getline(ss, customerID, ',');
getline(ss, password, ',');
string accountData;
getline(ss, accountData, ',');
int accountCount = stoi(accountData);
for (int i = 0; i < accountCount; i++) {
getline(ss, accountData, ',');
Account* account = createAccountFromData(accountData);
if (account != nullptr) {
accounts.push_back(account);
}
}
}
private: string generateAccountNumber() const { static int counter = 0; return "ACC-" + to_string(++counter); }
Account* findAccount(const string& accNum) const {
for (Account* account : accounts) {
if (account->getAccountNumber() == accNum) {
return account;
}
}
return nullptr;
}
Account* createAccountFromData(const string& data) const {
stringstream ss(data);
string accType;
getline(ss, accType, ',');
if (accType == "S") {
string accNum, pwd, balanceStr, interestRateStr;
getline(ss, accNum, ',');
getline(ss, pwd, ',');
getline(ss, balanceStr, ',');
getline(ss, interestRateStr, ',');
double balance = stod(balanceStr);
double interestRate = stod(interestRateStr);
return new SavingsAccount(accNum, pwd, balance, interestRate);
}
else if (accType == "C") {
string accNum, pwd, balanceStr, creditLimitStr, interestRateStr;
getline(ss, accNum, ',');
getline(ss, pwd, ',');
getline(ss, balanceStr, ',');
getline(ss, creditLimitStr, ',');
getline(ss, interestRateStr, ',');
double balance = stod(balanceStr);
double creditLimit = stod(creditLimitStr);
double interestRate = stod(interestRateStr);
return new CreditAccount(accNum, pwd, balance, creditLimit, interestRate);
}
return nullptr;
}
};
class Bank { BankAdmin admin; vector staff; vector customers;
public: Bank() { loadBankData(); }
~Bank() {
saveBankData();
}
void loadBankData() {
ifstream adminFile("admin.txt");
if (adminFile.is_open()) {
string line;
if (getline(adminFile, line)) {
admin.deserialize(line);
}
adminFile.close();
}
else {
cout << "无法打开管理员数据文件!" << endl;
}
ifstream staffFile("staff.txt");
if (staffFile.is_open()) {
string line;
while (getline(staffFile, line)) {
BankStaff staffMember("", "");
staffMember.deserialize(line);
staff.push_back(staffMember);
}
staffFile.close();
}
else {
cout << "无法打开银行职员数据文件!" << endl;
}
ifstream customerFile("customer.txt");
if (customerFile.is_open()) {
string line;
while (getline(customerFile, line)) {
Customer customer("", "");
customer.deserialize(line);
customers.push_back(customer);
}
customerFile.close();
}
else {
cout << "无法打开客户数据文件!" << endl;
}
}
void saveBankData() {
ofstream adminFile("admin.txt");
if (adminFile) {
adminFile << admin.serialize() << endl;
adminFile.close();
}
else {
cout << "无法打开管理员数据文件!" << endl;
}
ofstream staffFile("staff.txt");
if (staffFile) {
for (const BankStaff& staffMember : staff) {
staffFile << staffMember.serialize() << endl;
}
staffFile.close();
}
else {
cout << "无法打开银行职员数据文件!" << endl;
}
ofstream customerFile("customer.txt");
if (customerFile) {
for (const Customer& customer : customers) {
customerFile << customer.serialize() << endl;
}
customerFile.close();
}
else {
cout << "无法打开客户数据文件!" << endl;
}
}
void adminLogin() {
string empID, pwd;
cout << "请输入管理员工号和密码进行登录:" << endl;
cout << "工号: ";
cin >> empID;
cout << "密码: ";
cin >> pwd;
if (empID == admin.getEmployeeID() && pwd == admin.getPassword()) {
cout << "管理员登录成功!" << endl;
admin.display();
adminMenu();
}
else {
cout << "管理员登录失败!" << endl;
}
}
void adminMenu() {
int choice;
while (true) {
cout << "管理员菜单:" << endl;
cout << "1. 添加银行职员" << endl;
cout << "2. 显示所有银行职员" << endl;
cout << "3. 删除银行职员" << endl;
cout << "4. 返回上级菜单" << endl;
cout << "请选择操作: ";
cin >> choice;
switch (choice) {
case 1:
addBankStaff();
break;
case 2:
displayBankStaff();
break;
case 3:
removeBankStaff();
break;
case 4:
return;
default:
cout << "无效的选项!" << endl;
break;
}
}
}
void addBankStaff() {
string empID, pwd;
cout << "请输入要添加的银行职员工号和密码:" << endl;
cout << "工号: ";
cin >> empID;
cout << "密码: ";
cin >> pwd;
BankStaff newStaff(empID, pwd);
staff.push_back(newStaff);
cout << "银行职员添加成功!" << endl;
}
void displayBankStaff() const {
cout << "银行职员列表:" << endl;
for (const BankStaff& staffMember : staff) {
staffMember.display();
}
}
void removeBankStaff() {
string empID;
cout << "请输入要删除的银行职员工号: ";
cin >> empID;
for (auto it = staff.begin(); it != staff.end(); ++it) {
if (it->getEmployeeID() == empID) {
staff.erase(it);
cout << "银行职员删除成功!" << endl;
return;
}
}
cout << "找不到指定的银行职员!" << endl;
}
void customerLogin() {
string custID, pwd;
cout << "请输入客户账号和密码进行登录:" << endl;
cout << "账号: ";
cin >> custID;
cout << "密码: ";
cin >> pwd;
Customer* customer = findCustomer(custID);
if (customer != nullptr && customer->getPassword() == pwd) {
cout << "客户登录成功!" << endl;
customerMenu(*customer);
}
else {
cout << "客户登录失败!" << endl;
}
}
void customerMenu(Customer& customer) {
int choice;
while (true) {
cout << "客户菜单:" << endl;
cout << "1. 申请开户" << endl;
cout << "2. 存款" << endl;
cout << "3. 取款" << endl;
cout << "4. 修改密码" << endl;
cout << "5. 转账" << endl;
cout << "6. 显示账户" << endl;
cout << "7. 返回上级菜单" << endl;
cout << "请选择操作: ";
cin >> choice;
switch (choice) {
case 1:
customer.applyAccount();
break;
case 2: {
double amount;
cout << "请输入存款金额: ";
cin >> amount;
customer.deposit(amount);
break;
}
case 3: {
double amount;
cout << "请输入取款金额: ";
cin >> amount;
customer.withdraw(amount);
break;
}
case 4:
customer.changePassword();
break;
case 5: {
double amount;
cout << "请输入转账金额: ";
cin >> amount;
customer.transfer(amount);
break;
}
case 6:
customer.displayAccounts();
break;
case 7:
return;
default:
cout << "无效的选项!" << endl;
break;
}
}
}
Customer* findCustomer(const string& custID) {
for (Customer& customer : customers) {
if (customer.getCustomerID() == custID) {
return &customer;
}
}
return nullptr;
}
};
int main() { Bank bank; int choice; while (true) { cout << "银行系统菜单:" << endl; cout << "1. 管理员登录" << endl; cout << "2. 客户登录" << endl; cout << "3. 退出系统" << endl; cout << "请选择操作: "; cin >> choice;
switch (choice) {
case 1:
bank.adminLogin();
break;
case 2:
bank.customerLogin();
break;
case 3:
cout << "系统已退出。" << endl;
return 0;
default:
cout << "无效的选项!" << endl;
break;
}
}
}