/************************************************************************
* Copyright (C) 2021 Chenyang https://wcy-dt.github.io *
* *
* @file bankdb.cpp *
* @brief Bankdb simulates a database for the project. The speed of *
* this database is slow for that we must ensure the safety *
* of every deal. *
* @author Chenyang *
* @date 2021 - 07 *
************************************************************************/
#include "bankdb.h"
#include <QDebug>
#include <QDialog>
#include <QMessageBox>
#include <fstream>
#include <map>
#include <time.h>
using namespace std;
/**
* @brief initialization and read from the file
*/
bankdb::bankdb()
{
GetFile();
}
/**
* @brief read from the file which stores the data
*/
void bankdb::GetFile()
{
vAccount.clear();
mAccount.clear();
mMoney.clear();
int iNumOfAccounts; /// number of accounts
int iNumOfFlow; /// number of flows
ifstream iFile("bankdb.db", ios::binary);
if (!iFile) /// the file not exist
{
iNumOfAccounts = 0; /// zero account
WriteFile();
return;
}
iFile >> iNumOfAccounts;
for (int i = 0; i < iNumOfAccounts; i++)
{
accountInfo tmpAccount; /// get a record, and pushback to vector
string strTmpString; /// for the convenience of infile
/**
* @note here i use a lot of getline(), for that spaces may exist in the strings
* getline(iFile, strTmpString) is for jump over ENTER
*/
iFile >> tmpAccount.strNumber;
getline(iFile, strTmpString);
getline(iFile, tmpAccount.strName);
getline(iFile, tmpAccount.strPasswd);
getline(iFile, tmpAccount.strAddress);
iFile >> tmpAccount.iType >> tmpAccount.dInterest >> tmpAccount.bLost >> tmpAccount.tLostTime;
getline(iFile, strTmpString);
getline(iFile, tmpAccount.strOperator);
iFile >> iNumOfFlow;
mMoney[tmpAccount.strNumber] = 0; /// initialize the money in the account
for (int j = 0; j < iNumOfFlow; j++)
{
flowInfo tmpFlow; /// get a flow, and pushback to vector
iFile >> tmpFlow.tTime >> tmpFlow.dMoney >> tmpFlow.iOperationType;
getline(iFile, strTmpString);
getline(iFile, tmpFlow.strOperator);
if (tmpFlow.iOperationType == 0) /// deposit
mMoney[tmpAccount.strNumber] += tmpFlow.dMoney;
else /// withdraw
mMoney[tmpAccount.strNumber] -= tmpFlow.dMoney;
tmpAccount.vFlow.push_back(tmpFlow);
}
mAccount[tmpAccount.strNumber] = vAccount.size(); /// map account number to the index
/// @see bankdb.h
vAccount.push_back(tmpAccount);
}
}
/**
* @brief write data to the file
*/
void bankdb::WriteFile()
{
int iNumOfAccounts = vAccount.size();
/**
* @note use trunc for that it is not much slower than find where to insert or edit
*/
ofstream oFile("bankdb.db", ios::trunc | ios::binary);
oFile << iNumOfAccounts << "\n";
for (int i = 0; i < iNumOfAccounts; i++)
{
accountInfo tmpAccount = vAccount[i];
int iNumOfFlow = tmpAccount.vFlow.size();
oFile << tmpAccount.strNumber << "\n"
<< tmpAccount.strName << "\n"
<< tmpAccount.strPasswd << "\n"
<< tmpAccount.strAddress << "\n"
<< tmpAccount.iType << "\n"
<< tmpAccount.dInterest << "\n"
<< tmpAccount.bLost << "\n"
<< tmpAccount.tLostTime << "\n"
<< tmpAccount.strOperator << "\n"
<< iNumOfFlow << "\n";
for (int j = 0; j < iNumOfFlow; j++)
{
flowInfo tmpFlow = tmpAccount.vFlow[j];
oFile << tmpFlow.tTime << "\n"
<< tmpFlow.dMoney << "\n"
<< tmpFlow.iOperationType << "\n"
<< tmpFlow.strOperator << "\n";
}
}
}
/**
* @brief add an account
*
* @param [in] strNum number of the account
* @param [in] strNam name of the account owner
* @param [in] strPas password of the account
* @param [in] strAdd address of the account owner
* @param [in] iType type of the account
* @param [in] dInt interest of the account
* @param [in] strOpe operator of the deal
*
* requirments of these params @see bankdb.h
*/
void bankdb::AddAccount(string strNum, string strNam, string strPas, string strAdd, int iTyp, double dInt, string strOpe)
{
accountInfo tmpAccountInfo;
tmpAccountInfo.strNumber = strNum;
tmpAccountInfo.strName = strNam;
tmpAccountInfo.strPasswd = strPas;
tmpAccountInfo.strAddress = strAdd;
tmpAccountInfo.iType = iTyp;
tmpAccountInfo.dInterest = dInt;
tmpAccountInfo.bLost = false;
tmpAccountInfo.tLostTime = time(nullptr);
tmpAccountInfo.strOperator = strOpe;
vAccount.push_back(tmpAccountInfo);
vAccount[vAccount.size() - 1].vFlow.clear();
/**
* @note do not delete Getfile()!
* i do not think it is necessary, but without it the program has bugs
* it is to refresh the memory
*/
WriteFile();
GetFile();
}
/**
* @brief edit an account
*
* @param [in] strNum number of the account [unchangable]
* @param [in] strNam name of the account owner
* @param [in] strAdd address of the account owner
* @param [in] iType type of the account
* @param [in] dInt interest of the account
*
* requirments of these params @see bankdb.h
*/
void bankdb::EditAccount(string strNum, string strNam, string strAdd, int iTyp, double dInt)
{
vAccount[mAccount[strNum]].strName = strNam;
vAccount[mAccount[strNum]].strAddress = strAdd;
vAccount[mAccount[strNum]].iType = iTyp;
vAccount[mAccount[strNum]].dInterest = dInt;
WriteFile();
GetFile();
}
/**
* @brief edit password
*
* @param [in] strNum number of the account [unchangable]
* @param [in] strPas password of the account
*
* requirments of these params @see bankdb.h
*/
void bankdb::EditPasswd(string strNum, string strPas)
{
vAccount[mAccount[strNum]].strPasswd = strPas;
WriteFile();
}
/**
* @brief marked as lost
*
* @param [in] strNum number of the account [unchangable]
* @param [in] tTim time of the operation
*
* requirments of these params @see bankdb.h
*/
void bankdb::SetLost(string strNum, time_t tTim)
{
vAccount[mAccount[strNum]].bLost = true;
vAccount[mAccount[strNum]].tLostTime = tTim;
WriteFile();
}
void bankdb::RidLost(string strNum)
{
vAccount[mAccount[strNum]].bLost = false;
vAccount[mAccount[strNum]].tLostTime = 0;
WriteFile();
}
/**
* @brief judge whether the account exists
*
* @param [in] strNum number of the account
*
* @retval true finded
* false not finded
*/
bool bankdb::ExistAccount(string strNumber)
{
/**
* @note a common way to find sth. in a map
*/
map<string, int>::iterator iter;
iter = mAccount.find(strNumber);
return (iter != mAccount.end());
}
/**
* @brief judge whether the account number matches the password
*
* @param [in] strNumber number of the account
* @param [in] strPasswd password of the account
*
* @retval true match
* false not match
*/
bool bankdb::CheckAccount(string strNumber, string strPasswd)
{
if (!ExistAccount(strNumber)) /// the account does not exist
return false;
return (vAccount[mAccount[strNumber]].strPasswd == strPasswd);
}
/**
* @brief get name by the account number
*
* @param [in] strNum number of the account
*
* @return the name @see bandb.h
*/
strin
妄北y
- 粉丝: 2w+
- 资源: 1万+
最新资源
- 基于java+ssm+mysql+微信小程序的食堂校园预约就餐小程序 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的食堂线上订餐小程序 源码+数据库+论文(高分毕业设计).zip
- springboot-vue-民谣网站的设计与实现-源码工程-29页从零开始全套图文详解-32页设计论文-21页答辩ppt-全套开发环境工具、文档模板、电子教程、视频教学资源分享
- 基于python编写的视频合成代码
- T型三电平并网逆变器Matlab Simulink仿真模型,采用双闭环控制策略,并网电流外环,电容电流有源阻尼内环,电流波形质量完美, THD不到2%,采用三电平SVPWM算法,大扇区小扇区判断 报
- 河南地下粮仓工艺与设备设计
- 基于java+ssm+mysql+微信小程序的童装购买平台 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的投票评选系统 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的外卖点餐系统 源码+数据库+论文(高分毕业设计).zip
- Javascript数据类型转换规则电脑资料
- 基于java+ssm+mysql+微信小程序的微信评分小程序 源码+数据库+论文(高分毕业设计).zip
- 梯形图转HEX 51plc方案5.6.4.2版本,低成本plc方案,支持温湿度传感器,支持ds18b20.,支持无线联网,支持数码管按钮,最近发现软件在个别系统运行不良,(w764位95%可以用)
- java程序员辞职报告
- 多状态挠曲电结构电性能不确定性分析与仿真
- 微信小程序源码-大学生闲置物品交易平台的分析与设计-微信端-毕业设计源码-期末大作业.zip
- 微信小程序源码-大学生心理健康服务-微信端-毕业设计源码-期末大作业.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈