C++ 第六章 个人银行账户管理程序案例
Posted Barry
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 第六章 个人银行账户管理程序案例相关的知识,希望对你有一定的参考价值。
【第六章】 个人银行账户管理程序 案例实现
1 //第六章完整代码 2 #include<iostream> 3 #include<cstdlib> 4 #include<cmath> 5 using namespace std; 6 class Date { 7 private: 8 int year; 9 int month; 10 int day; 11 int totalDays; 12 public: 13 Date(int year, int month, int day); 14 int getYear()const { return year; } 15 int getMonth() const { return month; } 16 int getDay()const { return day; } 17 int getMaxDay()const; 18 bool isLeapYear()const{ 19 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 20 } 21 void show()const; 22 int distance(const Date &date)const { 23 return totalDays - date.totalDays; 24 } 25 }; 26 namespace 27 { 28 const int DAYS_BEFORE_MONTH[] = {0,31,59,90,120,151,181,212,243,273,304,334,365 }; 29 } 30 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 31 if (day <= 0 || day > getMaxDay()) { 32 cout << "Invalid date: "; 33 show(); 34 cout << endl; 35 exit(1); 36 } 37 int years = year - 1; 38 totalDays = year * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 39 if (isLeapYear() && month > 2) totalDays++; 40 } 41 int Date::getMaxDay()const { 42 if (isLeapYear() && month == 2) 43 return 29; 44 else 45 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 46 } 47 void Date::show() const { 48 cout << getYear() <<"-"<< getMonth() <<"-"<< getDay(); 49 } 50 class SavingsAccount //储蓄账户类 51 52 { 53 54 private: 55 56 std::string id; //账号 57 58 double balance; //余额 59 60 double rate; //存款的年利率 61 62 Date lastDate; //上次变更余额的日期 63 64 double accumulation; //余额按日累加之和 65 66 static double total; //所有账户的总金额 67 68 //记录一笔账,date为日期,desc为说明,amount为金额 69 70 void record(const Date &date, double amount,const std::string &desc); 71 72 //报告错误信息 73 void error(const std::string &msg)const; 74 //获得到指定日期为止的存款金额按日累积值 75 double accumulate(const Date &date) const 76 77 { 78 79 return accumulation + balance*date.distance(lastDate); 80 81 } 82 83 public: 84 85 //构造函数 86 87 SavingsAccount(const Date &date, const std::string &id, double rate); 88 89 const std::string &getId() const { return id; } 90 91 double getBalance() const { return balance; } 92 93 double getRate() const { return rate; } 94 95 static double getTotal() { return total; } 96 97 void deposit(const Date &date, double amount, const std::string &desc); //存入现金 98 99 void withdraw(const Date &date, double amount,const std::string &desc); //取出现金 100 101 //结算利息,每年1月1日调用一次该函数 102 103 void settle(const Date &date); 104 105 //显示账户信息 106 107 108 109 void show() const; 110 111 }; 112 double SavingsAccount::total = 0; 113 114 //SavingsAccount类相关成员函数的实现 115 116 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) 117 118 { 119 date.show(); 120 cout << "\t#" << id << " created" << endl; 121 122 } 123 124 void SavingsAccount::record(const Date &date, double amount, const string &desc) 125 126 { 127 128 accumulation = accumulate(date); 129 130 lastDate = date; 131 132 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 133 134 balance += amount; 135 136 total += amount; 137 date.show(); 138 139 cout <<"\t#" << id << "\t" << amount << "\t" << balance << "\t" <<desc<< endl; 140 141 } 142 void SavingsAccount::error(const string &msg)const 143 144 { 145 146 cout<<"Error(#"<<id<<"):"<<msg<<endl; 147 148 } 149 150 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) 151 152 { 153 154 record(date, amount,desc); 155 156 } 157 158 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) 159 160 { 161 162 if (amount > getBalance()) 163 error("not enough money"); 164 //cout << "Error:not enough money" << endl; 165 166 else 167 168 record(date, -amount,desc); 169 170 } 171 172 void SavingsAccount::settle(const Date &date) 173 174 { 175 176 double interest = accumulate(date)*rate / date.distance(Date(date.getYear()-1, 1, 1)); //计算年息 177 178 if (interest != 0) 179 180 record(date, interest,"interest"); 181 182 accumulation = 0; 183 184 } 185 186 void SavingsAccount::show() const 187 188 { 189 190 cout << id << "\tBalance:" << balance; 191 192 } 193 int main(){ 194 Date date(2008, 11, 1); 195 196 //建立几个账户 197 SavingsAccount accounts[] = { 198 199 SavingsAccount(date, "03755217", 0.015), 200 201 SavingsAccount(date, "02342342", 0.015) 202 203 204 }; 205 const int n = sizeof(accounts) / sizeof(SavingsAccount);//账户总数 206 207 208 //11月的几笔账目 209 210 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 211 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 212 //12月的几笔账目 213 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 214 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 215 //结算所有的账户并输出各个账户信息 216 cout << endl; 217 for (int i = 0;i < n;i++) { 218 accounts[i].settle(Date(2009, 1, 1)); 219 accounts[i].show(); 220 cout << endl; 221 } 222 cout << "Total: " << SavingsAccount::getTotal() << endl; 223 return 0; 224 }
ps:
配套教材:郑莉《c++程序设计语言》
课程:学堂在线《c++程序设计语言》
以上是关于C++ 第六章 个人银行账户管理程序案例的主要内容,如果未能解决你的问题,请参考以下文章