一个活期储蓄账户包括:
信息:账号(id)、余额(balance)、年利率(rate)等
操作:显示账户信息(show)、存款(deposit)、取款(withdraw)、结算利率(settle)等。
为此,我们定义了一个SavingAccount类:一开始id和日期date都是使用简单的int类型,后面会修改。
在UML建模中,“+”表示成员是共有的,“-”表示成员是私有的
无论是存款,取款还是结算利息,都需要修改当前的余额并且将余额的变动输出。我么定义一个record私有成员来执行。所有的日期都是以日为单位的相对日期,这样的话,通过两个日期相减既可以得到两个日期的相差天数,在计算利息时很方便。
难点1、利息的计算。因为余额是变化的。
那么,现在的类结构如下图所示:
版本一的不足之处:
改进1:
为了解决其中的一部分问题,为SavingAccount类增加一个静态数据成员total:double,其好处是在于全体账户对象共享一份,技能节省存储空间,有无须担心数据一致性问题。
另外,将不需要改变的成员函数,生命为长成员函数,用const修饰。
相应的,添加静态成员函数getTotal()函数来访问total成员。
改进2:
日期使用整数虽然方便,但是不直观也不友好。所以我们可以使用一个类来表示日期,内涵年月日三个数据成员。但是这就给日期的计算带来了不变。最后,我们使用了“相对日期”来计算两个日期的相差天数,只需要将两个日期的相对日期相减就可以了。
将公元元年1月1日定为公共的基准日期,将y年m月d日相聚这个基准日期记作f(y/m/d,1/1/1)可以将其分解为三部分:
f(y/m/d.1/1/1)=f(y/1/1,1/1/1) + f(y/m/1,y/1/1) + f(y/m/d,y/m/1)
其中的第一部分:f(y/1/1,1/1/1),元年每年365天,元年数加上闰年数。闰年数的计算:4年一闰,100的倍数免闰,400的倍数再闰。
所以:f(y/1/1,1/1/1)=(y-1)*365 + (y-1)/4 - (y-1)/100 + (y-1)/400
最简单的是f(y/m/d,y/m/1) = (d-1)
中间部分比较难以表示为一个统一的公式,但是平年中指定月份的1日与1月1日相差天数可以由月份唯一确定,所以可以把每月1日到1月1日的天数放在一个数组中,计算式只需要查询该数组,便可得到f(y/m/1,1/1/1).而对于闰年,只需要将m>2时差的的值加上1,该值只依赖m和y,我们把它记成g(m,y)。
将公元元年1月1日的相对日期定为i,则y年m月d日的相对日期就是:
f(y/m/d,1/1/1)=(y-1)365 + (y-1)/4 - (y-1)/100 + (y-1)/400 +g(m,y) + d
Date类:
改进剩余部分:用整数表示银行账号不完美,比如有时账号以0开头,或者账号超过整数的表示范围或者账号中有其他字符串,所以整数的表示方式不能胜任,我们将账号类型改为字符串形式。
账目列表使用字符串为各笔账目增加说明性文字
专门增加一个函数用于报告错误信息,当其他函数需要输出错误信息时,直接把信息以字符串形式传递给该函数即可,简化了错误信息的输出。
利用数组避免代码冗余:之前创建账户时,两个账户都是独立的变量,只能用名字取引用他们,主程序末尾分别对两个账户进行结算(settle)和显示(show)时需要将几乎相同的代码书写两遍,如果账户数量增多将会带来更多的麻烦。所以可以将多个账户组织在一个数组中,这样可以把需要对各个账户做得事情放在循环中,避免了代码的冗余。
修改后的代码:
//date.h
using namespace std;
//日期类
class Date
{
public:
Date(int year, int month, int day);
int getYear() const{ return year; }//const成员函数(位置在函数之后),不能修改
int getMonth() const{ return month; }
int getDay() const { return day; }
int getMaxDay() const;//获得当月有多少天
bool isLeapyYear() const{//判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
void show() const;//输出当前日期
//计算两个日期之间差多少天
int distance(const Date& date) const {
return totalDays - date.totalDays;
}
private:
int year;
int month;
int day;
int totalDays;//该日期是从公元1月1日开始的第几天
};
//date.cpp
#include<iostream>
#include<cmath>
#ifndef _DATA_H
#define _DATA_H
#include"date.h"
#endif // !_DATA_H
#include<cstdlib>//exit函数,该函数的声明在cstdlib头文件中
using namespace std;
namespace {//namespace使下面的定义只在当前文件中有效
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 (isLeapyYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const{
if (isLeapyYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
void Date::show() const {
cout << getYear() << "-" << getMonth() << "-" << getDay();
}
//account.h
#ifndef _DATA_H
#define _DATA_H
#include"date.h"
#endif // _DATA_H
#pragma once
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
class SavingAcount//储蓄账户类
{
private:
string id;//账号
Date lastDate;//上次变更日期
double balance;//余额
double accumulation;//余额按照日累加之和
double rate;//利率
static double total;
//记每一笔账,date为日期,amount为金额,desc为说明
void record(const Date &date, double amount, const string &desc);
//获得到指定日期为止的存款金额按日累计值
double accumulate(const Date& date)const {
return accumulation + balance * date.distance(lastDate);
}
public:
//构造函数
SavingAcount(const Date& date,const string& id, double rate);
~SavingAcount();
string getId() const { return id; }
double getBalance() const { return balance; }
double getRate() const { return rate; }
static double getTotal() { return total; }
//显示账户信息
void show() const;
//存入现金
void deposit(const Date& date, double amount,const string &desc);
//取出现金
void withdraw(const Date &date, double amount, const string &desc);
void error(const string & msg);
//结算利息
void settle(const Date& date);
};
//account.cpp
#include<iostream>
#include<cmath>
#ifndef _ACCOUNT_H
#define _ACCOUNT_H
#include"account.h"
#endif // !_ACCOUNT_H
using namespace std;
double SavingAcount::total = 0;
//SavingsAccount类相关成员函数的实现
SavingAcount::SavingAcount(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;
}
SavingAcount::~SavingAcount()
{
}
void SavingAcount::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 SavingAcount::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 SavingAcount::deposit(const Date &date, double amount,const string &desc) {
record(date, amount, desc);
}
void SavingAcount::withdraw(const Date &date, double amount, const string &desc) {
if (amount > getBalance())
cout << "Error: not enough money" << endl;
else
record(date, -amount, desc);
}
void SavingAcount::show() const{
cout << "#" << id << "\tBalance:" << balance;
}
void SavingAcount::error(const string &msg) {
cout << "Error(#" << id << "): " << msg << endl;
}
//main.cpp
#include<iostream>
#include<cmath>
#ifndef _ACCOUNT_H
#define _ACCOUNT_H
#include"account.h"
#endif // !_ACCOUNT_H
using namespace std;
int main() {
Date date(2018, 9, 1); //起始日期
//建立几个账户
SavingAcount accounts[] = {
SavingAcount(date, "S3755217", 0.015),
SavingAcount(date, "02342342", 0.015)
};
const int n = sizeof(accounts) / sizeof(SavingAcount); //账户总数
//11月份的几笔账目
accounts[0].deposit(Date(2018, 11, 5), 5000, "salary");
accounts[1].deposit(Date(2018, 11, 25), 10000, "sell stock 0323");
//12月份的几笔账目
accounts[0].deposit(Date(2018, 12, 5), 5500, "salary");
accounts[1].withdraw(Date(2018, 12, 20), 4000, "buy a laptop");
//结算所有账户并输出各个账户信息
cout << endl;
for (int i = 0; i < n; i++) {
accounts[i].settle(Date(2019, 1, 1));
accounts[i].show();
cout << endl;
}
cout << "Total: " << SavingAcount::getTotal() << endl;
return 0;
}
另外,还可以对程序进行扩展,以支持信用卡用户。
*************后续再补充*************
*************2018/9/26**************
*****加入了继承和派生的概念*****
**************************************
信用卡账户业务特点:
允许透支及信用额度:总的透支金额应该在这个额度之内
利息:信用账户中存钱不会有利息,二使用信用账户透支只需要支付利息,信用账户的利率一般以日为单位,为了简单起见,我们不去考虑这个免息期。
每月结算:与储蓄账户每年结算一次利息不同,信用账户每月结算一次,我么假定结算日是每月的1日
年费:此外,信用账户每年需要交一次年费,在本例中我们认为在每年的1月1日结算的时候扣缴年费。
结构调整:
设计一个基类Account用来描述所有账户的共性,再从中派生出:SavingAccount类和CreditAccount类
两类账户的利息计算有很大的差异:无论是计息的对象还是计息的周期。因此,计息任务也不能由基类Account来完成。
然而,两类账户在计算利息时都需要将某个数值(余额或者欠款金额)按日累加,为了避免书写重复的代码,有两种可行的解决方法:
- 在基类Account中设立几个额保护的成员函数来协助利息计算,然后在派生类中通过调用这些函数来计算利息。
- 建立一个新的类,由该类提供计算一项数值的按日累加之和所需要的接口,在两个派生类中分别将该类实例化,通过该类的实力来计算利息。
由于计算一项数值的按日累加之和这个功能是与其他的账户管理功能相对独立的,因此将这项功能从账户类中分离出来更好。这样可以降低账户类的复杂性,提高计算数值的按日累加之和代码的可复用性。
以下是相应的UML结构:
以下是代码:(明天码码看能不能搞出来)