// ************************************
// * *
// * ATM类的成员函数 *
// * *
// ************************************
#include "function.h"
#include <string.h>
#include<stdlib.h>
void ATM::welcome()
{
times=0;
cout<<"$
欢迎使用若雪银行ATM自动取款机!~!
"<<endl;
char pwd[8],num[20],ch;
int i=0;
do
{
i=0;
cout<<endl<<"请输入卡号:";
do
{
cin.get(ch);
num[i++]=ch;
}while(ch!='\n');
num[i-1]='\0';
i=0;
cout<<"请输入密码:";
do
{
cin.get(ch);
pwd[i++]=ch;
}while(ch!='\n');
pwd[i-1]='\0';
if(!check_passwd(num,pwd))
{
cout<<"你输入的卡号或密码有误,
请重新输入"<<endl;
times++;
}
else
{
functionshow();
}
}while(times<3);
lock();
}
bool ATM::check_passwd(char num[],
char pwd[])
{
if(strcmp(num,cnsm.get_num())==0&&strcmp
(pwd,cnsm.get_passwd())==0)
return true;
else
return false;
}
void ATM::functionshow()
{
int n;
do
{
cout<<endl<<"请你输入相应的操作序号进行操作:
"<<endl;
cout<<"1) 修改密码 "<<endl
<<"2) 取款 "<<endl
<<"3) 查询余额 "<<endl
<<"4) 退出系统 "<<endl;
cout<<"$ >\\";
cin>>n;
while(n<1||n>4)
{
cout<<"请输入正确的操作序号!"<<endl;
cout<<"$ >\\";
cin>>n;
}
switch(n)
{
case 1: change_passwd();
break;
case 2: fetchmoney();
break;
case 3: information();
break;
case 4: exitATM();
break;
}
}while(true);
}
void ATM::change_passwd()
{
char pwd[8],repwd[8];
times=0;
do
{
cout<<endl<<"请输入旧密码:";
cin>>pwd;
if(!check_passwd(cnsm.get_num(),pwd))
times++;
else
break;
}while(times<3);
if(times==3)
lock();
int t=0;
do
{
cout<<"请输入新密码:";
cin>>pwd;
cout<<"请再输入一次新密码:";
cin>>repwd;
if((t=strcmp(pwd,repwd))!=0)
cout<<"你输入的两次密码不一样,
请重新输入!"<<endl;
}while(t!=0);
cnsm.set_passwd(pwd);
cout<<"密码修改成功,请牢记!"<<endl;
}
void ATM::fetchmoney()
{
float m;
char ch;
do
{
cout<<endl<<"你要取多少钱:"
<<"\n$>\\"<<endl ;
cin>>m;
while(m<=0)
{
cout<<"请输入正确的数字!"<<endl;
cout<<"$ >\\ ";
cin>>m;
}
if(cnsm.get_money()-m<0)
{
cout<<"对不起,你的余额不足!"
<<endl;
}
else
{
cout<<endl<<"操作成功,请收好钱!"
<<endl;
cnsm.set_money(m);
}
cout<<"是否要继续该项操作:(Y/N) "
<<endl;
cout<<"$ >\\ ";
cin>>ch;
while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')
{
cout<<"$ >\\";
cin>>ch;
}
}while(ch=='y'||ch=='Y');
}
void ATM::information()
{
cout<<"**********************************"<<endl;
cout<<"*"<<endl;
cout<<"* 用户姓名:"<<cnsm.get_name()<<endl;
cout<<"* 卡号: "<<cnsm.get_num()<<endl;
cout<<"* 余额: "<<cnsm.get_money()<<endl;
cout<<"**********************************"<<endl;
}
void ATM::lock()
{
cout<<endl<<"对不起,由于你的操作有误,
你的卡已经被没收! "<<endl;
exit(1);
}
void ATM::exitATM()
{
cout<<endl<<"感谢你对本银行的支持,
欢迎下次光临!"<<endl;
cout<<"请取卡……"<<endl;
exit(0);
}