SavingAccount.cpp
#include"SavingAccount.h" #include<cmath> #include<iostream> using namespace std; double SavingAccount::total = 0; //SavingAccount类相关成员函数的实现 SavingAccount::SavingAccount(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 SavingAccount::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 SavingAccount::error(const string& msg) const { cout << "Error(#" << id << ")" << msg << endl; } void SavingAccount::deposit(const Date &date, double amount,const string &desc) { record(date, amount,desc); } void SavingAccount::withdraw(const Date& date, double amount, const string& desc) { if (amount > getBalance()) error("not enough money"); else record(date, -amount,desc); } void SavingAccount::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 SavingAccount::show() const { cout << "#" << id << "\tBalance:" << balance << endl; }SavingAccount.h
#ifndef _ACCOUNT_H_ #define _ACCOUNT_H #include"Date.h" #include<string> class SavingAccount { private: std::string id; double balance; //余额 double rate; //存款年利率 Date lastDate; //上次变更余额的日期 double accumulation; //余额按日累加之和 static double total; //获得到指定日期为止的存款金额按日累计值 double accumulate(const Date &date)const { return accumulation + balance * date.distance(lastDate); } //记录一笔账,date为日期,amount为金额 void record(const Date &date, double amount,const std::string &desc); //报告错误信息 void error(const std::string &msg)const; public: SavingAccount(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 show()const; //存入现金 void deposit(const Date &date, double amount,const std::string &desc); //取出现金 void withdraw(const Date &date, double amount,const std::string& desc); //结算利息,每年1月1日调用一次该函数 void settle(const Date &date); }; #endif //_ACCOUNT_HDate.h
#pragma once 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; } };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(); }主函数
#include"SavingAccount.h" #include<iostream> using namespace std; /* int main() { //建立几个账户 SavingAccount sa0(1, 213245125, 0.015); SavingAccount sa1(2, 987293857, 0.015); //几笔账目 sa0.deposit(5, 5000); sa1.deposit(25, 10000); sa0.deposit(45, 5500); sa1.withdraw(60, 4000); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个账户信息 sa0.show(); sa1.show(); cout << "Total:" << SavingAccount::getTotal() << endl; return 0; } */ int main() { Date date(2008, 11, 1); //建立几个账户 SavingAccount accounts[] = { SavingAccount(date,"S3755217",0.015), SavingAccount(date,"02342342",0.015) }; const int n = sizeof(accounts) / sizeof(SavingAccount);//账户总数 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 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:" << SavingAccount::getTotal() << endl; return 0; }