📄 caccountmanager.cpp
字号:
#include "link.h"
/************************************************************************
function:账户管理类默认构造函数
param:无
return:
************************************************************************/
CAccManager::CAccManager()
{
}
/************************************************************************
function:账户管理类拷贝构造函数
param:类对象
return:
************************************************************************/
CAccManager::CAccManager(CAccManager& accMan)
{
}
/************************************************************************
function:运算符=重载
param:类对象
return:类对象本身
************************************************************************/
CAccManager& CAccManager::operator = (CAccManager &accMan)
{
*this=accMan;
return *this;
}
/************************************************************************
function:对外接口
param:无
return:类对象本身
************************************************************************/
CAccManager& CAccManager::GetInstance()
{
static CAccManager accman;
return accman;
}
/************************************************************************
function:账户添加函数
param:账户对象
return:无
************************************************************************/
void CAccManager::AddNewAcc(CAccount& acc)
{
VecAccount.push_back(acc);
SaveData();
}
/************************************************************************
function:向量容器账户个数查询函数
param:无
return:int
************************************************************************/
int CAccManager::GetSize()
{
int size;
size=VecAccount.size();
return size;
}
/************************************************************************
function:获取所有正常账户的id函数
param:
return:
************************************************************************/
string CAccManager::GetAllID()
{
string stemp;
for(int i=0;i<GetSize();i++)
{
if(VecAccount[i].GetStat()=="Alive")
{
stemp+=CMyString::IntToStr(VecAccount[i].GetID());
stemp+="/";
}
}
return stemp;
}
/************************************************************************
function:账户信息查询函数
param:账户编号accno
return:账户对象
************************************************************************/
CAccount& CAccManager::GetRec(int accno)
{
if (accno<=GetSize())
{
return VecAccount[accno];
}
else
return *VecAccount.end();
}
/************************************************************************
function:数据保存函数
param:文件名filename
return:布尔型
************************************************************************/
bool CAccManager::SaveData(char* filename)
{
fstream file;
file.open(filename,ios::out|ios::binary);
if (!file)
{
cerr<<"File Open or Create Fail!"<<endl;
return 0;
}
int i,count,len,id;
float balance;
count=VecAccount.size();
file.write((char *)&count,sizeof(count));
for (i=0;i<count;i++)
{
//保存id
id=VecAccount[i].GetID();
file.write((char *)&id,sizeof(id));
//保存性别
len=VecAccount[i].GetSex().length();
file.write((char *)&len,sizeof(len));
file.write(VecAccount[i].GetSex().c_str(),len);
//保存姓名
len=VecAccount[i].GetName().length();
file.write((char *)&len,sizeof(len));
file.write(VecAccount[i].GetName().c_str(),len);
//保存生日
len=VecAccount[i].GetBirth().length();
file.write((char *)&len,sizeof(len));
file.write(VecAccount[i].GetBirth().c_str(),len);
//保存电话
len=VecAccount[i].GetMobile().length();
file.write((char *)&len,sizeof(len));
file.write(VecAccount[i].GetMobile().c_str(),len);
//保存余额
balance=VecAccount[i].GetBalance();
file.write((char *)&balance,sizeof(balance));
//保存状态
len=VecAccount[i].GetStat().length();
file.write((char *)&len,sizeof(len));
file.write(VecAccount[i].GetStat().c_str(),len);
}
file.close();
return true;
}
/************************************************************************
function:数据载入函数
param:文件名filename
return:布尔型
************************************************************************/
bool CAccManager::Restore(char* filename)
{
fstream file;
file.open(filename,ios::in|ios::binary);
if(access(filename,0)==-1)
return false;
if (!file)
{
cerr<<"File Open Fail!!"<<endl;
return 0;
}
int i,count,len,_id;
float _balance;
char tempstr[200];
CAccount tempacc;
file.read((char*)&count,sizeof(count));
for (i=0;i<count;i++)
{
//读取学号
file.read((char *)&_id,sizeof(_id));
tempacc.SetID(_id);
//读取性别
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetSex(tempstr);
//读取姓名
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetName(tempstr);
//读取生日
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetBirth(tempstr);
//读取电话
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetMobile(tempstr);
//读取余额
file.read((char *)&_balance,sizeof(_balance));
tempacc.SetBalance(_balance);
//读取状态
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetStat(tempstr);
//临时账户添加到向量容器
VecAccount.push_back(tempacc);
}
file.close();
return true;
}
/************************************************************************/
/* 向量容器获取函数 */
/************************************************************************/
vector<CAccount>& CAccManager::GetVecAccount()
{
return VecAccount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -