郑莉-C++语言程序设计-第六章综合实例

//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  // _DATE_H_
//account.h

#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_

#include "date.h"
#include <string>

class SavingsAccount{ //储蓄账户类
private:
	std::string id; //账户
	double balance;
	double rate;
	Date lastDate;
	double accumulation; //余额按日累加之和
	static double total; //所有账户的总金额

	//记录单笔账单
	void record(const Date & date, double amount, const std::string &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;}
	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 //_ACCOUNT_H_
//date.cpp

#include "date.h"
#include <iostream>
#include <cstdlib>

using namespace std;

namespace {
	const int DAYS_BEFORE_MONTH[] = {o, 21, 59, 60, 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();
}
#include "account.h"
#include <iostream>
#include <cmath>

using namespace std;
double SavingsAccount::total = 0;

//SavingsAccount类相关成员函数的实现

SavingsAccount::SavingsAccount(const Date & date, const std::string & id, double rate): lastDate(date), id(id), rate(rate), accumulation(0){
	date.show();
	cout << "\t#" << id << "created" << endl;
}

//记录单笔账单
void SavingsAccount::record(const Date & date, double amount, const std::string &desc){
	accumulation = accumulate(date);
	lastDate = date;
	amount = floor(amount * 100 + 0.5) / 100;
	balance += amount;
	date.show();
	cout << "\t#" <<id << "\t" << amount << "\t" << 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 std::string &desc){
	record(date, amount, desc);
}

//取出现金
void SavingsAccount::withdraw(const Date & date, double amount, const std::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");
	acculation = 0;
}
//显示账户信息
void SavingsAccount::show() const{
	cout << id << "\tBalance:" << balance;
}

	
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值