#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
#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
#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 ( 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;
}