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


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

    //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 << "	#" << 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 << "	#" << id << "	" << amount << "	" << balance << "	" << desc << endl;
    
    }
    void Account::show() const
    {
    	cout << id << "	Balance:" << 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 << "	Available 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();
    		else
    			return 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;
    	else
    		return 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++程序设计语言》

    雄关不惧 成败自含香
  • 相关阅读:
    Zset-ZREVRANGEBYSCORE
    Zset-ZREVERANGE
    Zset-ZRANGEBYSCORE
    Leetcode1550. 存在连续三个奇数的数组
    Java中的IO流
    线程间通信(也叫线程并发协作)的四种方式
    数据库三大范式
    MVCC(Multi-Version Concurrency Control):多版本并发控制详解
    Java三种单例模式实现
    Java的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/cswangchen/p/7645056.html
Copyright © 2020-2023  润新知