个人银行账户管理程序
1 #include<iostream> 2 #include <cmath> 3 using namespace std; 4 class SavingsAccount{ //储蓄账户类 5 private: 6 int id; //账号 7 double balance; //余额 8 double rate; //存款的年利率 9 int lastDate; //上次变更余额的时期 10 double accumulation; //余额按日累加之和 11 //记录一笔账,date为日期,amount为金额,desc为说明 12 void record(int date,double amount); 13 //获得到指定日期为止的存款金额按日累计值 14 double accumulate(int date)const{ 15 return accumulation+balance*(date-lastDate); 16 } 17 public: 18 //构造函数 19 SavingsAccount(int date,int id,double rate); 20 int getId(){ 21 return id; 22 } 23 double getBalance(){ 24 return balance; 25 } 26 double getRate(){ 27 return rate; 28 } 29 void deposit(int date,double amount); //存入现金 30 void withdraw(int date,double amount); //取出现金 31 //结算利息,每年一月一日调用一次该函数 32 void settle(int date); 33 //显示账户信息 34 void shouw(); 35 }; 36 //SavingsAccount类相关成员函数的实现 37 SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 38 cout<<date<<"\t#"<<id<<"is created"<<endl; 39 } 40 void SavingsAccount::record(int date,double amount){ 41 accumulation=accumulate(date); 42 lastDate=date; 43 amount=floor(amount*100+0.5)/100; 44 balance+=amount; 45 cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; 46 } 47 void SavingsAccount::deposit(int date,double amount){ 48 record(date,amount); 49 } 50 void SavingsAccount::withdraw(int date,double amount){ 51 if(amount>getBalance()) 52 cout<<"error:not enough money"<<endl; 53 else 54 record(date,-amount); 55 } 56 void SavingsAccount::settle(int date){ 57 double interest=accumulate(date)*rate/365; //计算年息 58 if(interest!=0) 59 record(date,interest); 60 accumulation=0; 61 } 62 void SavingsAccount::shouw(){ 63 cout<<"#"<<id<<"\tBalance: "<<balance<<endl; 64 } 65 int main() 66 { 67 //建立几个账户 68 SavingsAccount sa0(1,21325302,0.015); 69 SavingsAccount sa1(1,58320212,0.015); 70 //几笔账目 71 sa0.deposit(5,5000); 72 sa1.deposit(25,10000); 73 sa0.deposit(45,5500); 74 sa1.withdraw(60,4000); 75 //开户后第九十天到了银行的计息日,结算所有账户的年息 76 sa0.settle(90); 77 sa1.settle(90); 78 //输出各个账户的信息 79 sa0.shouw(); 80 cout<<endl; 81 sa1.shouw(); 82 cout<<endl; 83 return 0; 84 }
标签:SavingsAccount,int,double,C++,rate,课本,date,例题,id From: https://www.cnblogs.com/Lyh3012648079/p/17343602.html