account.h
#ifndef ACCOUNT
#define ACCOUNT
class Account{
public:
//链表类操作
Account(unsigned int acntNo,float balan=0.0);
static Account * First(){return pFirst;}
Account * Next(){return pNext;}
static void RemoveLink();//删除整个个链表
static void SaveLink();//保存整个链表
static void getLink();//从txt文件中读取数据建立链表
static void linkDisplay();//输出整个链表上的帐户及其余额
//账户类操作
unsigned int AccountNo(){return acntNumber;}
float AccountBalan(){return balance;}
virtual void Display();//便于统一显示,设为虚函数
void Deposit(float amount){balance+=amount;}
virtual void Withdrawal(float amount)=0;
static Account *getAccount(unsigned int acntNo);//返回指定帐号所在结点
static void balanceAccount();//查询指定帐号的余额
static void depositAccount();//向指定帐号存款
static void WithdrawalAccount();//从指定帐号取款
//为什么没定义析构函数?
protected:
//链表类数据
static Account *pFirst;
static Account *pTail;
Account *pNext;
//账户类数据
unsigned int acntNumber;
float balance;
const char * acntType="Account";
};
//------------------------------------------------------------------
class Savings: public Account{
public:
Savings(unsigned acntNo,float balan=0.0);
virtual void Withdrawal(float amount);
virtual void Display();
protected:
static float minbalance;//设置透支范围
};
//----------------------------------------------------------------
enum REMIT{remitByPost,remintByCable,remintByother};//信汇、电汇,无
class Checking:public Account{
public:
Checking(unsigned acntNo,float balan);
void SetRemit(REMIT re){remmitance=re;}
virtual void Withdrawal(float amount);
virtual void Display();
protected:
REMIT remmitance;//汇款方式
};
//--------------------------------------------------------------------
#endif // ACCOUNT
//------------------------------------------------------------------------------------------------------------------
account.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include "account.h"
using namespace std;
//静态数据成员
Account * Account::pFirst=0;
Account * Account::pTail=0;
Account::Account(unsigned acntNo,float balan):acntNumber(acntNo),balance(balan),pNext(NULL){
if(pFirst==0)
pFirst=pTail=this;
else
{
pTail->pNext=this;
pTail=this;//新建结点挂到链尾
}
}
//-----------
void Account::Display(){
cout<<"Account number:"<<acntNumber<<"
balance:"<<balance<<endl;
}
//-------------------------------------------------------------------------------
float Savings::minbalance=500;//设置透支范围
Savings::Savings(unsigned acntNo,float balan):Account(acntNo,balan){acntType="Savings";}
//
void Savings::Withdrawal(float amount){
if(amount>balance+minbalance)
cout<<"Insufficient funds: balance "<<balance<<",withdrawal "<<amount<<endl;
else
balance-=amount;
}
//
void Savings::Display(){
cout<<"Savings
";
Account::Display();
}
//----------------------------------------------------------------------------------
Checking::Checking(unsigned acntNo,float balan):Account(acntNo,balan),remmitance(remintByother){acntType="Checking";}
//
void Checking::Withdrawal(float amount){
float tmp=amount;
if(remmitance==remintByCable)
tmp+=60;
if(remmitance==remitByPost)
tmp+=30;
if(balance<tmp)
cout<<"Insufficisent fuds:balance "<<balance<<" ,withdrawal "<<tmp<<endl;
else
balance-=tmp;
}
//
void Checking::Display(){
cout<<"Checking ";
Account::Display();
}
//--------------------------------------------------------------------------------
//链表类操作
//从txt文件中读取数据建立链表
void getLink(){
ifstream cin("acc.txt");
char *type;unsigned int ac;float ba;
cout<<"hello"<<endl;
for(;cin>>type>>ac>>ba;){cout<<type<<endl;
if(!(strcmp(type,"Savings")))
new Savings(ac,ba);
else if(!(strcmp(type,"Checking")))
new Checking(ac,ba);
else{
cout<<"error account type :"<<type<<endl;
exit(1);
}
}
cout<<"world"<<endl;
}
//把整个链表保存在文件acc.txt中
void Account::SaveLink(){
}
//删除整个链表
void Account::RemoveLink(){
for(Account *q,*p=Account::First();p;p=q){
q=p->pNext;
delete p;
}
}
//输出整个链表上的帐户及其余额
void Account::linkDisplay(){
Account *p=Account::First();
for(;p;p=p->Next())
p->Display();
}
//账户类操作------
//返回指定帐号所在结点
Account * Account::getAccount(unsigned int acntNo){
Account *p=Account::First();
for(;p&&p->AccountNo()!=acntNo;p=p->Next());
return p;
}
//查询指定帐号的余额
void Account::balanceAccount(){
unsigned int acntNo;
cout<<"input the account number:";
cin>>acntNo;
Account *p=getAccount(acntNo);
if(!p)
cout<<"account number :"<<acntNo<<" not exist!"<<endl;
else
p->Display();
}
//向指定帐号存款
void Account::depositAccount(){
unsigned int acntNo;
float amount;
cout<<"input the account number and the amount to deposit:"<<endl;
cin>>acntNo>>amount;
Account *p=getAccount(acntNo);
if(!p)
cout<<"account number :"<<acntNo<<" not exist!"<<endl;
else{
p->Deposit(amount);
p->Display();
}
}
//从指定帐号取款
void Account::WithdrawalAccount(){
unsigned int acntNo;
float amount;
cout<<"input the account number and the amount to withdrawal:"<<endl;
cin>>acntNo>>amount;
Account *p=getAccount(acntNo);
if(!p)
cout<<"account number :"<<acntNo<<" not exist!"<<endl;
else{
p->Withdrawal(amount);
p->Display();
}
}
//--------------------------------------------------------------------------------------------------
acc.txt
Savings 12345 12345.00
Savings 10000 10000.00
Savings 09988 9988.00
Checking 20032 20032.00
Checking 25678 25678.00
Savings 26100 26100.00