c++银行账户管理程序4(最终版)

c++银行账户管理程序4(最终版)

在C++银行账户管理程序3的基础上做如下改进:
在基类Account中添加deposit、withdraw、settle这3个函数并将其声明为纯虚函数;将show函数声明为虚函数,通过基类的指针可以调用派生类的相应函数。将计算两个日期相差天数的distance函数改为“—”运算符重载函数。

main.cpp

#include<iostream>
#include<string.h>
#include"date.h"
#include"account.h"
using namespace std;
double Account::total = 0;
int main()
{
    Date d01(2008,11,5),d02(2008,12,5),d11(2008,11,25),d12(2008,12,20),d2(2009,1,1),dc1(2008,11,15),dc2(2008,12,1);
    Account *ptr;           //定义基类指针
    cout<<"--------------------------------"<<"1001"<<"------------------------------"<<endl;
    SavingsAccount s0(2008,11,1,1001,0,0.015);
    ptr=&s0;                //基类指针调用
    ptr->deposit(d01,5000);
    ptr->deposit(d02,5500);
    ptr->settle(d2);
    d2.show();
    ptr->show();
    cout<<"------------------------------------------------------------------"<<endl;
    cout<<"--------------------------------"<<"1002"<<"------------------------------"<<endl;
    SavingsAccount s1(2008,11,1,1002,0,0.015);
    ptr=&s1;
    ptr->deposit(d11,10000);
    ptr->withdraw(d12,4000);
    ptr->settle(d2);
    d2.show();
    ptr->show();
    cout<<"------------------------------------------------------------------"<<endl;
    cout<<"--------------------------------"<<"1003"<<"------------------------------"<<endl;
    CreditAccount c0(2008,11,1,1003,0,10000,0.0005,50);
    ptr=&c0;
    ptr->deposit(dc1,2000);
    ptr->withdraw(dc2,2000);
    ptr->withdraw(dc2,-c0.getDebt(dc2));    //结算利息
    ptr->settle(d2);
    d2.show();
    ptr->show();
    cout<<"------------------------------------------------------------------"<<endl;
    d2.show();
    ptr->getTotal();
    cout<<"------------------------------------------------------------------"<<endl;
}

account.h

#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include"date.h"
class Account                       //基类
{
protected:
    int id;
    double balance;
    static double total;
public:
    Account(int id, double balance);
    const int getId() {return id;}
    const double getBalance() {return balance;}
    static void getTotal();
    virtual void deposit(Date date,double amount)=0;
    virtual void withdraw(Date date,double amount)=0;
    virtual void settle(Date date)=0;
    virtual void show();
};
class SavingsAccount:public Account     //储蓄卡类
{
protected:
    double rate;    //利率
    Date lastDate;
    double accumulation=0;
    const double accumulate(Date date);
public:
    SavingsAccount(int year,int month,int day,int id,double balance,double rate);
    void deposit(Date date,double amount);
    void withdraw(Date date,double amount);
    void settle(Date date);
    const double getRate() {return rate;}
    void show();
};
class CreditAccount:public Account  //信用卡类
{
protected:
    double credit;      //额度
    double rate;        //利率
    double fee;         //年费
    Date lastDate;
public:
    CreditAccount(int year,int month,int day,int id,double balance,double credit,double rate,double fee);
    double getAvailableCredit() {return credit;}     //可用额度
    void deposit(Date date,double amount);
    void withdraw(Date date,double amount);
    double getDebt(Date date);                       //欠款额度
    void settle(Date date);
    void show();
};
#endif // ACCOUNT_H_INCLUDED

account.cpp

#include<iostream>
#include "account.h"
using namespace std;
Account::Account(int Id,double Balance)     //Account基类成员函数
{
    id=Id;
    balance=Balance;
}
void Account::show()
{
    cout<<"\t账号:"<<getId()<<"  余额:"<<getBalance()<<endl;
}
void Account::getTotal()
{
    cout<<"\t各账户总额:"<<total<<endl;
}
//SavingsAccount储蓄卡类成员函数
SavingsAccount::SavingsAccount(int year,int month,int day,int id,double balance,double Rate):Account(id,balance),lastDate(year,month,day)
{
    rate=Rate;
    cout<<year<<"/"<<month<<"/"<<day<<endl;
    cout<<"\t账号"<<id<<"创建成功!"<<endl;
}
const double SavingsAccount::accumulate(Date date)
{
    return (lastDate-date)*balance;
}
void SavingsAccount::deposit(Date date,double amount)
{
    accumulation+=accumulate(date);
    balance+=amount;
    lastDate = date;
    total+=amount;
    cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay()<<endl;
    cout<<"\t"<<id<<"\t存入:"<<amount<<"\t余额:"<<balance<<endl;
}
void SavingsAccount::withdraw(Date date,double amount)
{
    accumulation+=accumulate(date);
    balance-=amount;
    lastDate = date;
    total-=amount;
    cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay()<<endl;
    cout<<"\t"<<id<<"\t取出:"<<amount<<"\t余额:"<<balance<<endl;
}
void SavingsAccount::settle(Date date)
{
    double settle = 0;
    if(date.getYear()>lastDate.getYear())           //储蓄卡一年结算一次利息
    {
        for(int i=1;i<=date.getYear()-lastDate.getYear();i++)       //
        {
            accumulation+=accumulate(date);
            settle = accumulation/356*rate;
        }
    }
    cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay()<<endl;
    cout<<"\t"<<id<<"\t结算利息:"<<settle<<endl;
    deposit(date,settle);
}
void SavingsAccount::show()
{
    Account::show();
}
//CreditAccount信用卡类
CreditAccount::CreditAccount(int year,int month,int day,int id,double balance,double Credit,double Rate,double Fee): Account(id,balance),lastDate(year,month,day)
{
    credit=Credit;
    fee=Fee;
    rate=Rate;
    cout<<year<<"/"<<month<<"/"<<day<<endl;
    cout<<"\t账号"<<id<<"创建成功!"<<endl;
}
double CreditAccount::getDebt(Date date)
{
    settle(date);
    return balance;
}
void CreditAccount::deposit(Date date,double amount)
{
    settle(date);
    if(credit<=0 || amount>credit)
    {
        date.show();
        cout<<"\t"<<id<<"\t信用卡可用额度不足!"<<endl;
    }
    else
    {
        balance-=amount;
        credit-=amount;
        date.show();
        cout<<"\t"<<id<<"\t信用卡借款:"<<amount<<"\t\t还可用额度:"<<credit<<endl;
        total-=amount;
    }
}
void CreditAccount::withdraw(Date date,double amount)
{
    settle(date);
    balance+=amount;
    if((credit+amount)<=10000)
    {
        credit=credit+amount;
    }
    else
    {
        credit=10000;
    }
    total+=amount;
    date.show();
    cout<<"\t"<<id<<"\t信用卡还款:"<<amount<<"\t\t还可用额度:"<<credit<<endl;
}
void CreditAccount::settle(Date date)
{
    double settle=0;
    if((date.getMonth()<lastDate.getMonth())||(date.getYear()>lastDate.getYear()))
    {
        for(int i=1;i<=date.getYear()-lastDate.getYear();i++)
        {
            settle+=fee;                    //年费
        }
    }
    if(date.getDay()==1||date.getMonth()>lastDate.getMonth()||date.getYear()>lastDate.getYear())
    {
        if(date.getYear()==lastDate.getYear())
        {
            for(int j=1;j<=(date.getMonth()-lastDate.getMonth());j++)
            {
                for(int m=1;m<=lastDate-date;m++)
                {
                    settle-=balance*rate;
                }
            }
        }
        else
        {
            for(int j=1;j<=(date.getMonth()-lastDate.getMonth())+12;j++)
            {
                for(int m=1;m<=lastDate-date;m++)
                {
                    settle-=balance*rate;
                }
            }
        }
    }
    else
    {
        settle-=0;
    }
    balance-=settle;
    lastDate=date;
    total-=settle;
}
void CreditAccount::show()
{
    Account::show();
}

date.h

#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class  Date
{
    int  year, month, day;
    int  totalDays;   //该日期是从公元元年1月1日开始的第几天
public:
    Date(int year, int month, int day);
    int  getYear() const { return year; }
    int  getMonth() const { return month; }
    int  getDay() const { return day; }
    void  show() const;			//输出当前日期
    bool  isLeapYear(int year) const;	//判断当年是否为闰年
    int operator-(const Date &date) const {return date.totalDays-totalDays;}      //运算符-重载
};
#endif // DATE_H_INCLUDED

date.cpp

#include<iostream>
#include "date.h"
using namespace std;
Date::Date(int Year, int Month, int Day)
{
    year=Year;
    month=Month;
    day=Day;
    int Days=0;
    for(int i=0;i<year;i++)
    {
        if(isLeapYear(i)==true)
            Days=Days+366;
        else
            Days=Days+365;
    }
    switch(month){
    case 1:Days+=0;break;
    case 2:Days+=31;break;
    case 3:Days+=59;break;
    case 4:Days+=90;break;
    case 5:Days+=120;break;
    case 6:Days+=151;break;
    case 7:Days+=181;break;
    case 8:Days+=212;break;
    case 9:Days+=243;break;
    case 10:Days+=273;break;
    case 11:Days+=304;break;
    case 12:Days+=334;break;
    default:cout<<"month error!"<<endl;
    }
    totalDays=Days+day;
}
void  Date::show() const
{
    cout<<getYear()<<"/"<<getMonth()<<"/"<<getDay()<<endl;
}	//输出当前日期
bool  Date::isLeapYear(int year) const
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return true;
    else
        return false;
}	//判断当年是否为闰年

代码截图

c++银行账户管理程序4(最终版)_第1张图片
更多见
C++银行账户管理1
C++银行账户管理2
c++银行账户管理3

你可能感兴趣的:(C++银行账户管理)