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

}
相关推荐
Dillon Dong8 分钟前
【风电控制】FPGA采集Vdc的ADC增益系数解析——从数字码到实际电压的桥梁
算法·fpga开发·变流器·风电控制
EAIReport10 分钟前
Spring AI 详解:Java 开发者快速落地 AI 应用
java·人工智能·spring
YDS82912 分钟前
DeepSeek RAG&MCP + Agent智能体项目 —— Agent执行链路设计之ReAct Loop
java·spring boot·ai·agent·deepseek
c++之路13 分钟前
C++ 设计模式全总结
java·c++·设计模式
TDengine (老段)15 分钟前
TDengine 压缩编码机制 — 双层压缩架构与类型特化算法
大数据·数据库·物联网·算法·时序数据库·tdengine·涛思数据
郑洁文22 分钟前
基于Python的Web命令执行漏洞自动化检测系统
前端·python·网络安全·自动化
码语智行30 分钟前
首页地图功能分析
java
新酱爱学习31 分钟前
手搓 10 个 Skill 后,我把重复劳动收敛成了一套零依赖 CLI 工具
前端·javascript·人工智能
段ヤシ.35 分钟前
回顾Java知识点,面试题汇总Day10:日期类、IO流(持续更新)
java·序列化·反序列化·日期类·i/o流
IT_陈寒1 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端