題目描述
前面創(chuàng)建了Account類(lèi)來(lái)建模銀行賬戶(hù):
(1)一個(gè)名為id的int型數(shù)據(jù)域淳蔼,表示賬戶(hù)的身份號(hào)。
(2)一個(gè)名為balance的double型的數(shù)據(jù)域但惶,表示賬面余額枷遂。
(3)一個(gè)名為annualInterestRate的double型數(shù)據(jù)域,保存當(dāng)前年利率竞川。
(4)一個(gè)無(wú)參的構(gòu)造函數(shù)店溢,創(chuàng)建一個(gè)缺省的賬戶(hù),其數(shù)據(jù)域id為0委乌,balance為0床牧,annualInterestRate為0。
(5)id遭贸、balance和annualInterestRate的訪(fǎng)問(wèn)器和更改器函數(shù)戈咳。
一個(gè)賬戶(hù)有賬號(hào)、余額、年利率和賬戶(hù)創(chuàng)建時(shí)間等屬性著蛙,還有存款和取款函數(shù)删铃。創(chuàng)建它的兩個(gè)派生類(lèi)——支票賬戶(hù)和儲(chǔ)蓄賬戶(hù),前者有一個(gè)透支額度踏堡,后者不允許透支猎唁。定義Account類(lèi)的一個(gè)常量虛函數(shù)toString(),并在派生類(lèi)覆蓋它顷蟆,用來(lái)以字符串形式返回賬號(hào)的余額诫隅。
畫(huà)出這些類(lèi)的UML圖,實(shí)現(xiàn)該類(lèi)帐偎,并編寫(xiě)一個(gè)測(cè)試程序逐纬,它創(chuàng)建一個(gè)Account、一個(gè)SavingsAccount和一個(gè)CheckingAccount賬戶(hù)削樊,并調(diào)用它們的toString()函數(shù)风题。
C++代碼
Account.h
//
// Created by blue on 16-12-8.
//
#ifndef CHECKINGACCOUNTCLASS_ACCOUNT_H
#define CHECKINGACCOUNTCLASS_ACCOUNT_H
#include<string>
class Account {
private:
int id;
double balance;
double annualInterestRate;
public:
Account();
virtual std::string toString();
public:
int getId();
void setId(int id);
double getBalance();
void setBalance(double balance);
double getAnnualInterestRate();
void setAnnualInterestRate(double annualInterestRate);
};
#endif //CHECKINGACCOUNTCLASS_ACCOUNT_H
account.cpp
//
// Created by blue on 16-12-8.
//
#include "iostream"
#include "sstream"
#include "Account.h"
Account::Account() {
Account::id = 0;
Account::balance = 0;
Account::annualInterestRate = 0;
}
int Account::getId() {
return id;
}
void Account::setId(int id) {
Account::id = id;
}
double Account::getBalance() {
return balance;
}
void Account::setBalance(double balance) {
Account::balance = balance;
}
double Account::getAnnualInterestRate() {
return annualInterestRate;
}
void Account::setAnnualInterestRate(double annualInterestRate) {
Account::annualInterestRate = annualInterestRate;
}
std::string Account:: toString(){
std::ostringstream ostring;
ostring << "Account:" << Account::getId() << " balance:" << Account::getBalance();
return ostring.str();
};
SavingsAccount.h
//
// Created by blue on 16-12-8.
//
#ifndef CHECKINGACCOUNTCLASS_SAVINGSACCOUNT_H
#define CHECKINGACCOUNTCLASS_SAVINGSACCOUNT_H
#include <string>
#include "Account.h"
class SavingsAccount : public Account {
private:
bool allowedOverdraft;
std::string createAt;
public:
std::string toString() override;
bool deposit(int id,double money);
bool withdrawal(int id,double money);
public:
SavingsAccount();
const std::string &getCreateAt() const;
bool isAllowedOverdraft() const;
};
#endif //CHECKINGACCOUNTCLASS_SAVINGSACCOUNT_H
SavingsAccount.cpp
//
// Created by blue on 16-12-8.
//
#include "iostream"
#include "sstream"
#include "SavingsAccount.h"
#include "Date.h"
bool SavingsAccount::deposit(int id,double money){
if (id == SavingsAccount::getId()){
SavingsAccount::setBalance(SavingsAccount::getBalance()+money);
return true;
}
return false;
}
bool SavingsAccount::withdrawal(int id,double money){
if (id == SavingsAccount::getId()){
if (SavingsAccount::getBalance() < money){
return false;
}else{
SavingsAccount::setBalance(SavingsAccount::getBalance()-money);
return true;
}
}
return false;
}
const std::string &SavingsAccount::getCreateAt() const {
return createAt;
}
SavingsAccount::SavingsAccount() {
SavingsAccount::createAt = DateTime::currentTime();
}
bool SavingsAccount::isAllowedOverdraft() const {
return allowedOverdraft;
}
std::string SavingsAccount:: toString(){
std::ostringstream ostring;
ostring << "Account:" << SavingsAccount::getId() << " balance:" << SavingsAccount::getBalance() << " CreateAt:" << SavingsAccount::getCreateAt();
return ostring.str();
};
CheckingAccount.h
//
// Created by blue on 16-12-8.
//
#ifndef CHECKINGACCOUNTCLASS_CHECKINGACCOUNT_H
#define CHECKINGACCOUNTCLASS_CHECKINGACCOUNT_H
#include "Account.h"
class CheckingAccount : public Account {
private:
bool allowedOverdraft;
double overdraft;
std::string createAt;
public:
std::string toString() override;
bool deposit(int id,double money);
bool withdrawal(int id,double money);
public:
CheckingAccount();
const std::string &getCreateAt() const;
bool isAllowedOverdraft() const;
double getOverdraft() const;
void setOverdraft(double overdraft);
};
#endif //CHECKINGACCOUNTCLASS_CHECKINGACCOUNT_H
CheckingAccount.cpp
//
// Created by blue on 16-12-8.
//
#include "iostream"
#include "sstream"
#include "CheckingAccount.h"
#include "Date.h"
bool CheckingAccount::deposit(int id,double money){
if (id == CheckingAccount::getId()){
CheckingAccount::setBalance(CheckingAccount::getBalance()+money);
return true;
}
return false;
}
bool CheckingAccount::withdrawal(int id,double money){
if (id == CheckingAccount::getId()){
if (CheckingAccount::getBalance()-money <= (-CheckingAccount::getOverdraft())){
return false;
}else{
CheckingAccount::setBalance(CheckingAccount::getBalance()-money);
return true;
}
}
return false;
}
const std::string &CheckingAccount::getCreateAt() const {
return createAt;
}
CheckingAccount::CheckingAccount() {
CheckingAccount::overdraft = 10000;
CheckingAccount::createAt = DateTime::currentTime();
}
bool CheckingAccount::isAllowedOverdraft() const {
return allowedOverdraft;
}
double CheckingAccount::getOverdraft() const {
return CheckingAccount::overdraft;
}
void CheckingAccount::setOverdraft(double overdraft) {
CheckingAccount::overdraft = overdraft;
}
std::string CheckingAccount:: toString(){
std::ostringstream ostring;
ostring << "Account:" << CheckingAccount::getId() << " balance:" << CheckingAccount::getBalance() << " CreateAt:" << CheckingAccount::getCreateAt();
return ostring.str();
};
Date.h
//
// Created by blue on 16-12-8.
//
#ifndef CHECKINGACCOUNTCLASS_DATE_H
#define CHECKINGACCOUNTCLASS_DATE_H
#include <ctime>
#include <string>
#include <type_traits>
class DateTime {
public:
template<typename T>
static std::string convert(time_t t) {
return time2string(t);
}
template<typename T>
static time_t convert(const std::string& timeStr) {
return string2time(timeStr);
}
static std::string currentTime() {
return time2string(time(nullptr));
}
private:
static std::string time2string(time_t t) {
struct tm* tmNow = localtime(&t);
char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = {'\0'};
std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tmNow);
return timeStr;
}
static time_t string2time(const std::string& timeStr) {
struct tm stTm;
sscanf(timeStr.c_str(), "%d-%d-%d %d:%d:%d",
&(stTm.tm_year),
&(stTm.tm_mon),
&(stTm.tm_mday),
&(stTm.tm_hour),
&(stTm.tm_min),
&(stTm.tm_sec));
stTm.tm_year -= 1900;
stTm.tm_mon--;
stTm.tm_isdst = -1;
return mktime(&stTm);
}
};
#endif //CHECKINGACCOUNTCLASS_DATE_H
main.cpp
#include <iostream>
#include "CheckingAccount.h"
#include "SavingsAccount.h"
using namespace std;
int main() {
SavingsAccount savingsAccount;
CheckingAccount checkingAccount;
cout << "SavingsAccount:" << savingsAccount.toString() << endl;
cout << "CheckingAccount" << checkingAccount.toString() << endl;
return 0;
}