1 题目: 在vc++6.0下编写个人银行账户管理系统 2 问题描述: 一个人可以有多个活期储蓄账户,一个活期储蓄账户包括账号(id)、余额(balance)、年利率(rate)等信息,还包括显示帐户信息(show)、存款(deposit)、取款(withdraw)、结算利息(settle)等操作,为此,设计一个类SavingsAccount,将id、balance、rate均作为其成员数据,将deposit、withdraw、settle均作为其成员函数来设计一个类。另外设计私有成员函数record来实现修改当前的余额并将余额的变动输出。 3 设计要求 1、能够实现账户的添加和删除功能,即开户和销户功能。 2、能够实现现金的存入和取出功能。 3、为了简化计算,计算利息的时候要求只计算年息,即每年的最后一天对当前金额计算年息。 4、对于账户里面存入和取出的现金,要求再下一次进行存入和取出的时候是在前一次操作的基础上进行的。 5、要求屏幕上可以显示目前所开设的账户信息,每个账户的存取款记录及结余等信息。
时间: 2024-03-07 09:52:11 浏览: 113
基于C++实现的活期储蓄账目管理系统源码+项目说明.zip
以下是一个可供参考的实现方案:
```cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
class SavingsAccount {
public:
// 构造函数
SavingsAccount(string id, double balance, double rate) :
id(id), balance(balance), rate(rate) {}
// 存款
void deposit(double money) {
record("Deposit", money);
balance += money;
}
// 取款
bool withdraw(double money) {
if (money > balance) return false;
record("Withdraw", money);
balance -= money;
return true;
}
// 结算利息
void settle() {
time_t now = time(nullptr);
tm* localTime = localtime(&now);
if (localTime->tm_mday == 31) { // 如果是每年的最后一天
double interest = balance * rate;
record("Interest", interest);
balance += interest;
}
}
// 显示账户信息
void show() {
cout << "Account ID: " << id << endl;
cout << "Account Balance: " << balance << endl;
cout << "Account Rate: " << rate << endl;
cout << "Account Record:" << endl;
for (auto& r : recordList) {
cout << r.first << "\t" << r.second << endl;
}
cout << endl;
}
// 获取账户ID
string getID() const {
return id;
}
private:
string id; // 账户ID
double balance; // 账户余额
double rate; // 年利率
vector<pair<string, double>> recordList; // 存取款记录
// 记录存取款记录
void record(string type, double money) {
recordList.push_back(make_pair(type, money));
}
};
class Bank {
public:
// 添加账户
void addAccount() {
string id;
double balance, rate;
cout << "Please input account ID: ";
cin >> id;
cout << "Please input account balance: ";
cin >> balance;
cout << "Please input account rate: ";
cin >> rate;
SavingsAccount account(id, balance, rate);
accountList.push_back(account);
cout << "Account added successfully!" << endl;
}
// 删除账户
void deleteAccount() {
string id;
cout << "Please input account ID: ";
cin >> id;
auto it = find_if(accountList.begin(), accountList.end(), [id](const SavingsAccount& account) {
return account.getID() == id;
});
if (it != accountList.end()) {
accountList.erase(it);
cout << "Account deleted successfully!" << endl;
}
else {
cout << "Account not found!" << endl;
}
}
// 存款
void deposit() {
string id;
double money;
cout << "Please input account ID: ";
cin >> id;
auto it = find_if(accountList.begin(), accountList.end(), [id](const SavingsAccount& account) {
return account.getID() == id;
});
if (it != accountList.end()) {
cout << "Please input deposit amount: ";
cin >> money;
it->deposit(money);
cout << "Deposit succeeded!" << endl;
}
else {
cout << "Account not found!" << endl;
}
}
// 取款
void withdraw() {
string id;
double money;
cout << "Please input account ID: ";
cin >> id;
auto it = find_if(accountList.begin(), accountList.end(), [id](const SavingsAccount& account) {
return account.getID() == id;
});
if (it != accountList.end()) {
cout << "Please input withdraw amount: ";
cin >> money;
if (it->withdraw(money)) {
cout << "Withdraw succeeded!" << endl;
}
else {
cout << "Withdraw failed: insufficient balance!" << endl;
}
}
else {
cout << "Account not found!" << endl;
}
}
// 结算利息
void settle() {
for (auto& account : accountList) {
account.settle();
}
cout << "Interest settled!" << endl;
}
// 显示所有账户信息
void showAll() {
for (auto& account : accountList) {
account.show();
}
}
private:
vector<SavingsAccount> accountList; // 账户列表
};
int main() {
Bank bank;
while (true) {
cout << "====Bank System====" << endl;
cout << "1. Add Account" << endl;
cout << "2. Delete Account" << endl;
cout << "3. Deposit" << endl;
cout << "4. Withdraw" << endl;
cout << "5. Settle Interest" << endl;
cout << "6. Show All Accounts" << endl;
cout << "0. Exit" << endl;
cout << "Please select an operation: ";
int op;
cin >> op;
switch (op) {
case 1:
bank.addAccount();
break;
case 2:
bank.deleteAccount();
break;
case 3:
bank.deposit();
break;
case 4:
bank.withdraw();
break;
case 5:
bank.settle();
break;
case 6:
bank.showAll();
break;
case 0:
cout << "Bye!" << endl;
return 0;
default:
cout << "Invalid operation!" << endl;
break;
}
}
}
```
在此代码中,我们定义了SavingsAccount类和Bank类。SavingsAccount类实现了账户的基本功能,包括存款、取款、结算利息、显示账户信息等。Bank类实现了银行的基本功能,包括添加账户、删除账户、存款、取款、结算利息、显示所有账户信息等。
在main函数中,我们通过一个while循环不断接收用户输入的操作,并调用相应的函数来实现这些操作。
阅读全文