📄 bank.h
字号:
//Bank.h
#include<algorithm>
#include<fstream>
#include"Transaction.h"
#include"Account.h"
#include<list>
class Bank {
public:
Bank(char *filename);
~Bank();
void addAccount(char* uname,int pincode,float bal,float overdraft);
void removeAccount(int id);
bool validate(int id,int pincode);
void processTrans(Transaction*);
void print();
Account* getAccount(int i );// return a pointer to the Account with this id
void update(char* newfile); //Update information about the accounts to a new file
private:
list<Account> L;
};
Bank::Bank(char* filename){
ifstream in(filename);
if(!in){ //The input file is not found, the Program will be determinated!
cerr<<"Can not find the Account Record File!"<<endl;
exit(1);
}
char* uname=new char[80];
int uid,ucode;
float ubalance,uoverdraft;
// get the accounts to store in a list of STL in memory
while(!in.eof())
{
in>>uname>>uid>>ucode>>ubalance>>uoverdraft;
Account *NewAccount= new Account(uname,uid,ucode,ubalance,uoverdraft);
L.push_back(*NewAccount);
delete NewAccount;
}
delete[] uname;
}
Bank::~Bank(){}
void Bank::update(char* newfile){
ofstream out(newfile);
if(!out){
cerr<<"Can not find the New Account Record File!"<<endl;
exit(1);
}
int uid,ucode;
float ubalance,uoverdraft;
list <Account>::iterator Iter;
for(Iter=L.begin();Iter!=L.end();Iter++)
{
char * uname=new char[strlen(Iter->getName())+1];
strcpy(uname,Iter->getName());
uid=Iter->getID();
ucode=Iter->getCode();
ubalance=Iter->getBalance();
uoverdraft=Iter->getOverdraft();
out<<uname<<" "<<uid<<" "<<ucode<<" "<<ubalance<<" "<<uoverdraft;
if(Iter!=--L.end())
out<<endl;
delete[] uname;
}
}
void Bank::addAccount(char* uname,int pincode,float bal,float overdraft){
int uid=L.size()+1;
Account nameAccount(uname,uid,pincode,bal,overdraft);
L.push_back(nameAccount);
}
void Bank::removeAccount(int id){
list <Account>::iterator Iter;
for(Iter=L.begin();Iter!=L.end();Iter++)
{
if(Iter->getID()==id)
L.erase(Iter,++Iter);
}
}
bool Bank::validate(int id,int pincode){
list<Account>::iterator Iter;
Iter=L.begin();
while(Iter!=L.end())
{
if(Iter->getID()==id)//First find the id
{
if(Iter->getCode()==pincode)//check pincode is valid
return true;
else
return false;
}
else
Iter++;
}
return false;
}
void Bank::processTrans(Transaction* trans)
{
trans->execute();
}
void Bank::print(){
list<Account>::iterator Iter;
for(Iter=L.begin();Iter!=L.end();Iter++)
Iter->print();
}
Account* Bank::getAccount(int id){
list<Account>::iterator Iter;
Iter=L.begin();
while(Iter!=L.end())
{
if(Iter->getID()==id)
return &(*Iter);
else
Iter++;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -