美文网首页程序员
【漫跨】C++之个人银行账户管理程序(1)

【漫跨】C++之个人银行账户管理程序(1)

作者: _艾豆儿_ | 来源:发表于2017-11-21 17:33 被阅读83次

写在前面

没有什么能够阻挡你对考研的向往

正文
这个例子在前例的基础上添加了如下内容:

  1. 为SavingsAccount类添加一个静态数据成员total,用来记录各个账户的总金额,并为其添加了相应的静态成员函数getTotal对其进行访问;
  2. 将getBalance,accumulate等不需要改变对象状态的成员函数声明为常成员函数;
  3. 对程序结构进行了调整:将SavingsAccount类从主函数所在的源函数分开,建立两个新的文件account.h和account.cpp分别存放SavingsAccount类的定义和实现;
    代码如下:
//account.h
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
class SavingsAccount {
public:
    SavingsAccount(int date, int id, double rate);//构造函数
    int getId() const { return id; }
    double getBalance() const { return balance; }
    double getRate() const { return rate; }
    void show() const;
    void deposit(int date, double amount);//取钱
    void withdraw(int date, double amount);//存钱
    void settle(int date);//结算利息,假设每年1月1号结算一次
    static double getTotal() { return total; }
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;//计算账户余额的按日累计值
                        //记录一笔账,date为日期,amount为金额
    static double total;
    void record(int date, double amount);
    double accumulate(int date) const {
        return accumulation + (date - lastDate)*balance;
    }
}; 
#endif
//account.cpp
#include "account.h"
#include <cmath>
#include <iostream>
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) {
    accumulate(date);
    lastDate = date;
    amount = floor(amount * 100 + 0.5) / 100;//保留小数点后两位
    balance = balance + amount;
    total = total + amount;
    cout << date << "\t#" << id << "\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 << endl;

}

//test.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, 100000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);
    //假设开户90天到了银行的计息日,结算所有账户的年息
    sa0.settle(90);
    sa1.settle(90);
    //输出各个账户的信息
    sa0.show();
    cout << endl;
    sa1.show();
    cout << endl;
    cout << "Total:" << SavingsAccount::getTotal() << endl;
    return 0;

}
Here is the result

相关文章

网友评论

    本文标题:【漫跨】C++之个人银行账户管理程序(1)

    本文链接:https://www.haomeiwen.com/subject/dyvwvxtx.html