(由网课老师演示后,本人编写练习而来)
基础算法
一、分析
(1)数据成员
根据个人储蓄账户特点,一个账户(即一个对象)必须包含如下数据,另外,还需要显示整个系统现存金额。
数据名称 | 变量 | 类型 |
---|---|---|
账号 | id |
int |
余额 | balance |
double |
年利率 | rate |
double |
日期(补) | lastDate |
int |
全年每天余额加和(补) | accumulation |
double |
系统总金额 | total |
static double |
“系统总金额” 为整个类所共有,并非哪个对象所有,故将其声明为静态变量。
(2)函数成员
创建一个账户()后,对该账户所执行的一些必要操作有
- 数据信息的公共访问接口
- 获取账户信息:
getId():int;
- 获取余额:
getBalance():double;
- 获取年利率:
getRate():double;
- 获取总额:
getTotal():double;
- 获取账户信息:
- 对账户的一些基本操作
- 存款:
deposit(int date, double amount):void;
- 取款:
withdraw(int date, double amount):void;
- 计算利息:
settle(int date):void;
- 显示账户信息:
show():void;
- 存款:
二、算法设计
(1)UML图
SavingsAccount |
---|
- id:int - balance:double - rate:double - total:static double // 静态数据成员必须使用静态函数成员处理 - lastDate:int - accumulation:double
|
+ SavingsAccount():void//构造函数 + <<const>>getId():int + <<const>>getBalance():double + <<const>>getRate():double + <<static>>getTotal():double + deposit(int date, double amount):void // amount金额 + withdraw(int date, double amount):void + settle(int date):void + show():void + record(int date, double amount):void
|
(2)核心
- 日期:简化处理,例如:第2天存入500元,第5天再存入200,则500元维持的天数为
5-2=3
天;即日期为一年中第x天。
- 利息计算
- 日均余额=一年中每天的余额加和(
accumulation
)/一年的总天数; - 年利(
interest
)=日均余额*年利率;
- 日均余额=一年中每天的余额加和(
- 由于存、取,其对数据成员的操作完全相同,故提取出一个
record(...);
记账函数,减少重复。 - 增加一个函数
accumulate(int date);
,计算在第二次余额发生变动时,第一阶段的每日余额加和值。
//account.hpp
#ifndef account_hpp
#define account_hpp
#include <stdio.h>
using namespace std;
#define YPERD 365
class SavingsAccount{
private:
int id;
int lastDate;
double balance;
double rate;
double accumulation;
static double total;
void record(int date,double amount);
inline double accumulate(int date) const { return accumulation+balance*(date-lastDate); }
public:
SavingsAccount(int id,int date,double rate);//构造函数
inline int getId() const { return id; }
inline double getBalance() const { return balance; }
inline double getRate() const { return rate; }
inline static double getTotal() { return total; }
void deposit(int date,double amount);
void withdraw(int date,double amount);
void settle(int date);
void show();
};
#endif /* account_hpp */
//account.cpp
#include "account.hpp"
#include <iostream>
using namespace std;
double SavingsAccount::total=0;
//构造函数
SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),lastDate(date),balance(0),rate(rate),accumulation(0){
cout<<"\t#"<<id<<" is create."<<endl;
}
void SavingsAccount::record(int date,double amount){
accumulation=accumulate(date);
amount=(amount*100+0.5)/100;
lastDate=date;
balance+=amount;
total+=amount;
cout<<"\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>balance)
cout<<"Error, No enough money."<<endl;
else
record(date, -amount); }
void SavingsAccount::settle(int date){
double interest=accumulate(date)*rate/YPERD;
if (interest)
record(date, interest);
accumulation=0;
}
void SavingsAccount::show(){
cout <<"\t#"<< id << "\tBalance: " << balance;
}
//main.cpp
#include <iostream>
#include "account.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
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<<"\t---System Total:"<<SavingsAccount::getTotal()<<"---"<<endl;
return 0;
}
运行结果:
#21325302 is create.
#58320212 is create.
#21325302 5000.01 5000.01
#58320212 10000 10000
#21325302 5500.01 10500
#58320212 -3999.99 6000.01
#21325302 27.642 10527.7
#58320212 21.7858 6021.8
#21325302 Balance: 10527.7
#58320212 Balance: 6021.8
---System Total:16549.4---