首先是新增的Date类头文件(文件名:Date.h),做的时候有个疑问,貌似这个类的totaldays成员并没有被用到(可能后续会用到吧),另外视频中用的PPT也存在小错误(有几处需要将id的类型调整成string类型的没有调整,可能是PPT中忘改了)
#ifndef __DATE_H__
#define __DATE_H__
class Date
{
private:
int year;
int month;
int day;
int totalDays;//表示这一天的相对日期
public:
Date(int year,int month,int day);
int getYear() const { return year; };
int getMonth() const { return month; };
int getDay() const { return day; };
int getMaxDay() const ;//得到当前月的天数
bool isLeapYear() const ;//用来判断是否为闰年
void show() const;
int distance(const Date &date) const ;//用来判断当前天数与指定日期相差天数
};
#endif // !__DATE_H__
然后是Date类的实现文件(文件名Date.cpp)
#include"Date.h"
#include<iostream>
#include<cstdlib>
using namespace std;
namespace{ //namespace使下面的定义只在当前文件中有效
const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
//存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项0,当月份为1月时通过数组下标能够获得正确的天数
}
Date::Date(int year, int month, int day):year(year),month(month),day(day)
{
if (day <= 0 || day > getMaxDay())
{
cout << "invalid date" << endl;
show();
cout << endl;
exit(1);//它用来终止当前程序的执行,并且将一个整数返回给系统,该整数的作用与由主函数main返回的整数相同,如果是0表示程序正常退出,如果非0表示程序异常终止
}
}
void Date::show() const
{
cout << year<<"-" << month<<"-" << day << "\t";
}
int Date::getMaxDay() const //得到当前月的天数
{
if (isLeapYear() && month > 2)
{
return 365 * year + (year - 1) / 4 - ((year - 1) / 100) + ((year - 1) / 400) + DAYS_BEFORE_MONTH[month - 1] + 1 + day;
}
else
{
return 365 * year + (year - 1) / 4 - ((year - 1) / 100) + ((year - 1) / 400) + DAYS_BEFORE_MONTH[month - 1] + day;
}
}
bool Date::isLeapYear() const//用来判断是否为闰年
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 3200 != 0) || year % 172800 == 0)
{
return true;
}
else
{
return false;
}
}
int Date::distance(const Date& date) const
{
return getMaxDay() - date.getMaxDay();
}
接下来是更新的SavingAccount类头文件,文件名(SavingsAccount.h)
#ifndef __SAVINGSACCOUNT_H__
#define __SAVINGSACCOUNT_H__
#include<iostream>
#include<string>
#include<cmath>
#include"Date.h"
using namespace std;
class SavingsAccount
{
private:
string id;//账户id
double balance;//账户余额
double rate;//账户利率
Date lastDate;//上一次存取款日期
double accumulation;//存储上次计算利息后直到最近一次余额变动时余额按日累加的值
static double total;
void record(const Date &date, double amount,const string &desc);//记录账户每一次变动情况
void error(const string &msg);
double accumulate(const Date &date) const//计算截至指定日期的账户余额按日累积值
{
return accumulation + balance * abs(lastDate.distance(date));
}
public:
SavingsAccount(const Date &date, const string& id,double rate);//账户构造函数
string getId()//获取账户id
{
return id;
}
double getBalance()//获取账户余额
{
return balance;
}
double getRate()//获取账户的利率
{
return rate;
}
void show();//输出当前账户状态和信息
void deposit(const Date &date, double amount, const string& desc);//存款操作
void withdraw(const Date &date, double amount, const string& desc);//取款操作
void settle(const Date &date);//结算利息操作
static double gettotal() { return total; };//返回total值的静态成员函数
};
#endif // !__SAVINGSACCOUNT_H__
然后是SavingsAccount类的实现文件(SavingsAccount.cpp)
#include"SavingsAccount.h"
double SavingsAccount::total = 0;
SavingsAccount::SavingsAccount(const Date &date,const string &id, double rate):id(id),rate(rate),lastDate(date),balance(0),accumulation(0)
{
date.show();
cout << "\t#" << id << "\t"<<"is created" << endl;
}
void SavingsAccount::record(const Date &date, double amount,const string &desc)
{
accumulation = accumulate(date);
lastDate = date;
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
total += amount;
date.show();
cout << "\t#" << id << "\t" << amount << "\t" << balance <<"\t"<<desc<< endl;
}
void SavingsAccount::settle(const Date &date)
{
double interest = accumulate(date) * rate / 365;
if (interest != 0)
{
record(date, interest,"结算利息");
}
else
{
accumulation = 0;
}
}
void SavingsAccount::show()
{
cout << "#" << id << "\tBalance:" << balance << endl;
}
void SavingsAccount::deposit(const Date &date, double amount,const string &desc)
{
record(date, amount,desc);
}
void SavingsAccount::withdraw(const Date &date, double amount, const string &desc)
{
if (amount > getBalance())
{
error("Error, not enough money");
}
else
{
record(date, -amount,desc);
}
}
void SavingsAccount::error(const string& msg)
{
cout << msg << endl;
exit(1);
}
最后是主函数(main.cpp)
#include"SavingsAccount.h"
#include<iostream>
using namespace std;
int main()
{
Date date(2008, 11, 1);
//建立几个账户
SavingsAccount accounts[] =
{
SavingsAccount(date, "S3755217", 0.015),
SavingsAccount(date, "02342342", 0.015)
};
const int n = sizeof(accounts) / sizeof(SavingsAccount);//账户总数
//11月份的几笔账目
accounts[0].deposit(Date(2008,11,5), 5000,"Salary");
accounts[1].deposit(Date(2008, 11,25), 10000,"sell stock 0323");
//12月份的几笔账目
accounts[0].deposit(Date(2008,12,5), 5500,"Salary");
accounts[1].withdraw(Date(2008,12,20), 4000,"buy a laptop");
//结算所有账户并输出各个账户信息
cout << endl;
for (int i = 0; i < n; i++)
{
accounts[i].settle(Date(2009, 1, 1));
accounts[i].show();
cout << endl;
}
//输出各个账户的信息
cout << "Total:" << SavingsAccount::gettotal() << endl;
return 0;
}
输出结果如下: