个人银行账户管理程序(文件分开)

分成三个文件

//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__

class SavingsAccount {
private:
	int id;
	double balance;  //余额 
	double rate;     //利率 
	int lastDate;    //上次变更的日期 
	double accumulation;  //最近一次余额变动时余额累加之和 
	static double total;  //所有账户总金额 

	void record(int date, double amount);//记录 

	double accumulate(int date) const {  //累积 
		return (accumulation + balance*(date - lastDate));
	}

public:
	SavingsAccount(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<iostream>
#include<cmath> 

using namespace std;

double SavingsAccount::total = 0;

SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
	cout << date << "\t#" << id << " is created" << endl;
}

void SavingsAccount::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 SavingsAccount::deposit(int date, double amount) {
	record(date, amount);
}

void SavingsAccount::withdraw(int date, double amount) {
	if (amount>getBalance())
		cout << "ERROR:not enough money" << endl;
	else
		record(date, -amount);
}

void SavingsAccount::settle(int date) {
	double interest = (accumulate(date)*rate) / 365;
	if (interest != 0)
		record(date, interest);
	accumulation = 0;
}

void SavingsAccount::show() const {
	cout << "#" << id << "\tBalance:" << balance;
}
//5_11.cpp
#include "account.h"
#include<iostream>
using namespace std;

int main() {

	SavingsAccount sa0(1, 21325302, 0.015);
	SavingsAccount sa1(1, 58320212, 0.015);

	sa0.deposit(5, 5000);
	sa1.deposit(25, 10000);
	sa0.deposit(45, 5500);
	sa1.withdraw(60, 4000);

	sa0.settle(90);
	sa1.settle(90);

	sa0.show(); cout << endl;
	sa1.show(); cout << endl;
	cout << "Total:" << SavingsAccount::getTotal() << endl;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值