您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 综合实例个人银行账户管理程序
_综合实例_个人银行账户管理程序//date.h#ifndef__DATE_H__#define__DATE_H__classDate{//日期类private:intyear;//年intmonth;//月intday;//日inttotalDays;//该日期是从公元年1月1日开始的第几天public:Date(intyear,intminth,intday);//用年、月、日构造日期intgetYear()const{returnyear;}intgetMonth()const{returnmonth;}intgetDay()const{returnday;}intgetMaxDay()const;//获得当月有多少天boolisLeapYear()const//判断当年是否为闰年{returnyear%4==0&&year%100!=0||year%400==0;}voidshow()const;//输入当前日期//计算两个日期之间差多少天intoperator-(constDate&date)const//计算两个日期之间差多少天{returntotalDays-date.totalDays;}};#endif//__DATE_H__//date.cpp#include"date.h"#include<iostream>#include<cstdlib>usingnamespacestd;namespace//namespace使下面的定义只在当前的文件中有效{//存储平年中的某个月1月之前有多少天,为便于getMaxDay函数的实现,该数组多出一项constintDAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365};}Date::Date(intyear,intmonth,intday):year(year),month(month),day(day){if(day<=0||day>getMaxDay()){cout<<"Invaliddate:";show();cout<<endl;exit(1);}intyears=year-1;totalDays=year*365+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day;if(isLeapYear()&&month>2)totalDays++;}intDate::getMaxDay()const{if(isLeapYear()&&month==2)return29;elsereturnDAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];}voidDate::show()const{cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();}//accout.h#ifndef__ACCOUNT_H__#define__ACCOUNT_H__#include"date.h"#include"accumulator.h"#include<string>classAccount//账户类{private:std::stringid;//帐号doublebalance;//余额staticdoubletotal;//所有账户的总金额protected://供派生类调用的构造函数,id为账户Account(constDate&date,conststd::string&id);//记录一笔帐,date为日期,amount为金额,desc为说明voidrecord(constDate&date,doubleamount,conststd::string&desc);//报告错误信息voiderror(conststd::string&msg)const;public:conststd::string&getId()const{returnid;}doublegetBalance()const{returnbalance;}staticdoublegetTotal(){returntotal;}//存入现金,date为日期,amount为金额,desc为款项说明virtualvoiddeposit(constDate&date,doubleamount,conststd::string&desc)=0;//取出现金,date为日期,amount为金额,desc为款项说明virtualvoidwithdraw(constDate&date,doubleamount,conststd::string&desc)=0;//结算(计算利息、年费等),每月结算一次,date为结算日期virtualvoidsettle(constDate&date)=0;//显示账户信息virtualvoidshow()const;};classSavingsAccount:publicAccount//储蓄账户类{private:Accumulatoracc;//辅助计算利息的累加器doublerate;//存款的年利率public://构造函数SavingsAccount(constDate&date,conststd::string&id,doublerate);doublegetRate()const{returnrate;}voiddeposit(constDate&date,doubleamount,conststd::string&desc);//存入现金voidwithdraw(constDate&date,doubleamount,conststd::string&desc);//取出现金voidsettle(constDate&date);//结算利息,每年1月1日调用一次该函数voidshow()const;//显示账户信息};classCreditAccount:publicAccount//信用账户类{private:Accumulatoracc;//辅助计算利息的累加器doublecredit;//信用额度doublerate;//欠款的日利率doublefee;//信用卡年费doublegetDebt()const//获得欠款额{doublebalance=getBalance();return(balance<0?balance:0);}public://构造函数CreditAccount(constDate&date,conststd::string&id,doublecredit,doublerate,doublefee);doublegetCredit()const{returncredit;}doublegetRate()const{returnrate;}doublegetFee()const{returnfee;}doublegetAvailableCredit()const//获得可用信用额度{if(getBalance()<0)returncredit+getBalance();elsereturncredit;}voiddeposit(constDate&date,doubleamount,conststd::string&desc);//存入现金voidwithdraw(constDate&date,doubleamount,conststd::string&desc);//取出现金voidsettle(constDate&date);//结算利息,每年1月1日调用一次该函数voidshow()const;//显示账户信息};#endif//__ACOUNT_H__//account.cpp#include"account.h"#include<cmath>#include<iostream>usingnamespacestd;doubleAccount::total=0;//Accout类的实现Account::Account(constDate&date,conststring&id):id(id),balance(0){date.show();cout<<"\t#"<<id<<"created"<<endl;}voidAccount::record(constDate&date,doubleamount,conststring&desc){amount=floor(amount*100+0.5)/100;//保留小数点后两位balance+=amount;total+=amount;date.show();cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;}voidAccount::show()const{cout<<id<<"\tBalance:"<<balance;}voidAccount::error(conststring&msg)const{cout<<"Error(#"<<id<<"):"<<msg<<endl;}//SavingsAccount类相关成员函数的实现SavingsAccount::SavingsAccount(constDate&date,conststring&id,doublerate):Account(date,id),rate(rate),acc(date,0){}voidSavingsAccount::deposit(constDate&date,doubleamount,conststring&desc){record(date,amount,desc);acc.change(date,getBalance());}voidSavingsAccount::withdraw(constDate&date,doubleamount,conststring&desc){if(amount>getBalance())error("notenoughmoney");elserecord(date,-amount,desc);acc.change(date,getBalance());}voidSavingsAccount::settle(constDate&date){doubleinterest=acc.getSum(date)*rate/(date-Date(date.getYear()-1,1,1));//计算年利息if(interest!=0)record(date,interest
本文标题:综合实例个人银行账户管理程序
链接地址:https://www.777doc.com/doc-2284714 .html