10.29總結
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;
}

浙公網安備 33010602011771號