课程设计-银行储蓄系统
bank.h
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:bank.h
* 文件标识:无
* 内容摘要:Bank类和Client类的声明
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#ifndef BANK_H
#define BANK_H
#include <string>
#include "client.h"
#include "clerk.h"
class Bank;
class Client;
// Class Bank -> The core of whole bank. Store the clients list;
class Bank
{
private:
int numberOfClients;
string maxIdNumber;
public:
Client *clients; //clients' list;
Bank();
void addClient(Client&);
void closeClient(string);
string getCurrentMaxID();
int getAccountNumber();
int getPositionByIdNumber(string);
void save();
};
class Client
{
private:
//the basic info of an account;
string accountID;
string name;
string idNumber;
double money;
string address;
string telNum;
string passwd;
int status; //the value of this argument is 0 default and means the account is unverified, 1 point verified, -1 point locked;
//record *records;
public:
Client(){};
Client(string n, string idNum, double m, string addr, string tel, string pass); //create new account;
Client(string id, string n, string idNum, double m, string addr, string tel, string pass, int statu); //read data from file;
//copy object;
Client(Client& newClient);
//some mothed to change the data;
int resetPassWord(string, string );
int resetName(string n);
int resetIdNumber(string );
int resetAddress(string);
int resetTelNumber(string );
//status operations;
bool setStatus(int);
int getStatus();
//money operation;
double getReminingMoney();
double saveMoney(double saveAmountOfMoney);
double drawMoney(double drawAmountOfMoney);
//get the infomation of this object;
string getID();
string getName();
string getIdNumber();
string getAddress();
string getTelNumber();
//verify the password;
bool verifyPassword(string);
friend class Bank;
~Client();
};
#endif // BANK_H
functions.h
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:functions.h
* 文件标识:无
* 内容摘要:The declaration of functions.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#ifndef FUNCTIONS
#define FUNCTIONS
void openAccount();
void closeAccount();
void saveMoney();
void drawMoney();
void inquireInfo();
void transferMoney();
void reportLoss();
void removeLoss();
void changePassword();
string int2str(int num);
int str2int(string);
bool verifyAccount(string id, string passwd);
#endif // FUNCTIONS
bank.cpp
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:bank.cpp
* 文件标识:无
* 内容摘要:The realize of Bank class.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#include "bank.h"
#include <fstream>
#include <string>
#include <string.h>
#include <iostream>
#include "functions.h"
using namespace std;
/************************************************************
* Description : The constructor function of Bank class.
* Arguement : None.
* Returned Value : None.
* **********************************************************/
Bank::Bank()
{
ifstream inConfigfile("config", ios::in);
ifstream inClientfile("accounts", ios::in);
if(!inConfigfile)
{
cerr << "Open file failed.\n";
return;
}
inConfigfile >> numberOfClients >> maxIdNumber;
clients = new Client[numberOfClients];
int i;
for (i = 0; i < numberOfClients; i++)
{
inClientfile >> clients[i].accountID
>> clients[i].name
>> clients[i].idNumber
>> clients[i].money
>> clients[i].address
>> clients[i].telNum
>> clients[i].passwd
>> clients[i].status;
// string tempPath = "reocrd/" + string(clients[i].accountID);
// ifstream inCurrentClientRecord(tempPath, ios::in);
}
inConfigfile.close();
inClientfile.close();
}
/************************************************************
* Description : Save data into file
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void Bank::save()
{
ofstream outConfigfile("config", ios::out);
ofstream outClientfile("accounts", ios::out);
outConfigfile << numberOfClients << " " <<maxIdNumber;
int i;
for (i = 0; i < numberOfClients; i++)
{
outClientfile << clients[i].accountID << " "
<< clients[i].name << " "
<< clients[i].idNumber << " "
<< clients[i].money << " "
<< clients[i].address << " "
<< clients[i].telNum << " "
<< clients[i].passwd << " "
<< clients[i].status << endl;
}
outClientfile.close();
outConfigfile.close();
}
/************************************************************
* Description : Add new client to bank.
* Arguement : client& newClient.
* Returned Value : void.
* **********************************************************/
void Bank::addClient(Client& newClient)
{
//cout << numberOfClients << endl;
numberOfClients += 1;
maxIdNumber = int2str(str2int(maxIdNumber) + 1);
newClient.accountID = maxIdNumber;
newClient.status = 0;
//cout << "Create temp object array successfully and length is " << numberOfClients << endl;
Client *tempObj = new Client[numberOfClients];
//cout << "Create Client array successfully!\n";
for (int i = 0; i < numberOfClients - 1; i++)
{
tempObj[i] = clients[i];
//cout << tempObj[i].accountID << " " << tempObj[i].name << endl;
}
tempObj[numberOfClients - 1] = newClient;
//cout << tempObj[numberOfClients - 1].accountID << " " << tempObj[numberOfClients - 1].name << endl;
//cout << "Copy successfully!\n";
//delete clients;
clients = tempObj;
//cout << "the space of clients has been released\n";
this->save();
}
/************************************************************
* Description : Close an account by account ID.
* Arguement : string id.
* Returned Value : void.
* **********************************************************/
void Bank::closeClient(string id)
{
//int position = this->getPositionByIdNumber(id);
//vector<Client>::iterator po = clients.begin() + position;
//clients.erase(po);
int po = this->getPositionByIdNumber(id);
for (int i = po; i < numberOfClients - 1; i++)
{
clients[i] = clients[i + 1];
}
this->save();
}
/************************************************************
* Description : Return the current max user ID;
* Arguement : None.
* Returned Value : String.
* **********************************************************/
string Bank::getCurrentMaxID()
{
return maxIdNumber;
}
/************************************************************
* Description : Return the account's position in array.
* Arguement : string id.
* Returned Value : int.
* **********************************************************/
int Bank::getPositionByIdNumber(string id)
{
int i;
bool accountIsexisted = false;
int returnValue;
for (i = 0; i < numberOfClients; i++)
{
if (clients[i].getID() == id)
{
accountIsexisted = true;
returnValue = i;
break;
}
}
if(accountIsexisted == true)
{
return returnValue;
}else {
return -1;
}
}
//int Bank::getAccountNumber()
//{
// return numberOfClients;
//}
client.cpp
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:client.cpp
* 文件标识:无
* 内容摘要:The realize of Client class.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#include <string>
#include <string.h>
#include "bank.h"
#include "functions.h"
using namespace std;
extern Bank bank;
Client::Client(string n, string idNum, double m, string addr, string tel, string pass)
{
accountID = int2str(str2int(bank.getCurrentMaxID()) + 1);
name = n;
idNumber = idNum;
money = m;
address = addr;
telNum = tel;
passwd = pass;
status = 0;
}
Client::Client(string id, string n, string idNum, double m, string addr, string tel, string pass, int statu)
{
accountID = id;
name = n;
idNumber = idNum;
money = m;name = n;
idNumber = idNum;
money = m;
address = addr;
telNum = tel;
passwd = pass;
address = addr;
telNum = tel;
passwd = pass;
status = statu;
}
int Client::resetPassWord(string oldPasswd, string newPasswd)
{
if (status == -1)
{
return -1;
}
else if(passwd == oldPasswd)
{
passwd = newPasswd;
return 1;
}
else {
return 0;
}
}
int Client::resetName(string n)
{
if(status == 1)
{
name = n;
return 1;
}else if(status == 0) {
return 0;
}else {
return -1;
}
}
int Client::resetIdNumber(string newIdNum)
{
if(status == 1)
{
idNumber = newIdNum;
return 1;
}else if(status == 0) {
return 0;
}else {
return -1;
}
}
int Client::resetAddress(string newAddr)
{
if(status == 1)
{
address = newAddr;
return 1; //successful
}else if(status == 0) {
return 0;
}else {
return -1;
}
}
int Client::resetTelNumber(string newTel)
{
if(status == 1)
{
telNum = newTel;
return 1;
}else if(status == 0) {
return 0;
}else {
return -1;
}
}
bool Client::setStatus(int newStatus)
{
status = newStatus;
if(status == newStatus)
{
return true;
}else {
return false;
}
}
double Client::getReminingMoney()
{
if(status == 1)
{
return money;
}else if(status == -1) {
return -1;
}else {
return -2; //unverified infomation: avoid the problem that moneyis 0 too;
}
}
double Client::saveMoney(double saveAmountOfMoney)
{
if(status == 1 || status == 0)
{
money += saveAmountOfMoney;
return money;
} else if (status == -1)
{
return -1;
}
}
double Client::drawMoney(double drawAmountOfMoney)
{
if(status == 1)
{
if (drawAmountOfMoney > money)
{
return -3; //code -3 means there is not enough money.
}
{
money -= drawAmountOfMoney;
return money;
}
}else if (status == -1)
{
return -1;
}else {
return -2;
}
}
string Client::getID()
{
return accountID;
}
string Client::getName()
{
return name;
}
string Client::getIdNumber()
{
return idNumber;
}
string Client::getAddress()
{
return address;
}
string Client::getTelNumber()
{
return telNum;
}
int Client::getStatus()
{
return status;
}
Client::~Client()
{
//write the information in file;
}
Client::Client(Client &newClient)
{
this->accountID = newClient.accountID;
this->name = newClient.name;
this->idNumber = newClient.idNumber;
this->money = newClient.money;
this->address = newClient.address;
this->telNum = newClient.telNum;
this->passwd = newClient.passwd;
this->status = newClient.status;
}
bool Client::verifyPassword(string Passwd)
{
if(passwd == Passwd)
{
return true;
}else {
return false;
}
}
bank_func.cpp
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:bank_func.cpp
* 文件标识:无
* 内容摘要:functions of basic operation for system.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
//
#include <string>
#include "bank.h"
#include "functions.h"
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
extern Bank bank;
extern int current;
/************************************************************
* Description : function 1 - Open an account.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void openAccount() //open an account;
{
string name;
string IdNumber;
double money;
string address;
string telNum;
string passwd;
string passwd2;
cout << "Number : " << int2str(str2int(bank.getCurrentMaxID()) + 1) << endl;
cout << "Enter name : ";
cin >> name;
cout << "Enter ID number : ";
cin >> IdNumber;
cout << "Enter address : ";
cin >> address;
cout << "Enter Telephone number : ";
cin >> telNum;
cout << "Enter password : ";
cin >> passwd;
cout << "Enter passwd again : ";
cin >> passwd2;
if(passwd == passwd2)
{
cout << "Enter money : ";
cin >> money;
Client newClient(name, IdNumber, money, address, telNum, passwd);
bank.addClient(newClient);
cout << "Open account successfully!\n";
} else {
cout << "Open account failed!\n";
}
//bank.save();
}
/************************************************************
* Description : function 2 - Delete an account.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void closeAccount()
{
string idWillBeClosed;
cout << "Enter ID : ";
cin >> idWillBeClosed;
bank.closeClient(idWillBeClosed);
}
/************************************************************
* Description : function 3 - Save money.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void saveMoney()
{
double moneyWillSave, back;
cout << "Enter the money you will save : ";
cin >> moneyWillSave;
back = bank.clients[current].saveMoney(moneyWillSave);
if(back == -1)
{
cout << "Account has been locked. Please Unlock it first!\n";
} else if (back >= 0)
{
cout << "Save money successfully!\n";
cout << "There is $" << back << " in your account now!\n";
}
}
/************************************************************
* Description : function 4 - Draw money.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void drawMoney()
{
double moneyWillDraw, back;
cout << "Enter the money you will draw : ";
cin >> moneyWillDraw;
back = bank.clients[current].drawMoney(moneyWillDraw);
if(back >= 0)
{
cout << "Draw moeny successfully!\n";
cout << "There is $" << back << " in your account now!\n";
}else if (back == -1){
cout << "Account has been locked.Please Unlock it first!\n";
}else if (back == -3)
{
cout << "Sorry, this account doesn't have enough money!\n";
}
}
/************************************************************
* Description : function 5 - inquire client's infomation.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void inquireInfo()
{
cout << "Account ID : " << bank.clients[current].getID()
<< "\nName : " << bank.clients[current].getName()
<< "\nMoney : " << bank.clients[current].getReminingMoney()
<< "\nAddress : " << bank.clients[current].getAddress()
<< "\nTelephone : " << bank.clients[current].getTelNumber() << endl;
}
/************************************************************
* Description : function 6 - transfer money.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void transferMoney()
{
string targetAccountID;
double moneyWillBeTransfered, backValue;
bool markAsHasDrawMoeny = false;
cout << "Enter the target account : ";
cin >> targetAccountID;
cout << "Enter the money you will transfer : ";
cin >> moneyWillBeTransfered;
backValue = bank.clients[current].drawMoney(moneyWillBeTransfered);
if (backValue == -3)
{
cout << "Sorry, this account doesn't have enough money!\n";
return;
} else if (backValue == -1)
{
cout << "Account has been locked.Please Unlock it first!\n";
return;
}else if (backValue >= 0)
{
markAsHasDrawMoeny = true;
}
int po = bank.getPositionByIdNumber(targetAccountID);
if (po == -1)
{
cout << "Target account is not existed!\n";
}else {
backValue = bank.clients[po].saveMoney(moneyWillBeTransfered);
if(backValue == -1)
{
cout << "Target account has been locked.\n";
return;
}else if (backValue >= 0 && markAsHasDrawMoeny == true)
{
cout << "Transfer money successfully!\n";
cout << "There is $" << backValue << " in your account now!\n";
}else {
cout << "There is something wrong with the program.T T\n" << "Back code is " << backValue << endl;
}
}
}
/************************************************************
* Description : function 7 - report the loss.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void reportLoss()
{
string targetAccountID;
cout << "Enter the Account ID : ";
cin >> targetAccountID;
bank.clients[bank.getPositionByIdNumber(targetAccountID)].setStatus(-1);
cout << "Lock account successfully!\n";
}
/************************************************************
* Description : function 8 - remove loss report.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void removeLoss()
{
string targetAccountID;
cout << "Enter the Account ID : ";
cin >> targetAccountID;
bank.clients[bank.getPositionByIdNumber(targetAccountID)].setStatus(0);
cout << "Unlock Account successfully!\n";
}
/************************************************************
* Description : function 9 - change password.
* Arguement : None.
* Returned Value : void.
* **********************************************************/
void changePassword()
{
string oldPassword;
string newPassword;
int backValue;
cout << "Enter the current password : ";
cin >> oldPassword;
cout << "Enter the new password : ";
cin >> newPassword;
backValue = bank.clients[current].resetPassWord(oldPassword, newPassword);
if (backValue == 1)
{
cout << "Change password successfully!\n";
} else if (backValue == 0)
{
cout << "Sorry, wrong password!\n";
} else if (backValue == -1)
{
cout << "Your account has been locked, please unlock and try again.\n";
}
}
other_func.cpp
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:other_func.cpp
* 文件标识:无
* 内容摘要:Other functions.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#include "bank.h"
#include <string>
#include <sstream>
#include <iostream>
#include <cstdio>
using namespace std;
extern Bank bank;
/************************************************************
* Description : convert int type to string.
* Arguement : int num.
* Returned Value : string.
* **********************************************************/
string int2str(int num) //convert int type to string;
{
stringstream ss;
string temp;
ss << num;
ss >> temp;
return temp;
}
/************************************************************
* Description : convert string to int.
* Arguement : string str.
* Returned Value : int.
* **********************************************************/
int str2int(string str) //convert string to int;
{
stringstream ss;
int temp;
ss << str;
ss >> temp;
return temp;
}
/************************************************************
* Description : verfiy client account by id and password.
* Arguement : string id, string passwd.
* Returned Value : bool.
* **********************************************************/
bool verifyAccount(string id, string passwd) //verfiy client account by id and password;
{
int po = bank.getPositionByIdNumber(id);
if(po == -1)
{
cout << "Account is not existed!\n";
return false;
}else {
return bank.clients[po].verifyPassword(passwd);
}
}
main.cpp
/**********************************************************************
* 版权所有 (c)2015, Zang Yunji.
*
* 文件名称:main.cpp
* 文件标识:无
* 内容摘要:Main process control.
* 其他说明:无
* 当前版本:V1.0
* 作 者:臧云吉
* 完成日期:2015年7月17日
*
* 修改记录:无
* ********************************************************************/
#include <iostream>
#include "bank.h"
#include "functions.h"
#include <string>
using namespace std;
Bank bank;
int current;
int main()
{
while(1)
{
int mode;
cout << "Log on as 1.Clerk / 2.Client\n";
cin >> mode;
if (mode == 1){
while(1)
{
string inputClerk;
string ClerkPasswd;
bool backToMainMenu = false;
cout << "Enter Clerk name : ";
cin >> inputClerk;
cout << "Enter Password : ";
cin >> ClerkPasswd;
if(inputClerk == "admin" && ClerkPasswd == "123456")
{
while(1)
{
int option;
cout << "+-------------------------------------+" << endl
<< "| 1. Open Account 2. Close Account |" << endl
<< "| 3. Report Loss 4. Unlock Account |" << endl
<< "| 0. Exit |" << endl
<< "+-------------------------------------+" << endl;
cin >> option;
if(option == 1)
{
openAccount();
} else if (option == 2)
{
closeAccount();
} else if (option == 3)
{
reportLoss();
} else if (option == 4)
{
removeLoss();
} else if (option == 0)
{
backToMainMenu = true;
break;
} else {
continue;
}
}
}else {
cout << "Wrong Name or Password!\n";
break;
}
if(backToMainMenu == true)
{
break;
}
}
}else if (mode == 2){
while(1)
{
string idWillEnter;
string passwordWillEnter;
cout << "Enter your account ID : ";
cin >> idWillEnter;
cout << "Enter password : ";
cin >> passwordWillEnter;
bool backToMainMenu = false;
if(verifyAccount(idWillEnter, passwordWillEnter))
//if(idWillEnter == "111" && passwordWillEnter == "111")
{
current = bank.getPositionByIdNumber(idWillEnter);
bank.clients[current].setStatus(1);
cout << "Welcome to our Bank. The status of your account is ";
if(bank.clients[current].getStatus() == -1)
{
cout << "Locked! Most functions is disabled!";
}else {
cout << "Healthy! Have a good use!\n";
}
while(1)
{
int option;
cout << "+-----------------------------------------------------------+" << endl
<< "| 1. Save Money 2. Draw Money 3. Inquire Info |" << endl
<< "| 4. Transfer 5. Change Password 0. Exit Account |" << endl
<< "+-----------------------------------------------------------+" << endl;
cin >> option;
if (option == 1)
{
saveMoney();
} else if (option == 2)
{
drawMoney();
} else if (option == 3)
{
inquireInfo();
} else if (option == 4)
{
transferMoney();
} else if (option == 5)
{
changePassword();
}else if (option == 0){
bank.clients[current].setStatus(0);
backToMainMenu = true;
break;
}else {
continue;
}
}
if (backToMainMenu == true)
{
break;
}
}else {
continue;
}
}
}else {
continue;
}
}
}
运行结果