个人银行账户管理程序(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();

}
相关推荐
阿冲Runner1 分钟前
创建一个生产可用的线程池
java·后端
YeeWang2 分钟前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript
写bug写bug10 分钟前
你真的会用枚举吗
java·后端·设计模式
地平线开发者16 分钟前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Orange30151118 分钟前
《深入源码理解webpack构建流程》
前端·javascript·webpack·typescript·node.js·es6
lovepenny40 分钟前
Failed to resolve entry for package "js-demo-tools". The package may have ......
前端·npm
Tisfy41 分钟前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
喵手1 小时前
如何利用Java的Stream API提高代码的简洁度和效率?
java·后端·java ee
超凌1 小时前
threejs 创建了10w条THREE.Line,销毁数据,等待了10秒
前端
-Xie-1 小时前
Maven(二)
java·开发语言·maven