要求:
一个人可以有多个活期存储账户,一个活期存储账户包括账号(id)、余额(balance)、年利率(rate)等信息,还包括显示账户信息(show)、存款(deposit)、取款(withdraw)、结算利息(settle)等操作,为此,设计一个类SavingsAccount,将id、balance、rate均作为其成员数据,将deposit、withdraw、settle均作为其成员函数。
实现该类的难点在于利息的计算。由于账户的余额是不断变化的,因此不能通过余额与年利率相乘的办法来计算年利,而是需要将一年当中每天的余额累积起来再除以一年的总天数,得到一个日均余额,再乘以年利率。为了计算余额的按日累计值,SavingsAccount引入了私有数据成员lastDate、accumulation和私有成员函数accumulate。lastDate用来存储上一次余额变动的日期,accumulation用来存储上次计算利息后直到最近一次余额变动时余额按日累加的值,accumulate成员函数用来计算截止至指定日期的账户余额按日累计值。这样,当余额变动时,需要做的是将变动前的余额与该余额所持续的天数相乘,累加得到accumulation中,再修改lastDate。
account.h、account.cpp:
#pragma once
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include"date.h" //引入date类
#include<string>
class SavingsAccount //定义银行账户类
{
private:
std::string id; //由于开头没有用using namespace,因此这里要用std标准库
double balance; //余额
double rate; //利率
Date lastDate; //上一次变动日期
double accumulation; //累计余额
static double total; //所有账户累计余额
void record(const Date& date, double amount, const std::string& desc); /*记录一笔账,date为日期,amount为金额,desc为说明*/
void error(const std::string& msg) const; //报告错误信息
double accumulate(const Date& date) const //获得到指定日期为止的存款金额按日累计值
{
return accumulation + balance * date.distance(lastDate);
}
public:
SavingsAccount(const Date& date, const std::string& id, double rate); //构造函数
const std::string& getId() const { return id; } //获取ID
double getBalance() const { return balance; } //获取余额
double getRate() const { return rate; } //获取利率
static double getTotal() { return total; } //获取总余额
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); //结算利率
void show() const; //显示账户信息
};
#endif
#include"account.h" //引入account类
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total = 0; //初始化total为0
SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :
id(id), balance(0), rate(rate), lastDate(date), accumulation(0) //构造函数的具体实现
{
date.show();
cout << "\t#" << id << " 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; //通过floor函数进行保留小数点两位的四舍五入
balance += amount;
total += amount;
date.show();
cout << "\t#" << id << "\t" << amount << balance << "\t" << desc << endl;
}
void SavingsAccount::error(const string& msg) const //报告错误函数
{
cout << "Error(#" << id << "):" << msg << 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("not enough money!");
else
record(date, -amount, desc); //根据比较输出不同结果
}
void SavingsAccount::settle(const Date& date) //利息结算
{
double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
accumulation = 0;
}
void SavingsAccount::show() const //输出账户信息
{
cout << id << "\tBalance:" << balance;
}
日期的表示:
日期可以用一个类来表示,内含年、月、日三个数据成员。
为了计算日期间相差天数,可以先选取一个比较规整的基准日期,在构造日期对象时将该日期到这个基准日期的相对天数计算出来,我们将这个相对天数称为“相对日期”。这样在计算两个日期相差的天数时,只需将两者的相对日期相减即可。我们将公元元年1月1日作为公共日期的基准日期,将y年m月d日相距这一天的天数记为f(y/m/d,1/1/1),可以将其分解为3部分:
f(y/m/d)=f(y/1/1,1/1/1)+f(y/m/1,y/1/1)+f(y/m/d,y/m/1)
上面等式右边的第一项表示当年的1月1日与公元元年1月1日相距的天数,即公元元年到公元(y-1)年的总天数。平年每年有365天,闰年多一天,因此该值为365(y-1)加上公元元年到(y-1)年之间的闰年数,由于4年一闰,100的倍数免闰,400的倍数再闰,故有:
其中表示对x向下取整。f(y/m/1,y/1/1)表示y年的m月1日与1月1日相距的天数。由于每月的天数不同,因此这难以表示为一个统一的公式。好在各平年中指定月份的1日与1月1日的相差天数可以由月份m唯一确定,因此可以把每月1日到1月1日的天数放在一个数组中,计算时只要查询该数组,便可得到f(y/m/1,y/1/1)的值。而对于闰年,仍可通过数组查询,只需在m>2时将查得的值加1.该值只依赖于x和y,将它记为g(x,y)。此外:
如果把公元元年1月1日的相对日期定为1,那么公元y年m月d日的相对日期就是:
相对日期得出后,计算两日期相差天数的难题就迎刃而解了。
Date.h:
#ifndef __DATE_H__
#define __DATE_H__
class Date //日期类
{
private:
int year; //年
int month; //月
int day; //日
int totalDays; //该日期是从公元元年1月1日开始的第几天
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 //判断当年是否为闰年
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
void show() const; //输出当前日期
int distance(const Date& date) const //计算两个日期之间是多少天
{
return totalDays - date.totalDays;
}
};
#endif //__DATE_H__
Date.cpp:
#include"date.h"
#include<iostream>
#include<cstdlib>
using namespace std;
namespace //namespace使下面的定义只在当前文件中有效
{ //存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}
Date::Date(int year, int month, int day) :year(year), month(month), day(day)
{
if (day <= 0 || day > getMaxDay())
{
cout << "Invalid date:";
show();
cout << endl;
exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const
{
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
void Date::show()const
{
cout << getYear() << "-" << getMonth() << "-" << getDay();
}
main.cpp:
#include<iostream>
#include"account.h"
using namespace std;
int main()
{
Date date(2008, 11, 1); //起始日期
SavingsAccount accounts[] =
{
SavingsAccount(date,"03755217",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;
}
运行结果: