实现一个银行账号类
功能与现实生活中的atm机相似
以下为代码:
#include
#include"bank.h"
#include
#include
#include
#include
#include
using namespace std;
int find(string zhanghao,vector v1);
int main ()
{
//read the bank account information
ifstream fin1;
ofstream fout1;
ofstream fout2;
fout1.clear();
fout1.open(“bankAccount_log.txt”,ios_base::app);
fin1.open(“bankAccount.csv”);
string s9,s8;
getline(fin1, s9);
vector info;
while(getline(fin1,s9))
{
int n = s9.size();
for (int i = 0; i < n; ++i){
if (s9[i] == ‘,’){
s9[i] = ’ ';
}
}
istringstream out(s9);
while(out>>s8)
info.push_back(s8);
}
// 假设以下容器中的对象为所有的银行卡账号
vector v1;
for(int i = 0 ; i < (info.size()/6) ; i++)
{
Bank a ;
a= Bank(info[6i],info[6i+1],info[6i+2],info[6i+3],info[6i+4],info[6i+5]);
v1.push_back(a);
}
//打印界面
string s=“hello world”;
while(s!=“q”)
{
cout<<“welcome to China Bank!”<<endl;
cout<<“Please enter your account”<<endl;
string password1;
int flag = 0;
cin >> s;
if(s==“q”){
break;
}
int target = find(s,v1);
while(target== -1)
{
cout<<“The account you entered is not exist.\n”<<
“please try again.\n”;
cin>>s;
target = find(s,v1);
}
cout<<“Please enter your password”<<endl;
for(int i=0 ; i<3&&v1[target].showState() ; i++)
{
cin >> password1;
if(password1 == v1[target].repass())
{
flag = 1;
break;
}
else
{
cout<<“Please enter the true password.”<<endl;
}
}
if(flag == 0){
cout<<“You have entereed your password three times wrong”
<<" and you card will be lock.If you have any problems,"
<<“please contact our worker in time.”;
v1[target].set_state(false);
}
else
{
cout<<“Main menu:”<<endl;
cout<<" 1.View my balance"<<endl;
cout<<" 2.Withdraw cash"<<endl;
cout<<" 3.Deposite funds"<<endl;
cout<<" 4.Transfer funds"<<endl;
cout<<" 5.Change the password"<<endl;
cout<<" 6.Exit"<<endl;
int i = 0;
int num,numd,target2,num1;
string ss1;
string pas1,pas2;
while( i != 6)
{
cin>>i;
switch(i)
{
case 1: v1[target].show();break;
case 2:
cout<<"Please enter how much monney you want to withdraw :"<<endl;
cin>>num;
if(num>=v1[target].reonce_limit()&&num>=v1[target].showBalance())
cout<<"The number you want to deposite has over the limit."<<endl;
else
v1[target].WithDraw(num);
fout1<<v1[target].reaccount()<<":"<<endl;
fout1<<"Withdraw monney of "<<num<<endl;
fout1<<"THere is "<<v1[target].showBalance()<<"to be left"<<endl;
break;
case 3:
cout<<"Please enter how much monney you want to deposit :"<<endl;
cin>>numd;
v1[target].Deposit(numd);
fout1<<v1[target].reaccount()<<":"<<endl;
fout1<<"deposit monney of "<<numd<<endl;
fout1<<"THere is "<<v1[target].showBalance()<<"to be left"<<endl<<endl;;
break;
case 4:
cout<<"Please enter the account you want to transfer :"<<endl;
cin>>ss1;
target2 = find(ss1,v1);
while(target2== -1)
{
cout<<"The account you entered is not exist.\n"<<
"please try again.\n";
cin>>ss1;
target2 = find(ss1,v1);
}
cout<<"The number you want to transfer :";
cin>>num1;
if(num1<=v1[target].reonce_limit())
{
v1[target2].Deposit(num1);
fout1<<v1[target2].reaccount()<<":"<<endl;
fout1<<"deposit monney of "<<num1<<endl;
fout1<<"THere is "<<v1[target2].showBalance()<<"to be left"<<endl<<endl;
v1[target].WithDraw(num1);
fout1<<v1[target].reaccount()<<":"<<endl;
fout1<<"Withdraw monney of "<<num<<endl;
fout1<<"THere is "<<v1[target].showBalance()<<"to be left"<<endl<<endl;
}
else
{
cout<<"The number is over the limit ,you can't transfer it"<<endl;
}
break;
case 5:
cout<<"Please enter the new password!"<<endl;
cin>>pas1;
cout<<"Please enter the new password again!"<<endl;
cin>>pas2;
if(pas1==pas2)
v1[target].resetPasswords(pas1);
cout<<"You have change the password sucessfully!"<<endl;
break;
}
}
}
}
//更改账户信息
fout2.clear();
fout2.open(“bankAccount.csv”);
fout2 <<“账号”<<’,’<<“名字”<<’,’<<“余额”<<’,’<<“身份证”<<’,’<<“密码”<<’,’<<“状态”<<endl;
for(int i=0; i<v1.size(); i++)
{
fout2 <<v1[i].reaccount()<<’,’<<v1[i].rename()<<’,’<<v1[i].showBalance()
<<’,’<<v1[i].reid()<<’,’<<v1[i].repass()<<’,’<<v1[i].restate()<<endl;
}
return 0;
}
int find(string zhanghao,vector v1)
{
for(int i=0;i<v1.size();i++)
{
if(v1[i].reaccount() == zhanghao)
return i;
}
return -1;
}