32

#include <iostream>
#include <cmath>
using namespace std;

class SavingsAccount {
private:
int id;
double balance;
double rate;
int lastDate;
double accumulation;

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() { return id; }
double getBalance() { return balance; }
double getRate() { return rate; }

void deposit(int date, double amount);
void withdraw(int date, double amount);
void settle(int date);
void show();
};

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;
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() {
cout << "#" << id << "\tBalance: " << balance;
}

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;
return 0;
}

转载于:https://www.cnblogs.com/acm-icpcer/p/6640341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值