账户信息
virtual void show() const;
};
class SavingsAccount:public Account //储蓄账户类
{
private:
Accumulator acc;//辅助计算利息的累加器
double rate;//存款的年利率
public:
//构造函数
SavingsAccount(const Date &date,const std::string &id,double rate);
double getRate() const {return rate;}
void deposit(const Date &date,double amount,const std::string &desc); //存入现金
void withdraw(const Date &date,double amount,const std::string &desc); //取出现金
void settle(const Date &date); //结算利息,每年1月1日调用一次该函数
void show() const; //显示账户信息
};
class CreditAccount:public Account //信用账户类
{
private:
Accumulator acc;//辅助计算利息的累加器
double credit;//信用额度
double rate;//欠款的日利率
double fee;//信用卡年费
double getDebt() const//获得欠款额
{
double balance=getBalance();
return (balance<0?balance:0);
}
public:
//构造函数
CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee);
double getCredit() const{return credit;}
double getRate() const{return rate;}
double getFee() const{return fee;}
double getAvailableCredit() const//获得可用信用额度
{
if(getBalance()<0)
return credit+getBalance();
else
return credit;
}
void deposit(const Date &date,double amount,const std::string &desc); //存入现金
void withdraw(const Date &date,double amount,const std::string &desc); //取出现金
void settle(const Date &date); //结算利息,每年1月1日调用一次该函数
void show() const; //显示账户信息
};
#endif//__ACOUNT_H__
//account.cpp
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;
double Account::total=0;
//Accout类的实现
Account::Account(const Date &date,const string &id):id(id),balance(0)
{
date.show();
cout<<"\t#"<<id<<"created"<<endl;
}
void Account::record(const Date &date,double amount,const string &desc)
{
amount=floor(amount*100+0.5)/100; //保留小数点后两位
balance+=amount;
total+=amount;
date.show ();
cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
}
void Account::show() const
{
cout<<id<<"\tBalance:"<<balance;
}
void Account::error(const string &msg)const
{
cout<<"Error(#"<<id<<"):"<<msg<<endl;
}
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate):Account(date,id),rate(rate),acc(date,0){}
void SavingsAccount::deposit(const Date &date,double amount,const string &desc)
{
record (date,amount,desc);
acc.change(date,getBalance());
}
void SavingsAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount>getBalance())
error("not enough money");
else
record(date,-amount,desc);
acc.change(date,getBalance());
}
void