"Seventh chapter" personal Bank Account management procedure case Realization
Account.cpp #include "account.h" #include <cmath> #include <iostream>using namespace std;double account:: Total = Implementation of 0;//account class-related member functions Account::account (const Date &date, const string &id): ID (ID), balance (0) {Date.show (); cout << "\t#" << ID << "created" << Endl;} void Account::record (const Date &date, double amount, const string &desc) {amount = floor (amount * 100 + 0.5)/100 ; Reserved decimal two-bit balance + = Amount;total + amount;date.show (); cout << "\t#" << ID << "\ t" << Amount < < "\ t" << balance << "\ t" << desc << Endl;} void Account::show () const{cout << ID << "\tbalance:" << balance;} void Account::error (const string &msg) Const{cout << "error (#" << ID << "):" << msg << en DL;} Implementation of SavingsAccount class-related member functions Savingsaccount::savingsaccount (const Date &date, const string &id, double rate): Account (Date,id), Rate, ACC (date,0) {}void SaviNgsaccount::d eposit (const date &date, double amount, const string &desc) {record (Date, amount, desc); Acc.change ( Date, getbalance ());} void Savingsaccount::withdraw (const Date &date, double amount, const string &desc) {if (Amount > GetBalance ()) { Error ("Not enough money");} else {record (date,-amount, desc); Acc.change (date, getbalance ());}} void Savingsaccount::settle (const date &date) {Double interest = acc.getsum (date) *rate/date.distance (date ( Date.getyear ()-1, 1, 1); Calculates the annual interest if (interest! = 0) record (date, interest, "interest"), Acc.reset (Date,getbalance ()); Creditaccount::creditaccount (const Date &date, const std::string &id, double credit, double rate, double fee): ACC Ount (date, id), credits, fee (fee), rate, ACC (date, 0) {}void creditaccount::d eposit (const date &date, do Uble amount, const string &desc) {record (date, amount, desc); Acc.change (date, getdebt ());} void Creditaccount::withdraw (const Date &date, double amount, const string &DESC) {if (Amount-getbalance () > Credits) {error ("Not enough Credits");} else {record (date,-amount, desc); Acc.change (date, getdebt ());}} void Creditaccount::settle (const date &date) {Double interest = acc.getsum (date) *rate;if (Interest! = 0) record (date, Interest, "interest"), if (date.getmonth () = = 1) record (date,-fee, "annual fee"), Acc.reset (date, getbalance ());} void Creditaccount::show () const{account::show (); cout << "\tavailable credit:" << Getavailablecredit ();}
Account.h:defines the entry point for the console application. #ifndef _account_h_#define _account_h_#include "Date.h" # Include "Accumulator.h" #include <string>class account//Savings accounts class {Private:std::string id;//account double balance;// Balance static double total; The total amount of all accounts protected:account (const date&date, const std::string &id);//Record one account, date, desc as description, amount as amount void Record (const Date &date, double amount, const std::string &DESC);//Report error message void error (const std::string &msg) const;public://Constructor Const std::string &getid () const {return ID;} Double GetBalance () const {return balance;} Static double Gettotal () {return total;} void Show () const;//displays account information};class savingsaccount:public account{private:accumulator acc;double rate;public: SavingsAccount (const Date &date, const std::string &id, double rate);d ouble getrate () const {return rate;} void Deposit (const Date &date, double amount, const std::string &DESC); Deposit cash void Withdraw (const Date &date, double AMount, const std::string &DESC); Remove cash void Settle (const Date &date);//settlement interest, called once every January 1 of the function};class Creditaccount:p ublic 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);d ouble GETCR Edit () const {return credit;} Double getrate () const {return rate;} Double Getfee () const {return fee;} Double Getavailablecredit () const {if (GetBalance () < 0) return credit + GetBalance (); Elsereturn credits;} void Deposit (const Date &date, double amount, const std::string &DESC); Deposit cash void Withdraw (const Date &date, double amount, const std::string &DESC); Remove cash void Settle (const Date &date);//settlement interest, called once every January 1 of the function Void Show () const;//display account information}; #endif//_account_h_
Accumulator.h #ifndef _accumulator_h_#define _accumulator_h_#include "Date.h" class Accumulator {Private:date Lastdate;double value;double sum;public:accumulator (const date&date,double value): Lastdate (Date), value (value), SUM (0) {}double getsum (const Date &date) Const {return sum + value*date.distance (lastdate);} 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//_accumulator_h_
Chapter07 bank account. Cpp:defines the entry point for the console application.////chapter06.cpp:Defines the entry point fo R the console application.//7_10.cpp//#include "account.h" #include "account.h" #include "date.h" #include <iostream >using namespace Std;int Main () {date Date (2008, 11, 1);//Establish several accounts SavingsAccount SA1 (date, "03755217", 0.015); SavingsAccount SA2 (date, "02342342", 0.015); Creditaccount CA (date, "C5392394", 10000, 0.0005, 50); A few accounts in November (Date (Sa1.deposit, 5), "salary"), Ca.withdraw (date (one, one, and a), and "buy a phone"); Sa2.deposit (Date (2008, 12, 1)), 10000, "Sell stock 0323"),//settlement of credit card Ca.settle (date (),//12 month), Ca.deposit ( Date (+, 1), "repay the credit"), Sa1.deposit (Date (, 1), 5500, "salary");//Settle all accounts and export individual account information Sa1.settle (Date (1, 1)), Sa2.settle (Date (1, 1)), Ca.settle (Date (, 1, 1)), cout << endl;sa1.show (); cout << Endl;sa2.show (); cout << endl;ca.show (); cout << endl;cout << "Total: "<< account::gettotal () << Endl;return 0;}
Date.cpp#include "date.h" #include <iostream> #include <cstdlib>using namespace std;namespace {const int Days_before_month[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};} Date::D ate (int year, Int. month, int day): Year (year), month (month), Day (day) {if (Day <= 0 | | Day > Getmaxday ()) {Co UT << "Invalid date:"; show (); cout << endl;exit (1);} int years = Year-1;totaldays = year * 365 + YEARS/4-years/100 + years/400 + days_before_month[month-1] + day;i F (isleapyear () && month > 2) totaldays++;} int Date::getmaxday () const {if (Isleapyear () && month = = 2) return 29;elsereturn Days_before_month[month]-days_b EFORE_MONTH[MONTH-1];} void Date::show () const {cout << getYear () << "-" << getMonth () << "-" << GetDay ();}
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%!! = 0 | | year% 400 = 0;} void Show () const;int distance (const Date &date) const {return totaldays-date.totaldays;}}; #endif//!_date_h_
Ps:
Supporting materials: PhD "C + + programming language"
Course: the Academy Online "C + + programming language"
"C + + Chapter seventh personal bank Account management procedures case"