用C++写一个程序,实现简单的银行管理系统,要求:信息: 账号(id)、余额(balance)、年利率(rate)等 操作:注册账户、 显示账户信息(show)、存款(deposit)、取款( withdraw)、结算利息(settle)等
时间: 2023-08-01 18:12:05 浏览: 168
好的,以下是一个简单的银行管理系统的 C++ 代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Account {
private:
string id;
double balance;
double rate;
public:
Account(string id, double balance, double rate) {
this->id = id;
this->balance = balance;
this->rate = rate;
}
string getId() {
return id;
}
double getBalance() {
return balance;
}
double getRate() {
return rate;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount > balance) {
cout << "余额不足!" << endl;
} else {
balance -= amount;
cout << "取款成功,当前余额为:" << balance << endl;
}
}
void settle() {
balance *= (1 + rate);
}
void show() {
cout << "账户信息:" << endl;
cout << "账号:" << id << endl;
cout << "余额:" << balance << endl;
cout << "年利率:" << rate << endl;
}
};
class Bank {
private:
vector<Account*> accounts;
public:
void registerAccount() {
string id;
double balance, rate;
cout << "请输入账号、余额、年利率(用空格分隔):";
cin >> id >> balance >> rate;
Account* account = new Account(id, balance, rate);
accounts.push_back(account);
cout << "注册成功!" << endl;
}
void showAccount(string id) {
for (Account* account : accounts) {
if (account->getId() == id) {
account->show();
return;
}
}
cout << "找不到该账户!" << endl;
}
void deposit(string id, double amount) {
for (Account* account : accounts) {
if (account->getId() == id) {
account->deposit(amount);
cout << "存款成功,当前余额为:" << account->getBalance() << endl;
return;
}
}
cout << "找不到该账户!" << endl;
}
void withdraw(string id, double amount) {
for (Account* account : accounts) {
if (account->getId() == id) {
account->withdraw(amount);
return;
}
}
cout << "找不到该账户!" << endl;
}
void settle(string id) {
for (Account* account : accounts) {
if (account->getId() == id) {
account->settle();
cout << "结算利息成功,当前余额为:" << account->getBalance() << endl;
return;
}
}
cout << "找不到该账户!" << endl;
}
};
int main() {
Bank bank;
while (true) {
cout << "请输入操作代号(1.注册账户 2.显示账户信息 3.存款 4.取款 5.结算利息 0.退出):";
int op;
cin >> op;
if (op == 0) {
break;
}
switch (op) {
case 1: {
bank.registerAccount();
break;
}
case 2: {
string id;
cout << "请输入账号:";
cin >> id;
bank.showAccount(id);
break;
}
case 3: {
string id;
double amount;
cout << "请输入账号、存款金额(用空格分隔):";
cin >> id >> amount;
bank.deposit(id, amount);
break;
}
case 4: {
string id;
double amount;
cout << "请输入账号、取款金额(用空格分隔):";
cin >> id >> amount;
bank.withdraw(id, amount);
break;
}
case 5: {
string id;
cout << "请输入账号:";
cin >> id;
bank.settle(id);
break;
}
default: {
cout << "无效的操作代号!" << endl;
break;
}
}
}
return 0;
}
```
该程序实现了注册账户、显示账户信息、存款、取款和结算利息等操作。用户可以根据输入的操作代号选择相应的操作,程序会根据用户的输入调用相应的函数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)