C++语言程序设计第四版例7-10个人银行账户管理程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//Date.h
#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{
return year%4==0&&year%100!=0||year%400==0;
}
void show() const;
//计算两个日期之间差多少天
int distance(const Date& date) const{
return totalDays-date.totalDays;
}
};
#endif
//Accumulator.h
#ifndef _ACCUMULATOR_H_
#define _ACCUMULATOR_H_
#include"Date.h"
class Accumulator{
private:
Date lastDate;
double value;
double sum;
public:
//构造函数,date开始累加的日期,value为初始值
Accumulator(const Date &date,double value):lastDate(date),value(value),sum(0){} //获得日期date的累加结果
double getSum(const Date &date) const{
return sum+value*date.distance(lastDate);
}
//在date将数值变更为value
void change(const Date &date,double value){
sum=getSum(date);
lastDate=date;this->value=value;
}
void reset(const Date &date,double value){
lastDate=date;
this->value=value;
sum=0;
}
};
#endif
//Accout.h
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include"Date.h"
#include"Accumulator.h"
#include
class Account{
private:
std::string id;
double balance;
static double total;
protected:
//供派生类调用的构造函数,id为账户
Account(const Date &date,const std::string &id);
//记录一笔括账,date为日期,amount为金额,desc为说明
void record(const Date &date,double amount,const std::string &desc);
//报告错误信息
void error(const std::string &msg) const;
public:
const std::string &getId() const{return id;}
double getBalance() const{return balance;}
static double getTotal(){return total;}
//显示账户信息
void show() const;
};
class SavingsAccount:public Account{
private:
Accumulator acc;
//double credit;
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);
};
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(){return fee;}
double getAvailableCredit() const{
if(getBalance()<0)
return credit+getBalance();
else