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

}
相关推荐
2601_9583529038 分钟前
A-59U 仅 37mm 却集成 USB + 双波束?
前端·回声消除·语音模块·降噪处理·降噪消回音
智慧物业老杨3 小时前
物业数字化落地思考:真正的转型,是底层数据秩序的重构
java·大数据·人工智能·微服务·系统架构
萧西待水4 小时前
奥赛一本通 1447 靶形数独
算法·深度优先
星空5 小时前
金蝶苍穹build.gradle配置
java·build.gradle
泯泷6 小时前
那段文字是谁删的?Yjs 14 正式版之前,一套删除归属方案的实现与边界
前端·javascript·算法
hold?fish:palm6 小时前
34 合并K个升序链表
javascript·算法·链表
步行cgn8 小时前
Spring 基于 XML 的自动装配:byName 详解
java·后端·spring
Navigator_Z8 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
野槐9 小时前
前端项目优化(有了我,会发生...)
前端
大帅点兵9 小时前
Apache Ant 1.9.9 完整讲解
java