#include "atm.h"
#include "head.h"
ATM::ATM():full(10000),unit(10),up(100),fetchcash(100),savecash(9900)
{
strcpy(chinarea,"100101");
}
bool ATM::search()
{
char c[7];
bool b=false;
char suffix[]=".dat";
for (int i=0;i<6;i++)
{
c[i]=IDIn[i];
}
c[6]='\0';
if(strcmp(c,chinarea)==0)
{
strcat(IDIn,suffix); //字符连接
fstream infile(IDIn,ios_base::in|ios_base::binary);
if(!infile) cerr<<'\t'<<" 不存在此用户! 欢迎下次光临!"<<endl<<endl;
else
{
infile.seekg(-(1*sizeof(a)),ios::end);
infile.read((char*) & a,sizeof(a));
if(a.getpermit()==true ) b=true;
else cout<<'\t'<<" 此用户已被冻结!"<<endl<<endl;
}
infile.close();
}
else cout<<'\t'<<" 非本行账户,欢迎下次光临! "<<endl<<endl;
t.run(5);
return b;
}
void ATM::rewrite()
{
fstream ofile(IDIn,ios_base::binary|ios_base::out|ios_base::in);
if(!ofile)
{
cerr<<"open error!"<<endl;
abort();
}
ofile.seekp(0,ios::end);
ofile.write((char * ) &a,sizeof(a));
ofile.close();
}
void ATM::getmoney()
{
float m=0;
char ch='y';
do
{
system("cls");
if(fetchcash>0)
{
cout<<endl;
cout<<'\t'<<'\t'<<"********** 请输入要取的钱数: **********" <<'\n'<<'\t'<<'\t'<<">>>>>> ";
cin>>m;
cout<<endl;
if (m<=0) cout<<'\t'<<'\t'<<" 对不起,所取的钱数不能是零或负!"<<endl;
else if (m>up) cout<<'\t'<<'\t'<<" 对不起,一次取钱上限是100元!"<<endl;
else if (m>fetchcash) cout<<'\t'<<'\t'<<" 对不起,本机现金不够!"<<endl;
else if (int(m)%unit != 0) cout<<'\t'<<'\t'<<" 对不起,所取钱数必须是10的整数倍!"<<endl;
else if (a.getmoney()-m<0) cout<<'\t'<<'\t'<<" 对不起,你卡内的余额不足!"<<endl;
else
{
system("cls");
cout<<endl;
cout<<'\t'<<'\t'<<"***************** 请等待,正在进行操作 **************"<<endl;
t.run(3);
system("cls");
cout<<endl<<'\t'<<'\t'<<"操作成功,请收好钱!"<<endl;
a.cutmoney(m);
fetchcash-=m;
}
m=0;
rewrite();
t.run(5);
system("cls");
cout<<endl;
cout<<'\t'<<'\t'<<" 是否要继续取款操作:(Y/N) " <<endl;
cout<<'\t'<<'\t'<<" >>>>>> ";
cin.ignore();
cin>>ch;
while(ch!='n' && ch!='N' && ch!='Y' && ch!='y')
{
cout<<'\t'<<'\t'<<" >>>>>> ";
cin.ignore();
cin>>ch;
}
}
else
{
cout<<endl;
cout<<'\t'<<'\t'<<" 本机可以提取的现金已空!!"<<endl;
t.wait(5);
break;
}
} while(ch=='y'||ch=='Y');
}
void ATM::savemoney()
{
float m=0;
char ch='y';
bool ans;
do
{
system("cls");
if(savecash<full)
{
cout<<endl<<'\t'<<'\t'<<"************ 请放入存款: ************"<<endl<<'\t'<<">>>>>> ";
cin>>m;
system("cls");
cout<<endl;
cout<<'\t'<<'\t'<<"**************** 请等待,正在进行操作 **************"<<endl;
t.run(5);
if (m<=0) cout<<'\t'<<'\t'<<" 对不起,所存的钱数不能是零或负!"<<endl;
else if (m>up) cout<<'\t'<<'\t'<<" 对不起,一次存款的上限是100元!"<<endl;
else if (int(m)%unit != 0) cout<<'\t'<<'\t'<<" 对不起,所存钱数必须是10的整数倍!"<<endl;
else if (m+savecash>full) cout<<'\t'<<'\t'<<" 对不起,不能接受,超出本机最大现金容量!"<<endl;
else
{
system("cls");
cout<<'\t'<<'\t'<<" 存款数目是 "<<m<<" 元,请确认! 1--确认 0--退款 "<<endl;
cout<<'\t'<<'\t'<<" >>>>>> ";
cin.ignore();
cin>>ans;
t.run(3);
system("cls");
if (ans==1)
{
cout<<endl<<'\t'<<'\t'<<" 操作成功,存款成功! "<<endl;
a.addmoney(m);
savecash+=m;
rewrite();
}
else cout<<endl<<'\t'<<'\t'<<" 请收好您的钱!"<<endl;
}
m=0;
t.wait(3);
system("cls");
cout<<'\t'<<'\t'<<" 是否要继续存款操作:(Y/N) "<<endl;
cout<<'\t'<<'\t'<<" >>>>>> ";
cin.ignore();
cin>>ch;
while(ch!='n' && ch!='N' && ch!='Y' && ch!='y')
{
cout<<'\t'<<'\t'<<" >>>>>> ";
cin.ignore();
cin>>ch;
}
}
else
{
cout<<endl<<'\t'<<'\t'<<'\t'<<"本机存款空间已满!"<<endl;
t.wait(5);
break;
}
}while(ch=='y'||ch=='Y');
}
bool ATM::testpassword()
{
int tt=0;
char P[20];
char c;
cout<<endl<<'\t'<<" 请输入户主名:";
gets(IDIn);
bool b=search();
if(b==true)
{
do
{ system("cls");
cout<<endl<<'\t'<<" 请输入密码:";
for(int i=0;i<20;i++)
{
c=getch();
if(c==13)
break;
if(c==8)
{
cout<<"\b \b";
i=i-2;
}
else
{
P[i]=c;
cout<<"*";
}
}
P[i]='\0';
char *ppa=a.getpassword();
if(strcmp(ppa,P)!=0)
{
cout<<endl;
cout<<endl<<'\t'<<" 密码输入错误,请重新输入!"<<endl;
tt++;
t.wait(3);
}
else break;
} while(tt<3);
if(tt>=3)
{
system("cls");
a.fpermit();
rewrite();
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t连续三次输入错误,吞卡,账户被冻结!"<<endl;
t.run(5);
return false;
}
else
{
cout<<endl;
cout<<'\t'<<" 密码输入正确!!!"<<endl;
t.wait(3);
return true;
}
}
else return false;
}
void ATM::lookaccount()
{
system("cls");
cout<<endl;
cout<<'\t'<<'\t'<<" 存款余额为: "<<a.getmoney()<<" 元"<<endl;
cout<<endl;
t.run(6);
}
void ATM::rewritepass()
{
char p1[20],p2[20];
char ch,ch1;
int i;
int j;
int k;
for(k=0;k<3;k++)
{
system("cls");
cout<<endl<<'\t'<<'\t'<<" 请输入修改后的密码: ";
for(i=0;i<20;i++)
{
ch=getch();
if(ch==13)
break;
if(ch==8)
{
cout<<"\b \b";
i=i-2;
}
else
{
p1[i]=ch;
cout<<'*';
}
}
p1[i]='\0';
cout<<endl;
cout<<'\t'<<'\t'<<" 请再输入一次密码: ";
for(j=0;j<20;j++)
{
ch1=getch();
if(ch1==13)
break;
if(ch1==8)
{
cout<<"\b \b";
j=j-2;
}
else
{
p2[j]=ch1;
cout<<'*';
}
}
p2[j]='\0';
cout<<endl;
if(strcmp(p1,p2)==0)
{
cout<<'\t'<<'\t'<<" 两次密码输入相同,修改成功!"<<endl;
a.setpass(p1);
rewrite();
t.run(5);
break;
}
else
{
cout<<'\t'<<'\t'<<" 两次密码不同,请重新输入!"<<endl;
t.wait(3);
}
}
if(k>=3) {cout<<'\t'<<'\t'<<" 返回,修改密码失败!"<<endl;t.wait(3);}
}
void ATM::setchinarea()
{
gets(chinarea);
}