个人银行账户管理程序(2)

在(1)的基础上进行改进

1:增加一个静态成员函数total,记录账户总金额和静态成员函数getTotal

2对不需要改变的对象进行const修饰

3多文件实现

account。h文件

#ifndef _ACCOUNT_
#define _ACCOUNT_
class SavingAccount {
	private:
		int id;
		double rate;
		double balance;
		int lastdate;
		double accumulation;
		static double total;
		void record(int date, double amount);
		double accumulate(int date)const {
			return accumulation + balance * (date - lastdate);
		}
public:
	SavingAccount(int date, int id, double rate);
	int getid()const { return id; }
	double getbalance()const { return balance; }
	double getrate()const { return rate; }
	static double getTotal() { return total; }
	void deposit(int date, double amount);
	void withdraw(int date, double amount);
	void settle(int date);
	void show()const;
};
#endif

account。cpp文件

#include"account.h"
#include<cmath>
#include<iostream>
using namespace std;
double SavingAccount::total = 0;
SavingAccount::SavingAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastdate(date)
, accumulation(0) {
	cout << date << "\t#" << id << "    " << endl;
}
void SavingAccount::record(int date, double amount) {
	accumulation = accumulate(date);
	lastdate = date;
	amount = floor(amount * 100 + 0.5) / 100;
	balance += amount;
	total += amount;
	cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingAccount::deposit(int date, double amount) {
	record(date, amount);
}
void SavingAccount::withdraw(int date, double amount) {
	if (amount > getbalance())
		cout << "余额不足" << endl;
	else
		record(date, -amount);
}
void SavingAccount::settle(int date) {
	double interest = accumulate(date) * rate / 365;
	if (interest != 0)
		record(date, interest);
	accumulation = 0;
}
void SavingAccount::show()const {
	cout << "#" << id << "\tbalance:" << balance;
}

1.cpp

#include"account.h"
#include<iostream>
using namespace std;
int main() {
	SavingAccount s1(1, 21325302, 0.0015);
	SavingAccount s2(1, 21325303, 0.0015);

	s1.deposit(5, 5000);
	s2.deposit(25, 10000);
	s1.deposit(45, 5500);
	s2.withdraw(60, 4000);
	
	s1.settle(90);
	s2.settle(90);

	s1.show();
	s2.show();

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值