基于C++的个人银行账户管理系统 : 设计一个面向个人的银行账户管理系统, 包括设置账号、余额、年利率等信息,还包括显示信息、存款、取款、结算利息等操作。要求: (1)无论是存款、取款还是结算利息,都需要修改当前的余额并且将余额的变动输出。 (2)实现利息计算。由于账户的余额是不断变化的,因此不能同过余额与年利率相乘的办法来计算年利,而是需要将一年中每天的余额累计起来再除以一年的总天数。 (3)在个人银行账户管理程序中增加信用卡账户。 (4)输出不同账户信息。 (5)将所有用户信息存于文件中。
时间: 2024-03-24 15:38:24 浏览: 213
以下是一个基于C++的个人银行账户管理系统的示例代码,其中包括一个账户类(Account)和一个信用卡账户类(CreditAccount):
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
class Account {
private:
string accountNumber;
double balance;
double annualInterestRate;
public:
Account(string accountNumber, double balance, double annualInterestRate) {
this->accountNumber = accountNumber;
this->balance = balance;
this->annualInterestRate = annualInterestRate;
}
string getAccountNumber() {
return accountNumber;
}
double getBalance() {
return balance;
}
double getAnnualInterestRate() {
return annualInterestRate;
}
void deposit(double amount) {
balance += amount;
cout << "Deposit $" << fixed << setprecision(2) << amount << " successfully. Current balance is $" << fixed << setprecision(2) << balance << endl;
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
cout << "Withdraw $" << fixed << setprecision(2) << amount << " successfully. Current balance is $" << fixed << setprecision(2) << balance << endl;
} else {
cout << "Withdraw failed. Not enough balance." << endl;
}
}
double calculateInterest() {
time_t now = time(0);
tm* ltm = localtime(&now);
int days = ((ltm->tm_year + 1900) % 4 == 0 && (ltm->tm_year + 1900) % 100 != 0) || (ltm->tm_year + 1900) % 400 == 0 ? 366 : 365;
double dailyBalance = balance / days;
double annualInterest = dailyBalance * annualInterestRate;
cout << "The annual interest is $" << fixed << setprecision(2) << annualInterest << endl;
return annualInterest;
}
};
class CreditAccount : public Account {
private:
double creditLimit;
double debt;
double debtInterestRate;
public:
CreditAccount(string accountNumber, double balance, double annualInterestRate, double creditLimit, double debt, double debtInterestRate) : Account(accountNumber, balance, annualInterestRate) {
this->creditLimit = creditLimit;
this->debt = debt;
this->debtInterestRate = debtInterestRate;
}
double getCreditLimit() {
return creditLimit;
}
double getDebt() {
return debt;
}
double getDebtInterestRate() {
return debtInterestRate;
}
void overdraft(double amount) {
if (amount > creditLimit - debt) {
cout << "Overdraft failed. Exceed the credit limit." << endl;
} else {
debt += amount;
cout << "Overdraft $" << fixed << setprecision(2) << amount << " successfully. Current debt is $" << fixed << setprecision(2) << debt << endl;
}
}
void repayment(double amount) {
if (amount > debt) {
cout << "Repayment failed. The amount is greater than the debt." << endl;
} else {
debt -= amount;
cout << "Repayment $" << fixed << setprecision(2) << amount << " successfully. Current debt is $" << fixed << setprecision(2) << debt << endl;
}
}
double calculateInterest() {
time_t now = time(0);
tm* ltm = localtime(&now);
int days = ((ltm->tm_year + 1900) % 4 == 0 && (ltm->tm_year + 1900) % 100 != 0) || (ltm->tm_year + 1900) % 400 == 0 ? 366 : 365;
double dailyDebt = debt / days;
double dailyBalance = (getBalance() - debt) / days;
double annualInterest = dailyBalance * getAnnualInterestRate() - dailyDebt * debtInterestRate;
cout << "The annual interest is $" << fixed << setprecision(2) << annualInterest << endl;
return annualInterest;
}
};
void saveAccount(Account* account) {
ofstream fout(account->getAccountNumber() + ".txt");
fout << fixed << setprecision(2);
fout << account->getAccountNumber() << endl;
fout << account->getBalance() << endl;
fout << account->getAnnualInterestRate() << endl;
fout.close();
}
Account* loadAccount(string accountNumber) {
ifstream fin(accountNumber + ".txt");
if (!fin.is_open()) {
return nullptr;
}
string number;
double balance;
double annualInterestRate;
fin >> number >> balance >> annualInterestRate;
fin.close();
Account* account = new Account(number, balance, annualInterestRate);
return account;
}
void saveCreditAccount(CreditAccount* account) {
ofstream fout(account->getAccountNumber() + ".txt");
fout << fixed << setprecision(2);
fout << account->getAccountNumber() << endl;
fout << account->getBalance() << endl;
fout << account->getAnnualInterestRate() << endl;
fout << account->getCreditLimit() << endl;
fout << account->getDebt() << endl;
fout << account->getDebtInterestRate() << endl;
fout.close();
}
CreditAccount* loadCreditAccount(string accountNumber) {
ifstream fin(accountNumber + ".txt");
if (!fin.is_open()) {
return nullptr;
}
string number;
double balance;
double annualInterestRate;
double creditLimit;
double debt;
double debtInterestRate;
fin >> number >> balance >> annualInterestRate >> creditLimit >> debt >> debtInterestRate;
fin.close();
CreditAccount* account = new CreditAccount(number, balance, annualInterestRate, creditLimit, debt, debtInterestRate);
return account;
}
int main() {
Account* account1 = new Account("1234567890", 1000.00, 0.01);
saveAccount(account1);
Account* account2 = loadAccount("1234567890");
cout << "Account " << account2->getAccountNumber() << " balance is $" << fixed << setprecision(2) << account2->getBalance() << endl;
account2->deposit(500.00);
saveAccount(account2);
account2->withdraw(2000.00);
account2->calculateInterest();
CreditAccount* account3 = new CreditAccount("0987654321", 1000.00, 0.01, 2000.00, 500.00, 0.05);
saveCreditAccount(account3);
CreditAccount* account4 = loadCreditAccount("0987654321");
cout << "Credit Account " << account4->getAccountNumber() << " balance is $" << fixed << setprecision(2) << account4->getBalance() << endl;
account4->overdraft(1500.00);
saveCreditAccount(account4);
account4->repayment(1000.00);
account4->calculateInterest();
delete account1;
delete account2;
delete account3;
delete account4;
return 0;
}
```
在这个示例中,我们使用了文件来存储所有用户信息,每个账户信息存储在一个单独的文本文件中,文件名使用账号作为标识符。在程序启动时,我们可以创建账户对象并保存到文件中,也可以从文件中读取账户对象。在程序结束时,我们需要将所有账户对象保存到文件中。
对于账户类和信用卡账户类,我们分别实现了存款、取款、结算利息、透支和还款等方法,并且在每个方法中修改当前的余额并且将余额的变动输出。利息计算的实现也与要求相符,将一年中每天的余额累计起来再除以一年的总天数。
阅读全文