• 【C++ 第五章 个人银行账户管理程序案例】


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

    //5_11.cpp
    
    #include"account.h"
    
    #include<iostream>
    #include"account.cpp"
    using namespace std;
    
    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;
    
    	cout << "Total:" << SavingsAccount::getTotal() << endl;
    
    	return 0;
    
    }
    

      

     1 // account.cpp : Defines the entry point for the console application.
     2 
     3 
     4 
     5 #ifndef _ACCOUNT_H_
     6 
     7 #define _ACCOUNT_H_
     8 
     9 class SavingsAccount //储蓄账户类
    10 
    11 {
    12 
    13 private:
    14 
    15     int id; //账号
    16 
    17     double balance; //余额
    18 
    19     double rate; //存款的年利率
    20 
    21     int lastDate; //上次变更余额的日期
    22 
    23     double accumulation; //余额按日累加之和
    24 
    25     static double total; //所有账户的总金额
    26 
    27                          //记录一笔账,date为日期,desc为说明
    28 
    29     void record(int date, double amount);
    30 
    31     //获得到指定日期为止的存款金额按日累积值
    32 
    33     double accumulate(int date) const
    34 
    35     {
    36 
    37         return accumulation + balance*(date - lastDate);
    38 
    39     }
    40 
    41 public:
    42 
    43     //构造函数
    44 
    45     SavingsAccount(int date, int id, double rate);
    46 
    47     int getId() const { return id; }
    48 
    49     double getBalance() const { return balance; }
    50 
    51     double getRate() const { return rate; }
    52 
    53     static double getTotal() { return total; }
    54 
    55     void deposit(int date, double amount); //存入现金
    56 
    57     void withdraw(int date, double amount); //取出现金
    58 
    59                                             //结算利息,每年1月1日调用一次该函数
    60 
    61     void settle(int date);
    62 
    63     //显示账户信息
    64 
    65     void show() const;
    66 
    67 };
    68 
    69 #endif//_ACCOUNT_H_
     1 //account.cpp
     2 
     3 #include"account.h"
     4 
     5 #include<cmath>
     6 
     7 #include<iostream>
     8 
     9 using namespace std;
    10 
    11 double SavingsAccount::total = 0;
    12 
    13 //SavingsAccount类相关成员函数的实现
    14 
    15 SavingsAccount::SavingsAccount(int date, int id, double rate)
    16 
    17     : id(id), balance(0), rate(rate), lastDate(date), accumulation(0)
    18 
    19 {
    20 
    21     cout << date << "	#" << id << " is created" << endl;
    22 
    23 }
    24 
    25 void SavingsAccount::record(int date, double amount)
    26 
    27 {
    28 
    29     accumulation = accumulate(date);
    30 
    31     lastDate = date;
    32 
    33     amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
    34 
    35     balance += amount;
    36 
    37     total += amount;
    38 
    39     cout << date << "	#" << id << "	" << amount << "	" << balance << endl;
    40 
    41 }
    42 
    43 void SavingsAccount::deposit(int date, double amount)
    44 
    45 {
    46 
    47     record(date, amount);
    48 
    49 }
    50 
    51 void SavingsAccount::withdraw(int date, double amount)
    52 
    53 {
    54 
    55     if (amount>getBalance())
    56 
    57         cout << "Error:not enough money" << endl;
    58 
    59     else
    60 
    61         record(date, -amount);
    62 
    63 }
    64 
    65 void SavingsAccount::settle(int date)
    66 
    67 {
    68 
    69     double interest = accumulate(date)*rate / 365; //计算年息
    70 
    71     if (interest != 0)
    72 
    73         record(date, interest);
    74 
    75     accumulation = 0;
    76 
    77 }
    78 
    79 void SavingsAccount::show() const
    80 
    81 {
    82 
    83     cout << "#" << id << "	Balance:" << balance;
    84 
    85 }

    ps:

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

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

    雄关不惧 成败自含香
  • 相关阅读:
    B+树Java代码实现以及测试
    TreeMap核心源码实现解析
    B树Java代码实现以及测试
    二叉树BinaryTree构建测试(无序)
    HashMap、HashTable差异详解
    TreeMap源码实现类中文全解析
    注解Annotation原理详解及其应用示例
    SpringCloud之Config配置中心+BUS消息总线原理及其配置
    在浏览器输入 URL 回车之后发生了什么
    SpringCloud之Zuul网关原理及其配置
  • 原文地址:https://www.cnblogs.com/cswangchen/p/7645085.html
Copyright © 2020-2023  润新知