【第七章】 个人银行账户管理程序  案例实现

//account.cpp
#include"account.h"
#include<cmath>
#include<iostream>
using namespace std;double Account::total = 0;
//Account类相关成员函数的实现
Account::Account(const Date &date, const string &id) : id(id), balance(0) {date.show();cout << "\t#" << id << "  created" << endl;}
void Account::record(const Date &date, double amount, const string &desc){amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位balance += amount;total += amount;date.show();cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;}
void Account::show() const
{cout << id << "\tBalance:" << balance;
}void Account::error(const string &msg)const{cout << "Error(#" << id << "):" << msg << endl;}//SavingsAccount类相关成员函数的实现SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate) : Account(date,id), rate(rate), acc(date,0)
{}void SavingsAccount::deposit(const Date &date, double amount, const string &desc){record(date, amount, desc);acc.change(date, getBalance());
}void SavingsAccount::withdraw(const Date &date, double amount, const string &desc){if (amount > getBalance()) {error("not enough money");
}else {record(date, -amount, desc);acc.change(date, getBalance());}
}void SavingsAccount::settle(const Date &date){double interest = acc.getSum(date)*rate / date.distance(Date(date.getYear() - 1, 1, 1)); //计算年息if (interest != 0)record(date, interest, "interest");acc.reset(date,getBalance());}
CreditAccount::CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee): Account(date, id), credit(credit), fee(fee), rate(rate), acc(date, 0) {}
void CreditAccount::deposit(const Date &date, double amount, const string &desc){record(date, amount, desc);acc.change(date, getDebt());
}void CreditAccount::withdraw(const Date &date, double amount, const string &desc){if (amount-getBalance() > credit) {error("not enough credit");}else {record(date, -amount, desc);acc.change(date, getDebt());}
}
void CreditAccount::settle(const Date &date){double interest = acc.getSum(date)*rate;if (interest != 0)record(date, interest, "interest");if (date.getMonth() == 1)record(date, -fee, "annual fee");acc.reset(date, getBalance());}
void CreditAccount::show() const
{Account::show();cout << "\tAvailable credit: " << getAvailableCredit();
}

  

// account.h : Defines the entry point for the console application.#ifndef _ACCOUNT_H_#define _ACCOUNT_H_#include"date.h"
#include"accumulator.h"
#include<string>class Account //储蓄账户类{private:std::string id; //账号double balance; //余额static double total; //所有账户的总金额
protected:Account(const Date&date, const std::string &id);//记录一笔账,date为日期,desc为说明,amount为金额void record(const Date &date, double amount, const std::string &desc);//报告错误信息void error(const std::string &msg)const;public://构造函数const std::string &getId() const { return id; }double getBalance() const { return balance; }static double getTotal() { return total; }void show() const;//显示账户信息
};
class SavingsAccount:public Account{
private:Accumulator acc;double rate;
public:SavingsAccount(const Date &date, const std::string &id, double rate);double getRate() const { return rate; }void deposit(const Date &date, double amount, const std::string &desc); //存入现金void withdraw(const Date &date, double amount, const std::string &desc); //取出现金void settle(const Date &date);//结算利息,每年1月1日调用一次该函数};
class CreditAccount :public Account {
private:Accumulator acc;double credit;double rate;double fee;double getDebt()const {double balance = getBalance();return(balance < 0 ? balance : 0);}
public:CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);double getCredit() const { return credit; }double getRate() const { return rate; }double getFee() const { return fee; }double getAvailableCredit() const {if (getBalance() < 0)return credit + getBalance();elsereturn credit;}void deposit(const Date &date, double amount, const std::string &desc); //存入现金void withdraw(const Date &date, double amount, const std::string &desc); //取出现金void settle(const Date &date);//结算利息,每年1月1日调用一次该函数void show() const;//显示账户信息};#endif//_ACCOUNT_H_

  

//accumulator.h
#ifndef _ACCUMULATOR_H_
#define  _ACCUMULATOR_H_
#include"date.h"
class Accumulator {
private:Date lastDate;double value;double sum;
public:Accumulator(const Date&date,double value):lastDate(date),value(value),sum(0){}double getSum(const Date &date)const {return sum + value*date.distance(lastDate);}void change(const Date&date, double value) {sum = getSum(date);lastDate = date;this->value = value;}void reset(const Date&date, double value) {lastDate = date;this->value = value;sum = 0;}
};
#endif // _ACCUMULATOR_H_

  

// chapter07银行账户.cpp : Defines the entry point for the console application.
//// chapter06.cpp : Defines the entry point for the console application.
//7_10.cpp//#include"account.h"
#include"account.h"
#include"date.h"
#include<iostream>
using namespace std;int main() {Date date(2008, 11, 1);//建立几个账户SavingsAccount sa1(date, "03755217", 0.015);SavingsAccount sa2(date, "02342342", 0.015);CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);//11月的几笔账目sa1.deposit(Date(2008, 11, 5), 5000, "salary");ca.withdraw(Date(2008, 11, 15), 2000, "buy a phone");sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");//结算信用卡ca.settle(Date(2008, 12, 1));//12月的几笔账目ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");sa1.deposit(Date(2008, 12, 1), 5500, "salary");//结算所有的账户并输出各个账户信息sa1.settle(Date(2009, 1, 1));sa2.settle(Date(2009, 1, 1));ca.settle(Date(2009, 1, 1));cout << endl;sa1.show();cout << endl;sa2.show();cout << endl;ca.show();cout << endl;cout << "Total: " << Account::getTotal() << endl;return 0;
}

  

//date.cpp#include"date.h"
#include<iostream>
#include<cstdlib>
using namespace std;
namespace {const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}
Date::Date(int year, int month, int day) :year(year), month(month), day(day) {if (day <= 0 || day > getMaxDay()) {cout << "Invalid date: ";show();cout << endl;exit(1);}int years = year - 1;totalDays = year * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay()const {if (isLeapYear() && month == 2)return 29;elsereturn DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
void Date::show() const {cout << getYear() << "-" << getMonth() << "-" << getDay();
}

  

//date.h#ifndef _DATE_H_
#define _DATE_H_
class Date {
private:int year;int month;int day;int totalDays;
public:Date(int year, int month, int day);int getYear()const { return year; }int getMonth() const { return month; }int getDay()const { return day; }int getMaxDay()const;bool isLeapYear()const {return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;}void show()const;int distance(const Date &date)const {return totalDays - date.totalDays;}
};
#endif // !_DATE_H_

ps:

配套教材:郑莉《c++程序设计语言》

课程:学堂在线《c++程序设计语言》

转载于:https://www.cnblogs.com/cswangchen/p/7645056.html

【C++ 第七章 个人银行账户管理程序案例】相关推荐

  1. 个人银行账户管理程序

    这个程序是一个银行账户管理的程序,是用C++来实现程序功能的,该程序包含六个文件,其中有date.h头文件 是日期类的头文件,date.cpp是日期类的实现文件,accumulator.h是按日将数值 ...

  2. JAVA初学(七):银行账户演示程序【改进版】

    一.编程说明 在上一篇博客中,笔者通过Scanner实现了由键盘对银行账户的控制,虽说直观性更强一些,但是复杂度也增加了不少,甚至造成最后编出的代码有点离题的意思.因此在借鉴了网上的代码并加以自己的理 ...

  3. shell银行账户管理程序_如何在德国设立银行账户?德国银行卡比较和解析

    什么是Current Account? 通过Expatrio在线办理的自保金账户是一个特殊的,有限制条款的冻结账户,不能作为储蓄和消费账户使用.因此您需要设立一个欧元往来账户(current acco ...

  4. shell银行账户管理程序_德国邮政 Postbank 银行 开户 中文 参考教程

    精 选 众家 » 生活点滴 » 德国邮政 Postbank 银行 开户 中文 参考教程 德国邮政 Postbank 银行 开户 中文 参考教程 2014-07-29 2518 次阅读 德国邮政银行Po ...

  5. JAVA初学(七):银行账户演示程序

    一.设计梗概 做一个简单的银行.账户演示程序. (1)定义一个账户类,包含用户名.密码.余额等属性.存钱.取钱等方法. (2)定义一个银行类,包含银行名.账户列表等属性,开账户.查账户的功能.账户列表 ...

  6. 个人银行账户管理程序(C++语言程序设计第4版)

    银行作业分享,欢迎批评指正.到第12章. 文章目录 Array.h accumulator.h date.h account.h account.cpp date.cpp main.cpp comma ...

  7. C++转Java个人银行账户管理程序

    4.9 1.private,public等在Java里面需要单独写在每一个成员前面 java private int id; private void record(int date, double ...

  8. jupyter notebook第七章seaborn库的一些案例分析加相关函数的解析

    目录 前言 相关案例与解析 写在最后: 前言 Matplotlib绘图基本模仿MATLAB绘图库,其绘图风格和MATLAB类似.由于MATLAB绘图风格偏古典,因此,Python开源社区开发了Seab ...

  9. 理论:第七章:用生活的案例解释23种设计模式

    设计模式 1. 根据目的来分 根据模式是用来完成什么工作来划分,这种方式可分为创建型模式.结构型模式和行为型模式 3 种. 创建型模式:用于描述"怎样创建对象",它的主要特点是&q ...

最新文章

  1. Java 装饰器模式详解
  2. MyEclipse优化浅析
  3. android ui stencil kit 下载,实用的iOS6/iPhone5 GUI/iPad PSD以及其它版本素材
  4. 微软DNS服务器默认,DNS 服务器成为一座岛 - Windows Server | Microsoft Docs
  5. 高中会考access数据库_Access操作会考复习.doc
  6. ROBOTS.TXT在SEO优化中的运用(ROBOTS.TXT SEO优化实战)
  7. python importlib qpython_Python的import机制
  8. win10计算机管理看不见蓝牙,如何解决Win10设备管理器找不到蓝牙?
  9. java毕业设计水库洪水预报调度系统源码+lw文档+mybatis+系统+mysql数据库+调试
  10. 如何用思维导图快速理解PMBOK-PMP第六版教材
  11. 茆诗松等《高等数理统计(第二版)》例 1.28 的错误及改正
  12. 有哪些免费且比较好用的数据可视化工具?
  13. 两个60后大叔的新能源战争:王传福与曾毓群的万亿赌局
  14. 自己写一个strcmp函数(C++)
  15. ERP的工单(MO)
  16. 2、设计2个类,要求如下:[必做题] 2.1 定义一个汽车类Vehicle, 2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型 )和速度speed(do
  17. 计算机硬件配置一般看什么CPU,电脑配置怎么看好坏
  18. 6个“纽扣”卖2600 索尼动捕设备要捕捉谁?
  19. 【概率图与随机过程】06 朴素贝叶斯:基于条件独立性假设
  20. html 查看更多按钮样式,CSS3多样式按钮

热门文章

  1. 消除游戏美术设计的这些套路,你都知道吗?
  2. 2016rMBP登录后自动关机、帐户数据丢失
  3. pmp最近5题(2022年3月23日)
  4. Python小游戏(XO大战)
  5. Spring AOP中的动态代理主要有两种方式,JDK动态代理和CGLIB动态代理:
  6. 零基础学Python(第二十章 异常处理try)
  7. css之为文本添加线性渐变和外描边
  8. 读《程序是怎样跑起来》第五章有感
  9. Bzoj4818--Sdoi2017序列计数
  10. 初探Margin负值(转)