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

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

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

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

3多文件实现

account。h文件

cpp 复制代码
#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文件

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

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();

}
相关推荐
二狗哈3 分钟前
Cesium快速入门22:fabric自定义着色器
运维·开发语言·前端·webgl·fabric·cesium·着色器
计算衎4 分钟前
FastAPI后端和VUE前端的数据交互原理详解
前端·vue.js·fastapi
黑岚樱梦4 分钟前
Linux系统编程
java·开发语言·前端
乌萨奇也要立志学C++5 分钟前
【洛谷】贪心专题之推公式 用 “相邻元素交换” 推导最优规则
c++·算法
我只会发热5 分钟前
超详细的 idea 类注释、方法注释、行注释配置(图文详解)
java·intellij-idea
xrl20126 分钟前
ruoyi-vue2前端集成DMN规则引擎
前端·规则引擎·工作流·dmn
转转技术团队6 分钟前
前端工程化实践:打包工具的选择与思考
前端·javascript·webpack
姜西西_9 分钟前
自动化测试框架pytest之fixture
android·java·pytest
前端郭德纲14 分钟前
React 19.2 已发布,现已上线 npm!
前端·react.js·npm
鸽鸽程序猿14 分钟前
【项目】【抽奖系统】查询中奖记录
java·spring