include
include
include
include
include
include
using namespace std;
// 账户类型枚举
enum AccountType {
SAVING, // 活期
FIXED_1Y, // 定期一年
FIXED_3Y, // 定期三年
FIXED_5Y // 定期五年
};
// 账户类
class Account {
private:
string accountId; // 账户ID
AccountType type; // 账户类型
double balance; // 余额
time_t openTime; // 开户时间
double interestRate; // 利率
double interest; // 利息
public:
Account(string id, AccountType t, double rate)
: accountId(id), type(t), balance(0), interestRate(rate), interest(0) {
openTime = time(nullptr);
}
string getAccountId() const { return accountId; }
AccountType getType() const { return type; }
double getBalance() const { return balance; }
double getInterest() const { return interest; }
time_t getOpenTime() const { return openTime; }
double getInterestRate() const { return interestRate; }void setInterestRate(double rate) { interestRate = rate; }// 存款
void deposit(double amount) {balance += amount;
}// 取款,返回是否成功
bool withdraw(double amount) {if (amount <= balance) {balance -= amount;return true;}return false;
}// 计算利息(移除const修饰符,因为需要修改interest)
void calculateInterest() {time_t now = time(nullptr);double days = difftime(now, openTime) / (60 * 60 * 24);interest = balance * interestRate * days / 365;
}// 显示账户信息
void display() const {cout << "账户ID: " << accountId << endl;cout << "账户类型: ";switch (type) {case SAVING: cout << "活期"; break;case FIXED_1Y: cout << "定期一年"; break;case FIXED_3Y: cout << "定期三年"; break;case FIXED_5Y: cout << "定期五年"; break;}cout << endl;cout << "余额: " << fixed << setprecision(2) << balance << endl;cout << "开户时间: " << ctime(&openTime);cout << "利率: " << interestRate * 100 << "%" << endl;cout << "利息: " << fixed << setprecision(2) << interest << endl;
}
};
// 账户管理系统类
class AccountManager {
private:
vector
double savingRate; // 活期利率
double fixed1YRate; // 一年定期利率
double fixed3YRate; // 三年定期利率
double fixed5YRate; // 五年定期利率
public:
AccountManager()
: savingRate(0.003), fixed1YRate(0.015), fixed3YRate(0.0275), fixed5YRate(0.03) {}
// 开户
bool openAccount(string id, AccountType type) {// 检查账户是否已存在for (const auto& acc : accounts) {if (acc.getAccountId() == id) {cout << "账户已存在!" << endl;return false;}}double rate;switch (type) {case SAVING: rate = savingRate; break;case FIXED_1Y: rate = fixed1YRate; break;case FIXED_3Y: rate = fixed3YRate; break;case FIXED_5Y: rate = fixed5YRate; break;default: rate = 0;}accounts.push_back(Account(id, type, rate));cout << "开户成功!" << endl;return true;
}// 销户
bool closeAccount(string id) {for (auto it = accounts.begin(); it != accounts.end(); ++it) {if (it->getAccountId() == id) {it->calculateInterest();cout << "销户信息:" << endl;it->display();accounts.erase(it);cout << "销户成功!" << endl;return true;}}cout << "账户不存在!" << endl;return false;
}// 汇总
void summary() const {double totalBalance = 0;double totalInterest = 0;cout << "账户汇总信息:" << endl;// 这里需要通过非const引用来调用calculateInterest,因此使用mutable迭代for (auto& acc : const_cast<vector<Account>&>(accounts)) {acc.calculateInterest();acc.display();cout << "------------------------" << endl;totalBalance += acc.getBalance();totalInterest += acc.getInterest();}cout << "总余额: " << fixed << setprecision(2) << totalBalance << endl;cout << "总利息: " << fixed << setprecision(2) << totalInterest << endl;
}// 查询
void query(string id) const {for (auto& acc : const_cast<vector<Account>&>(accounts)) {if (acc.getAccountId() == id) {acc.calculateInterest();acc.display();return;}}cout << "账户不存在!" << endl;
}// 存款
bool deposit(string id, double amount) {for (auto& acc : accounts) {if (acc.getAccountId() == id) {acc.deposit(amount);cout << "存款成功!当前余额:" << fixed << setprecision(2) << acc.getBalance() << endl;return true;}}cout << "账户不存在!" << endl;return false;
}// 取款
bool withdraw(string id, double amount) {for (auto& acc : accounts) {if (acc.getAccountId() == id) {if (acc.withdraw(amount)) {cout << "取款成功!当前余额:" << fixed << setprecision(2) << acc.getBalance() << endl;return true;} else {cout << "余额不足!" << endl;return false;}}}cout << "账户不存在!" << endl;return false;
}// 结息
void calculateInterest() {for (auto& acc : accounts) {acc.calculateInterest();}cout << "结息完成!" << endl;
}// 文件导入
bool importFromFile(const string& filename) {ifstream file(filename);if (!file.is_open()) {cout << "无法打开文件!" << endl;return false;}accounts.clear();int count;file >> count;for (int i = 0; i < count; ++i) {string id;int type;double balance, rate;time_t openTime;file >> id >> type >> balance >> openTime >> rate;Account acc(id, static_cast<AccountType>(type), rate);// 导入时需要设置余额(原代码未处理)for (int j = 0; j < balance; ++j) { // 临时模拟存款(实际应添加setBalance接口)acc.deposit(1);}accounts.push_back(acc);}file.close();cout << "导入成功!" << endl;return true;
}// 文件导出
bool exportToFile(const string& filename) const {ofstream file(filename);if (!file.is_open()) {cout << "无法打开文件!" << endl;return false;}file << accounts.size() << endl;for (const auto& acc : accounts) {file << acc.getAccountId() << " "<< acc.getType() << " "<< acc.getBalance() << " "<< acc.getOpenTime() << " "<< acc.getInterestRate() << endl;}file.close();cout << "导出成功!" << endl;return true;
}// 修改利率
void changeInterestRate() {cout << "当前利率:" << endl;cout << "活期:" << savingRate * 100 << "%" << endl;cout << "一年定期:" << fixed1YRate * 100 << "%" << endl;cout << "三年定期:" << fixed3YRate * 100 << "%" << endl;cout << "五年定期:" << fixed5YRate * 100 << "%" << endl;cout << "选择要修改的利率类型:" << endl;cout << "1. 活期" << endl;cout << "2. 一年定期" << endl;cout << "3. 三年定期" << endl;cout << "4. 五年定期" << endl;int choice;cin >> choice;double newRate;cout << "输入新的利率(例如 0.01 表示 1%):";cin >> newRate;switch (choice) {case 1:savingRate = newRate;for (auto& acc : accounts) {if (acc.getType() == SAVING) {acc.setInterestRate(newRate);}}break;case 2:fixed1YRate = newRate;for (auto& acc : accounts) {if (acc.getType() == FIXED_1Y) {acc.setInterestRate(newRate);}}break;case 3:fixed3YRate = newRate;for (auto& acc : accounts) {if (acc.getType() == FIXED_3Y) {acc.setInterestRate(newRate);}}break;case 4:fixed5YRate = newRate;for (auto& acc : accounts) {if (acc.getType() == FIXED_5Y) {acc.setInterestRate(newRate);}}break;default:cout << "选择无效!" << endl;return;}cout << "利率修改成功!" << endl;
}
};
int main() {
AccountManager manager;
int choice;
string id;
double amount;
string filename;
while (true) {cout << "\n个人银行账户管理系统:" << endl;cout << "1 开户" << endl;cout << "2 销户" << endl;cout << "3 汇总" << endl;cout << "4 查询" << endl;cout << "5 存款" << endl;cout << "6 取款" << endl;cout << "7 结息" << endl;cout << "8 修改利率" << endl;cout << "9 文件导入" << endl;cout << "10 文件导出" << endl;cout << "11 退出" << endl;cout << "请选择:";cin >> choice;switch (choice) {case 1: {cout << "输入账户ID:";cin >> id;cout << "选择账户类型(1:活期,2:一年定期,3:三年定期,4:五年定期):";int typeChoice;cin >> typeChoice;AccountType type;switch (typeChoice) {case 1: type = SAVING; break;case 2: type = FIXED_1Y; break;case 3: type = FIXED_3Y; break;case 4: type = FIXED_5Y; break;default: type = SAVING;}manager.openAccount(id, type);break;}case 2:cout << "输入要销户的账户ID:";cin >> id;manager.closeAccount(id);break;case 3:manager.summary();break;case 4:cout << "输入要查询的账户ID:";cin >> id;manager.query(id);break;case 5:cout << "输入账户ID:";cin >> id;cout << "输入存款金额:";cin >> amount;manager.deposit(id, amount);break;case 6:cout << "输入账户ID:";cin >> id;cout << "输入取款金额:";cin >> amount;manager.withdraw(id, amount);break;case 7:manager.calculateInterest();break;case 8:manager.changeInterestRate();break;case 9:cout << "输入导入文件名:";cin >> filename;manager.importFromFile(filename);break;case 10:cout << "输入导出文件名:";cin >> filename;manager.exportToFile(filename);break;case 11:cout << "谢谢使用,再见!" << endl;return 0;default:cout << "选择无效,请重新选择!" << endl;}
}return 0;
}