1. 程式人生 > >【C++ 第五章 個人銀行賬戶管理程序案例】

【C++ 第五章 個人銀行賬戶管理程序案例】

family public esp font avi col 程序設計 ++ pan

【第五章】 個人銀行賬戶管理程序 案例實現

//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 << "\t#" << 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 << "\t#" << id << "\t" << amount << "\t" << 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 << "\tBalance:" << balance;
84 
85 }

ps:

配套教材:鄭莉《c++程序設計語言》

課程:學堂在線《c++程序設計語言》

【C++ 第五章 個人銀行賬戶管理程序案例】