写一个c++代码符合以下要求“银行账户管理系统设计与实现”,能够生成各类账户,配置基本属性并完成相关信息查询和显示。要求系统设计具有一定弹性和可扩展性,使得后续维护和扩展功能更容易,后续可以随时增加不同
时间: 2023-06-13 16:08:05 浏览: 233
类型的账户,如定期账户、活期账户、储蓄账户等,并且能够支持账户的增加、删除、修改和查询操作。
以下是一个简单的银行账户管理系统的C++代码:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Account {
public:
Account(string name, int id, double balance) : name(name), id(id), balance(balance) {}
virtual ~Account() {}
virtual void deposit(double amount) {
balance += amount;
}
virtual void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
cout << "Error: insufficient balance!" << endl;
}
}
virtual void display() {
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Balance: " << balance << endl;
}
protected:
string name;
int id;
double balance;
};
class CheckingAccount : public Account {
public:
CheckingAccount(string name, int id, double balance) : Account(name, id, balance) {}
void withdraw(double amount) override {
if (balance >= amount) {
balance -= amount;
} else {
cout << "Error: insufficient balance!" << endl;
cout << "An overdraft protection of 5000 CNY is applied." << endl;
balance -= 5000;
}
}
};
class SavingsAccount : public Account {
public:
SavingsAccount(string name, int id, double balance, double interestRate) : Account(name, id, balance), interestRate(interestRate) {}
void deposit(double amount) override {
balance += amount;
balance += balance * interestRate;
}
private:
double interestRate;
};
class Bank {
public:
void addAccount(Account* account) {
accounts.push_back(account);
}
void removeAccount(int id) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i]->id == id) {
accounts.erase(accounts.begin() + i);
break;
}
}
}
Account* findAccount(int id) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i]->id == id) {
return accounts[i];
}
}
return nullptr;
}
private:
vector<Account*> accounts;
};
int main() {
Bank bank;
Account* account1 = new Account("Alice", 1001, 10000);
Account* account2 = new CheckingAccount("Bob", 1002, 5000);
Account* account3 = new SavingsAccount("Charlie", 1003, 20000, 0.01);
bank.addAccount(account1);
bank.addAccount(account2);
bank.addAccount(account3);
Account* account4 = new Account("David", 1004, 30000);
bank.addAccount(account4);
Account* account5 = bank.findAccount(1002);
if (account5 != nullptr) {
account5->deposit(1000);
}
bank.removeAccount(1003);
for (int i = 0; i < bank.accounts.size(); i++) {
bank.accounts[i]->display();
cout << endl;
}
return 0;
}
```
以上代码实现了一个简单的银行账户管理系统,包括了三种类型的账户:基本账户、支票账户和储蓄账户。Bank类提供了添加、删除和查找账户的功能。Account类提供了基本的账户操作,CheckingAccount类和SavingsAccount类分别在withdraw()和deposit()方法中增加了特定的操作。在main()函数中,我们创建了几个账户,并进行了一些操作,最后输出了所有账户的信息。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)