头文件
#ifndef _ACCOUNT_
#define _ACCOUNT_
class SavingAccount{
public:
SavingAccount(int date, int id, double rate);
int getId();
double getBalance();
double getRate();
void show();//显示信息
void deposit(int date, double amount);//存款
void withdraw(int date, double amount);//取款
void settle(int date);//结算利息
private:
int m_iID;
double m_dBalance;//余额,
double m_dRate;//利率,
int m_iLastDate;//上一次余额变动的日期。
double m_dAccumulate;//用来存储上次计算利息以后到最近一次余额变动时余额按日累加值,
void record(int date, double amount);//amount为金额。
double accumulate(int date);//用来计算截止至指定日期的账户余额按日累计值。
};
#endif
源文件1
#include "SavingAccount.h"
#include
#include
using namespace std;
SavingAccount::SavingAccount(int date, int id, double rate){
this->m_iLastDate = date;
this->m_iID = id;
this->m_dRate = rate;
this->m_dAccumulate = 0;
this->m_dBalance = 0;
}
int SavingAccount::getId(){return this->m_iID;}
double SavingAccount::getBalance(){return this->m_dBalance;}
double SavingAccount::getRate(){return this->m_dRate;}
void SavingAccount::show(){
cout << "账号为" << this->getId() << "余额为:" << this->getBalance() << "年利率为:" << this->getRate() << endl;
}
void SavingAccount::deposit(int date, double amount){
this->record(date, amount);
}
void SavingAccount::withdraw(int date, double amount){
if(amount > getBalance()){
cout << "你的余额不足,无法取款!" << endl;
}
else{
record(date, -amount);
}
}
void SavingAccount::settle(int date){
double interest = accumulate(date) * m_dRate / 365;//计算年息。
if(interest != 0){
record(date, interest);
}
m_dAccumulate = 0;
}
//获得到指定日期为止的存款金额按日累计值。
double SavingAccount::accumulate(int date){
return m_dAccumulate + m_dBalance * (date - m_iLastDate);
}
void SavingAccount::record(int date, double amount){
m_dAccumulate = accumulate(date);
m_iLastDate = date;
amount = floor(amount * 100 + 0.5) / 100;
m_dBalance += amount;
}
源文件2
#include
#include"SavingAccount.h"
int main(){
//建立账户
SavingAccount sa0(1, 21325302, 0.015);
SavingAccount sa1(1, 58320212, 0.015);
//几笔账目
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
//开户后第90天到了计息日,结算所有年息。
sa0.settle(90);
sa1.settle(90);
//输出账户的所有信息。
sa0.show();
sa1.show();
system("pause");
return 0;
}