在C++个人银行账户管理2的基础再设计一个基类Account用来表述所有账户的共性,然后将SavingsAccount类变为其派生类,此外还要再派生出用来表示信用账户的类CreditAccount。
整个程序分为5个文件:date.h account.h是类定义头文件,date.cpp account.cpp是类实现文件,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);
cout<<"--------------------------------"<<"1001"<<"------------------------------"<<endl;
SavingsAccount s0(2008,11,1,1001,0,0.015);
s0.deposit(d01,5000);
s0.deposit(d02,5500);
s0.settle(d2);
d2.show();
s0.show();
cout<<"------------------------------------------------------------------"<<endl;
cout<<"--------------------------------"<<"1002"<<"------------------------------"<<endl;
SavingsAccount s1(2008,11,1,1002,0,0.015);
s1.deposit(d11,10000);
s1.withdraw(d12,4000);
s1.settle(d2);
d2.show();
s1.show();
cout<<"------------------------------------------------------------------"<<endl;
cout<<"--------------------------------"<<"1003"<<"------------------------------"<<endl;
CreditAccount c0(2008,11,1,1003,0,10000,0.0005,50);
c0.deposit(dc1,2000);
c0.withdraw(dc2,2000);
c0.withdraw(dc2,-c0.getDebt(dc2)); //结算利息
c0.getDebt(d2);
d2.show();
c0.show();
cout<<"-----------------------------"<<"各账户总金额"<<"-------------------------"<<endl;
d2.show();
c0.getTotal();
cout<<"-------------------------------------------------------------------"<<endl;
}
#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();
void show();
};
class SavingsAccount: public Account
{
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;}
const 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);
const void show();
};
#endif // ACCOUNT_H_INCLUDED
#include<iostream>
#include "account.h"
using namespace std;
//Account基类成员函数
Account::Account(int Id,double Balance)
{
id=Id;
balance=Balance;
}
void Account::show()
{
cout<<"账号:"<<getId()<<" 余额:"<<getBalance()<<endl;
}
void Account::getTotal()
{
cout<<"各账户总额:"<<total<<endl;
}
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<<"\t";
cout<<"账号"<<id<<"创建成功!"<<endl;
}
const double SavingsAccount::accumulate(Date date)
{
return lastDate.distance(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()<<"\t";
cout<<id<<"存入:"<<amount<<"\t\t"<<id<<"余额:"<<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()<<"\t";
cout<<id<<"取出:"<<amount<<"\t\t"<<id<<"余额:"<<balance<<endl;
}
void SavingsAccount::settle(Date date)
{
double settle = 0;
accumulation+=accumulate(date);
settle = accumulation/356*rate;
cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay()<<"\t";
cout<<id<<"结算利息:"<<settle<<endl;
deposit(date,settle);
}
const void SavingsAccount::show()
{
Account::show();
}
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<<"\t";
cout<<"账号"<<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<<id<<"\t信用卡可用额度不足!"<<endl;
}
else
{
balance-=amount;
credit-=amount;
date.show();
cout<<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<<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.distance(date);m++)
{
settle-=balance*rate;
}
}
}
else
{
for(int j=1;j<=(date.getMonth()-lastDate.getMonth())+12;j++)
{
for(int m=1;m<=lastDate.distance(date);m++)
{
settle-=balance*rate;
}
}
}
}
else
{
settle-=0;
}
balance-=settle;
total-=settle;
lastDate=date;
}
const void CreditAccount::show()
{
Account::show();
}
#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 distance(const Date& date) {return date.totalDays-totalDays;}//计算当前日期与指定日期之间相差天数
};
#endif // DATE_H_INCLUDED
#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()<<"\t";
} //输出当前日期
bool Date::isLeapYear(int year) const
{
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
} //判断当年是否为闰年
C++银行账户管理1
C++银行账户管理2
C++银行账户管理4