# 银行账户管理系统（一）

42 篇文章 0 订阅

#include <iostream>
#include<cmath>
using namespace std;
class SavingAccount{
private:
int id;             //账户
double balance;       //余额
double rate;          //年利率
int lastdate;         //余额上一次变化的日期
double accumulation;  //余额按日累加之和
int accumulate(int date); //获得到指定日期位置的存款累计值
double getbalance(){return balance;}
double getrate(){return rate;}
void record(int date,double amount);    //记账
public:
SavingAccount(int date,int id,double rate);    //构造函数
void deposit(int date,double amount);   //存款
void withdrawl(int date,double amount);       //取款
void show();   //显示余额
void settle(int date);  //结算利息，每年一月一日调用此函数
};
//类的成员函数的实现
SavingAccount::SavingAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastdate(date),accumulation(0){
cout<<"#"<<id<<"is created"<<endl;
}
int SavingAccount::accumulate(int date){
return accumulation+balance*(date-lastdate);
}
void SavingAccount::record(int date,double amount){
lastdate=date;
amount=floor(100*amount+0.5)/100;   //floor为向下取整函数，此处可以做到保留两位小数
balance+=amount;
cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void SavingAccount::deposit(int date,double amount){
record(date,amount);
}
void SavingAccount::withdrawl(int date, double amount){
if(amount>balance)
cout<<"not enough money"<<endl;
else
record(date,-amount);
}
void SavingAccount::show(){
cout<<"#"<<id<<"\tBalance"<<balance<<endl;
}
void SavingAccount::settle(int date){
double interest=accumulate(date)*rate/365; //计算年息
if(interest!=0)
record(date,interest);
accumulation=0;
}

int main()
{
SavingAccount s1(0,11111,0.015);
SavingAccount s2(1,22222,0.015);
s1.deposit(5,5000);
s2.deposit(25,10000);
s1.deposit(45,5500);
s2.withdrawl(60,11000);
s1.settle(90);
s2.settle(90);
s1.show();
s2.show();

return 0;
}



• 6
点赞
• 0
评论
• 6
收藏
• 一键三连
• 扫一扫，分享海报

11-13 4986