///個人銀行賬戶管理程式
#include<iostream>
#include<cmath>
using namespace std;
class SavingsAccount {
private:
int id; //賬號
double balance; //余額
double rate; //存款的年利率
int lastDate; //上次變更的日期
double accumulation; //余額按日累加之和
//記錄一筆賬,date為日期, amount為金額
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); //取出現金
//結算利息,每年1月1日呼叫該函式
void settle(int date);
//顯示賬戶資訊
void show();
};
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;
}
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::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);
//開戶后第90天后到了銀行的計息日,結算所有賬戶年息
sa0.settle(90);
sa1.settle(90);
//輸出各個賬戶的資訊
sa0.show(); cout << endl;
sa1.show(); cout << endl;
system("pause");
return 0;
}
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/221150.html
標籤:其他
上一篇:3D曲面重建之移動最小二乘法
