资源描述
个人银行账户管理程序
实验内容:
设计一个程序,实现对个人银行账户的管理。银行账户有储蓄账户和信用账户两类。储蓄账户不可以透支,利息每年结算一次;信用账户允许在一定额度内透支,透支则需支付利息,并在每月1日进行一次结算。
实验要求:
1. 程序要实现的功能:
a) 用户创建新账户需要提供基本的账号以及金额,如果是储蓄账户,还需定义年利率,如果是信用账户,则还需定义信用额度,日利率,年费
b) 储蓄账户:在每年的1月1日结算利息,利息的计算: 将一年中每天的余额累积起来再除以一年总天数,得到日均余额,再乘以年利率
c) 信用账户:在每年的1月1日扣取年费,信用账户没有利息,但透支则需要支付利息,从透支那天开始计算利息(日均欠费金额×利率),并在每月的1日进行一次结算
d) 用户能查看账户信息以及账户余额的变动情况
e) 用户存取款需要提供日期和金额,取款失败应提示输出失败原因
2. 设计类并写出每个类的详细描述(包括构造函数,成员变量,成员函数的含义)和画出UML类图(体现类之间的关系和类内成员的访问权限)
3. 程序中使用的类应该做到类定义和实现分离(使用多文件结构)
4. 设计测试用例对程序功能进行充分测试,并对运行结果进行截图
实验分析:
定义账户基类Account描述所有账户的共性,包含私有成员:账号id、余额balance、账户总金额静态数据成员total;保护成员:构造函数Account()、记账函数record()、错误信息报告常函数error();公有成员:获取账号常函数getId()、获取余额常函数getBalance()、获取总金额静态函数getTotal()、显示账户信息show()。
再从账户基类中公有派生出储蓄账户类SavingsAccount和信用账户类CreditAccount,储蓄类账户中定义了私有成员:辅助计算利息的累加器acc、存款的年利率rate,公有成员:构造函数SavingsAccount()、用于访问年利率的常函数getRate()、存现金函数deposit()、取现金函数withdraw()、利息结算函数settle()。信用类账户中定义了私有成员:辅助计算利息的累加器acc、信用额度credit、欠款的日利率rate、信用卡年费fee、获得欠款项函数getDebt();公有成员:构造函数CreditAccount()、获取信用额度常函数getCredit()、获取日利率常函数getRate()、获取年费常函数getFee()、获得可用信用额度常函数getAvailableCredit()、存现金函数deposit()、取现金函数withdraw()、利息和年费结算函数settle()。
定义类Accumulator,用于计算一项数值的按日累加之和的接口,通过该类计算利息,包含三个数据成员——表示被累加数值上次变更日期的lastDate、被累加数值当前值value以及上次变更被累加数值为止的按日累加总和sum,该类同时包含四个函数成员——构造函数Accumulator()、用来计算到指定日期的累加结果的常函数getSum()、用来在指定日期更改数值的函数change()以及用来将累加器清零并重新设定初始日期和数值的函数reset()。
定义日期类Date,该类的数据成员包含year、month、day和totalDays,其中totalDays表示这一天的相对日期。该类的成员函数除了构造函数和用来获得year、month、day的函数外,还包括用来得到当前月的天数getMaxDay()函数、用来判断当前年是否为闰年的isLeapYear()函数、用来将当前日期输出的show()函数、用来判断当前日期与指定日期相差天数的distance()函数,这些函数都会被Date类的其他成员函数或SavingsAccount类的函数调用。
以上各个类之间的UML类图如下:
程序代码:
账户类定义头文件:
//Account.h
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <string>
#include "Date.h"
#include "Accumulator.h"
class Account {
private:
std::string id;
double balance;
static double total;
protected:
Account(const Date &date, const std::string &id);
void record(const Date &date, double amount, const std::string &desc);
void error(const Date &date, double amount, 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);
};
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(){
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);
void show() const;
};
#endif
账户类实现文件:
//Account.cpp
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Account.h"
using namespace std;
double Account::total = 0;
Account::Account(const Date &date, const std::string &id):id(id), balance(0){
date.show();
cout << "\t#" << id << " is created" << endl;
}
void Account::record(const Date &date, double amount, const std::string &desc){
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
total += amount;
date.show();
cout << "\t#" << id << "\t" << setw(6) << amount << "\t" << setw(10) << balance << "\t" << desc << endl;
}
void Account::show() const{
cout << "#" << id << "\tBalance:" << balance;
}
void Account::error(const Date &date, double amount, const string &msg) const{
date.show();
cout<< "\t#" << id << "\t" << setw(6) << amount << "\t" << setw(10) << balance << "\t" << "Error:"+msg <<endl;
}
SavingsAccount::SavingsAccount(const Date &date, const std::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(date, amount, "you 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 string& id,double credit,double rate,double fee):Account(date,id),credit(credit),rate(rate),fee(fee),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(date, amount, "you 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,getDebt());
}
void CreditAccount::show() const{
Account::show();
cout << "\t\tAvailable credit:" << getAvailableCredit();
}
日期类头文件:
//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;
}
int distance(const Date& date) const {
return totalDays - date.totalDays;
}
void show() const;
};
#endif
日期类实现文件:
//Date.cpp
#include <iostream>
#include <cstdlib>
#include "Date.h"
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 = years*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();
}
Accumulator类头文件:
//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
测试用例及截图:
//main.cpp
#include <iostream>
#include <iomanip>
#include "Account.h"
using namespace std;
int main() {
Date date(2016, 12, 22);
cout<<"建立账户"<<endl;
SavingsAccount sa1(date, "SA_2016", 0.015);
SavingsAccount sa2(date, "SA_2017", 0.015);
CreditAccount ca(date, "CA_2018", 10000, 0.0005, 100);
cout<<"所有账户的交易记录(存款、取款、结算)"<<endl;
cout<<"Time\t\t"<<"Account_id\t"<<setw(6)<<"Amount\t"<<setw(11)<<"Balance\t"<<"Notes"<<endl;
sa1.settle(Date(2017, 1, 1));
sa2.settle(Date(2017, 1, 1));
ca.settle(Date(2017, 1, 1));
sa1.deposit(Date(2017, 1, 5), 5000, "salary");
ca.withdraw(Date(2017, 1, 15), 3000, "buy a cell");
sa2.deposit(Date(2017, 1, 25), 10000, "sell stock 0323");
ca.settle(Date(2017, 2, 1));
ca.settle(Date(2017, 3, 1));
ca.settle(Date(2017, 4, 1));
ca.deposit(Date(2017, 4, 1), 2018, "repay the credit");
ca.settle(Date(2017, 5, 1));
sa1.deposit(Date(2017, 5, 5), 5500, "salary");
ca.settle(Date(2017, 6, 1));
ca.settle(Date(2017, 7, 1));
ca.withdraw(Date(2017, 7, 10), 10000, "donation");
sa2.withdraw(Date(2017, 7, 20), 15000, "buy a cell phone");
ca.settle(Date(2017, 8, 1));
ca.deposit(Date(2017, 8, 26), 1000, "repay the credit");
ca.settle(Date(2017, 9, 1));
ca.settle(Date(2017, 10, 1));
ca.settle(Date(2017, 11, 1));
ca.settle(Date(2017, 12, 1));
sa1.settle(Date(2018, 1, 1));
sa2.settle(Date(2018, 1, 1));
ca.settle(Date(2018, 1, 1));
cout<<"所有账户信息"<<endl;
sa1.show();cout<<endl;
sa2.show();cout<<endl;
ca.show();cout<<endl;
cout<<"所有账户总金额Total:"<< Account::getTotal() <<endl;
return 0;
}
运行结果:
展开阅读全文