个人银行账户管理程序
这个程序分为五步:此处我把最终的多文件传上来
五个多文件分别是:**
1:date.h(日期类头文件)
2:date.cpp(日期类实现文件)
3:accumulator.h(按日将数值累加的Accumulator类的头文件)
4:account.h(各个储蓄账户类定义头文件)
5:account.cpp(各个储蓄账户类实现文件)
6:main.cpp(主函数文件)**
1:date.h(日期类头文件)如下:
#ifndef DATE
#define DATE
using namespace std;
#include <iostream>
#include <cstring>
#include <cstdlib>
//定义一个日期类
class Date
{
private:
int year; //年
int month; //月
int day; //日
int totalDays; //表示这一天的相对日期
//记录平年的每个月月份的累计值:
int PingYearSumMon[13] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
public:
Date(int Year, int Month, int Day); //对年、月、日进行初始化
int getYear()const; //返回年份
int getMonth()const; //返回月份
int getDay()const; //返回天
int getMaxDay()const; //用来得到当前月的天数
bool isLeapYear()const; //判断当前年是否为闰年的
void show()const; //用来将当前日期输出
int operator-(Date date)const; //用来判断当前日期与指定日期相差天数
};
#endif
2:date.cpp(日期类实现文件)如下:
#include "account.h"
//Date类的实现
Date::Date(int Year, int Month, int Day) : year(Year), month(Month), day(Day)
{
//检验参数的合法性
if (day < 0 || day > getMaxDay())
{
cout << "日期有误!!";
show();
exit(-1);
}
int years = year - 1;
//按平年计算:这一天的相对日期
totalDays = years * 365 + year / 4 - year / 100 + year / 400 + PingYearSumMon[month - 1] + day;
//按闰年计算:这一天的相对日期
if (isLeapYear())
totalDays++;
}
//返回年份
int Date::getYear()const
{
return year;
}
//返回月份
int Date::getMonth()const
{
return month;
}
//返回天
int Date::getDay()const
{
return day;
}
//用来得到当前月的天数
int Date::getMaxDay()const
{
//闰年二月份的天数
if (isLeapYear() && month == 2)
return 29;
else
return PingYearSumMon[month] - PingYearSumMon[month - 1];
}
//判断当前年是否为闰年的
bool Date::isLeapYear()const
{
if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
return true;
else
return false;
}
//用来将当前日期输出
void Date::show()const
{
cout << "当前日期为:" << year << "年" << month << "月" << day << "日" << endl;
}
//用来判断当前日期与指定日期相差天数 : 指定日期为1年1月1日
int Date::operator-(Date date)const
{
if (date.totalDays - this->totalDays > 0)
{
cout << "您的参数有误!!,退出程序" << endl;
system("pause");
exit(-1);
}
else
{
return abs(date.totalDays - this->totalDays); //返回与指定日期相差的天数
}
}
3:accumulator.h(按日将数值累加的Accumulator类的头文件)
#ifndef ACCUMULATOR
#define ACCUMULATOR
#include "date.h"
//用于计算(按日累加之和)所需的接口的(接口类)
class Accumulator
{
private:
Date lastDate; //表示被累加数值上次变更日期
double value; //被累加数值当前值
double sum; //上次变更被累加数值为止的按日累加总和
public:
Accumulator(Date date, double Value); //构造函数:进行初始化
double getSum(Date date)const; //用来计算到指定日期的累加结果
void change(Date date, double Value); //用来在指定日期更改数值
void reset(Date date, double Value); //用来将累加器清零并重新设定初始日期和数值
};
#endif ACCUMULATOR
4:account.h(各个储蓄账户类定义头文件)
#ifndef ACCOUNT
#define ACCOUNT
#include "accumulator.h"
//基类 Account 用来描述所有账户的共性
class Account
{
private:
string id; //用户账号
double balance; //用户余额
static double tota1, tota2; //用来记录各个账户的总金额
protected:
Account(Date date, string Id); //设置一个保护的构造函数,供派生类调用
void record(Date date, double amount, string desc); //无论是存款、取款还是结算利息, 都需要修改当前的余额并且将余额的变动输出
void error(string msg)const; //用来报告错误的函数
public:
string getld()const; //返回Id
double getBalance()const; //返回余额
static void changetota1(double amount); //用于(储蓄账户)各个账户金额变更
static void changetota2(double amount); //用于(信用账户)各个账户金额变更
virtual void show()const; //显示账户信息(show)
virtual void deposit(Date date, double amount, string desc) = 0; //存款(deposit)
virtual double settle(Date date) = 0; //计算利息
virtual void withdraw(Date date, double amount, string desc) = 0; //取款(withdraw)
static double getTotal(); //用来访问total
static double getTota2(); //用来访问tota2
};
//开设银行(储蓄账户)的类
class SavingsAccount : public Account
{
private:
Accumulator acc; //组合类
double rate; //年利率
public:
SavingsAccount(string Id, Date date, double Rate); //账户信息信息初始化
void deposit(Date date, double amount, string desc); //存款(deposit)
void withdraw(Date date, double amount, string desc); //取款(withdraw)
double getRate()const; //返回年利率
double settle(Date date); //结算利息(settle)
};
//开设银行(信用账户)的类
class CreditAccount : public Account
{
private:
Accumulator acc; //组合类
double credit; //信用额度
double rate; //日利率
double fee; //年费
int flag; //控制存款利息为1的标志
double getDebt()const; //返回信用卡借款值
public:
CreditAccount(Date date, string Id, double Credit, double Rate, double Fee); //构造函数初始化
double getCredit()const; //返回信用额度
double getFee()const; //返回年费
double getRate()const; //返回日利率
double getAvailableCredit()const; //用来计算可用的信用额度
void deposit(Date date, double amount, string desc); //信用账户存款
void withdraw(Date date, double amount, string desc); //信用账户取款
double settle(Date date); //信用账户结算利息
void show()const; //显示信用账户的内容
};
#endif // !1
5:account.cpp(各个储蓄账户类实现文件)
#include "account.h"
#include <string>
using namespace std;
double Account::tota1 = 0; //用来记录各个账户的总金额
double Account::tota2 = 0; //用来记录各个账户的总金额
//Accumulator(接口类的实现)
//构造函数:进行初始化
Accumulator::Accumulator(Date date, double Value): lastDate(date) //表示被累加数值上次变更日期
{
value = Value; //被累加数值当前值
sum = 0; //上次变更被累加数值为止的按日累加总和
}
//用来计算到指定日期的累加结果
double Accumulator::getSum(Date date)const
{
return (sum + (value * (date - lastDate))) / 365; //上次变更被累加数值为止的按日累加总和
}
//用来在指定日期更改数值
void Accumulator::change(Date date, double Value)
{
sum = getSum(date); //上次变更被累加数值为止的按日累加总和
lastDate = date; //修改上次变更的日期
value = Value; //修改被累加数值的当前值
}
//用来将累加器清零并重新设定初始日期和数值
void Accumulator::reset(Date date, double Value)
{
lastDate = date; //重新设定上次变更的日期
value = Value; //重新设定被累加数值的当前值
sum = 0; //累加器清零
}
//设置一个保护的构造函数,供派生类调用
Account::Account(Date date, string Id) : id(Id), balance(0)
{
id = Id; //对用户账号进行初始化
balance = 0; //余额进行初始化
date.show(); //显示日期
cout << id << "开户成功!!" << endl << endl;
}
//无论是存款、取款还是结算利息, 都需要修改当前的余额并且将余额的变动输出
void Account::record(Date date, double amount, string desc)
{
double red = settle(date); //记录利息
//计算余额变动时所积累的利息
cout << "执行本次(存款或取款)操作前,所账户所积累的利息为:" << red << "元" << endl;
amount = floor(amount * 100 + 0.5) / 100; //精确amount
balance += (amount + red); //余额变更
cout << desc << endl; //显示操作类型
date.show(); //显示日期
Account::show(); //显示(变更后的)账户余额信息
cout << endl; //换行
}
//用来报告错误的函数
void Account::error(string msg)const
{
cout << msg;
}
//返回Id
string Account::getld()const
{
return id;
}
//返回余额
double Account::getBalance()const
{
return balance;
}
//用于(储蓄账户)各个账户金额变更
void Account::changetota1(double amount)
{
tota1 += amount;
}
//用于(信用账户)各个账户金额变更
void Account::changetota2(double amount)
{
tota2 += amount;
}
//显示账户信息(show)
void Account::show()const
{
cout << "户名为:" << getld() << endl;
cout << "用户余额为:" << getBalance() << "元" << endl;
}
//用来访问total
double Account::getTotal()
{
return tota1;
}
//用来访问tota2
double Account::getTota2()
{
return tota2;
}
//账户信息信息初始化
SavingsAccount::SavingsAccount(string Id, Date date, double Rate) : Account(date, Id), acc(date, 0) //对:上一次余额变动的日期进行赋值
{
rate = Rate; //年利率赋值
}
//存款(deposit)
void SavingsAccount::deposit(Date date, double amount, string desc)
{
//检验参数的合法性
if (amount < 0)
{
Account::error("您的信息有误!!");
exit(-1); //退出程序
}
else
{
changetota1(amount); //修改(储蓄账户)个个账户(累计)总金额
record(date, amount, desc); //修改当前的余额并且将余额的变动输出
acc.change(date, getBalance()); //更改数值
}
}
//取款(withdraw)
void SavingsAccount::withdraw(Date date, double amount, string desc)
{
//检验参数的合法性
if (-amount > 0 || getBalance() < amount)
{
Account::error("您的参数有误,取款失败!!\n"); //操作错误
return;
}
else
{
changetota1(-amount);
record(date, -amount, desc); //修改当前的余额并且将余额的变动输出
acc.change(date, getBalance()); //更改数值
}
}
//返回年利率
double SavingsAccount::getRate()const
{
return rate;
}
//结算利息(settle)
double SavingsAccount::settle(Date date)
{
double interest = acc.getSum(date) * rate; //利息
if (interest != 0)
{
changetota1(interest);
}
acc.reset(date, getBalance());
return interest;
}
//返回信用卡借款值
double CreditAccount::getDebt()const
{
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
CreditAccount::CreditAccount(Date date, string Id, double Credit, double Rate, double Fee) : Account(date, Id), acc(date, 0)
{
credit = Credit; //对信用额度初始化
rate = Rate; //对日利率进行初始化
fee = Fee; //对年费进行初始化
}
//返回信用额度
double CreditAccount::getCredit()const
{
return credit;
}
//返回年费
double CreditAccount::getFee()const
{
return fee;
}
//返回日利率
double CreditAccount::getRate()const
{
return rate;
}
double CreditAccount::getAvailableCredit()const
{
//当欠款时:还可用的信用额为(总信用额度 + 欠款数目)
if (getBalance() < 0)
{
return credit + getBalance();
}
//无欠款时:还可以用的信用额度为最大值:credit
else
return credit;
}
void CreditAccount::deposit(Date date, double amount, string desc)
{
//判断是否需要计算利息
if (getBalance() < 0)
{
flag = 1;
}
else
{
flag = 0;
}
changetota2(amount); //修改(储蓄账户)个个账户(累计)总金额
record(date, amount, desc); //修改当前的余额并且将余额的变动输出
acc.change(date, getBalance()); //更改数值
}
void CreditAccount::withdraw(Date date, double amount, string desc)
{
//判断是否需要计算利息
if (getBalance() < 0)
{
flag = 1;
}
else
{
flag = 0;
}
//当(取款金额 - 余额 > 信用余额时)
if (amount - getBalance() > getAvailableCredit())
{
error("您取金额数目(超过了)可用信用余额!!");
}
else
{
credit -= amount;
record(date, -amount, desc); //修改当前的余额并且将余额的变动输出
changetota2(-amount);
acc.change(date, getDebt());
}
}
double CreditAccount::settle(Date date)
{
double interest;
if (flag == 1)
{
interest = acc.getSum(date) * rate; //计算利息,信用卡取款时存在利息
}
else
{
interest = 0; //存款时无利息
}
if (interest != 0)
{
changetota2(interest);
}
//等于1月份,扣年费
if (date.getMonth() == 1)
{
record(date, -fee, "annual fee");
changetota2(-fee);
}
acc.reset(date, getDebt());
return interest;
}
void CreditAccount::show()const
{
Account::show();
cout << "\tAvailable credit:" << getAvailableCredit();
}
6:main.cpp(主函数文件)**
#include "accumulator.h"
#include "account.h"
#include <malloc.h>
//主函数
int main()
{
Account* p = new SavingsAccount("001号", { 2018, 6, 3 }, 0.15); //开设一个储蓄账户
Account* p1 = new SavingsAccount("002号", { 2018, 6, 3 }, 0.15); //开设一个储蓄账户
Account* q = new CreditAccount({ 2018, 6, 3 }, "信用卡1号", 8000, 0.15, 0.25); //开设一个信用账户
Account* q1 = new CreditAccount({ 2018, 6, 3 }, "信用卡1号", 5000, 0.15, 0.25); //开设一个信用账户
//对储蓄进行操作
p->deposit({ 2019, 4, 3 }, 5000, "存款"); //存款
p->withdraw({ 2020, 8, 5 }, 2000, "取款:"); //取款
p->deposit({ 2021, 8, 8 }, 4000, "存款"); //存款
p1->deposit({ 2019, 4, 3 }, 2000, "存款"); //存款
p1->withdraw({ 2020, 8, 5 }, 3000, "取款:"); //取款
cout << "储蓄账户总金额为:" << p->getTotal() << "元" << endl;
//对信用账户进行操作
q->withdraw({ 2020, 8, 5 }, 2000, "取款:"); //取款
q->withdraw({ 2021, 10, 5 }, 2000, "取款:"); //取款
q->deposit({ 2022, 4, 3 }, 5000, "存款"); //存款
q->deposit({ 2023, 4, 3 }, 5000, "存款"); //存款
q1->withdraw({ 2020, 8, 5 }, 1000, "取款:"); //取款
q1->withdraw({ 2021, 10, 5 }, 2000, "取款:"); //取款
q1->deposit({ 2022, 4, 3 }, 2000, "存款"); //存款
q1->deposit({ 2023, 4, 3 }, 5000, "存款"); //存款
cout << "信用账户总金额为:" << q->getTota2() << "元" << endl;
delete p;
system("pause");
return 0;
}
羊羊羊——————制作
特注:各位朋友参考为准,自己学会如何写才是初衷!!!