savingaccount.h:
#ifndef SAVINGACCOUNT_H
#define SAVINGACCOUNT_H
class Savingaccount
{
private:
int id;
double balance;
double rate;
int lastDate;
double accumulation;
void record(int date,double amount);
double accumulate(int date);
public:
Savingaccount(int date,int id,double rate);
int getid();
double getbalance();
double getRate();
void desposit(int date,double amount);
void withdraw(int date,double amount);
void settle(int date);
void show();
};
#endif // SAVINGACCOUNT_H
savingaccount.cpp:
#include "savingaccount.h"
#include<iostream>
#include<QDebug>
#include<cmath>
Savingaccount::Savingaccount(int date,int id,double rate)
:id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
{
qDebug()<<date<<"\t#"<<id<<" is created"<<endl;
}
double Savingaccount::accumulate(int date)
{
return accumulation+balance*(date-lastDate);
}
int Savingaccount::getid()
{
return id;
}
double Savingaccount::getbalance()
{
return balance;
}
double Savingaccount::getRate()
{
return rate;
}
void Savingaccount::desposit(int date,double amount)
{
record(date,amount);
}
void Savingaccount::withdraw(int date,double amount)
{
if(amount>getbalance())
qDebug()<<"Error: not enough money!"<<endl;
else
record(date,-amount);
}
void Savingaccount::record(int date,double amount)
{
accumulation=accumulate(date);
lastDate=date;
amount=floor(amount*100+0.5)/100;//向下取整
balance+=amount;
qDebug()<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void Savingaccount::settle(int date)
{
double interest=accumulate(date)*rate/365;
if(interest!=0)
record(date,interest);
accumulation=0;
}
void Savingaccount::show()
{
qDebug()<<"#"<<id<<"\tBalance: "<<balance;
}
main.cpp:
#include <QCoreApplication>
#include<iostream>
#include<cmath>
#include "savingaccount.h"
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Savingaccount sa0(1,21325302,0.015);
Savingaccount sa1(1,58320212,0.015);
sa0.desposit(5,5000);
sa1.desposit(25,10000);
sa0.desposit(45,5500);
sa1.withdraw(60,4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();
sa1.show();
return 0;
return a.exec();
}