zoukankan      html  css  js  c++  java
  • 【C++ 第四章 个人银行账户管理程序案例】

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

    #include<iostream>
    #include<cmath>
    using namespace std;
    class SavingsAccount //储蓄账户类
    
    {
    
    private:
    
    	int id; //账号
    
    	double balance; //余额
    
    	double rate; //存款的年利率
    
    	int lastDate; //上次变更余额的日期
    
    	double accumulation; //余额按日累加之和
    
    	static double total; //所有账户的总金额
    
    						 //记录一笔账,date为日期,desc为说明
    
    	void record(int date, double amount);
    
    	//获得到指定日期为止的存款金额按日累积值
    
    	double accumulate(int date) const
    
    	{
    
    		return accumulation + balance*(date - lastDate);
    
    	}
    
    public:
    
    	//构造函数
    
    	SavingsAccount(int date, int id, double rate);
    
    	int getId() const { return id; }
    
    	double getBalance() const { return balance; }
    
    	double getRate() const { return rate; }
    
    	
    
    	void deposit(int date, double amount); //存入现金
    
    	void withdraw(int date, double amount); //取出现金
    
    											//结算利息,每年1月1日调用一次该函数
    
    	void settle(int date);
    
    	//显示账户信息
    
    	void show() const;
    
    };
    
    SavingsAccount::SavingsAccount(int date, int id, double rate)
    
    	: id(id), balance(0), rate(rate), lastDate(date), accumulation(0)
    
    {
    
    	cout << date << "	#" << id << " is created" << endl;
    
    }
    
    void SavingsAccount::record(int date, double amount)
    
    {
    
    	accumulation = accumulate(date);
    
    	lastDate = date;
    
    	amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
    
    	balance += amount;
    
    	cout << date << "	#" << id << "	" << amount << "	" << 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 << "	Balance:" << balance;
    
    }
    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;
    
    
    
    	return 0;
    
    }
    

      

    ps:

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

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

    雄关不惧 成败自含香
  • 相关阅读:
    5月,专用程序猿的经典大作——APUE
    [Android]Can&#39;t create handler inside thread that has not called Looper.prepare()
    HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
    mac 下有些工具 app 推荐
    机器学习倚门回首嗅青梅
    Android复制iPhone日期和时间选择器
    更新代码和工具,组织起来,提供所有博文(C++,2014.09)
    poj3349
    web项目启动,运行方法
    jstat
  • 原文地址:https://www.cnblogs.com/cswangchen/p/7645095.html
Copyright © 2011-2022 走看看