要求:大概说一下吧,就是要创建一个用户(初始化一账户),模拟ATM的业务(取款,100的整数倍,改密 码,查剩余金额。等等,各种简单繁琐的操作 ;-) )
直接贴代码吧:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
class consumer;
class ATM // ATM取款机
{
public:
ATM(consumer& cn):cnsm(cn){}
void welcome(); // 登陆界面
bool check_passwd(char n[],char pwd[]); // 核对password
void change_passwd(); // 改动password
void fetchmoney(); // 取款
void information(); // 查询信息
void exitATM(); // 退出系统
void functionshow(); // 功能界面
void lock(); // 锁机
private:
int times; // 记录password输入次数
consumer& cnsm;
};
class consumer // 用户
{
public:
friend class ATM;
consumer(char Name[],char Num[],float Money,char Password[]);
protected:
char* get_name(); // 取得姓名
char* get_num(); // 取得卡号
char* get_passwd(); // 取得password
float get_money(); // 取得剩余金额
void set_passwd(char pwd[]); // 设置password
void set_money(float m); // 取钱
private:
char passwd[8]; // 用户password
char name[20]; // 用户姓名
char num[20];
float money;
};
consumer::consumer(char Name[],char Num[],float Money,char Password[])
{
strcpy(name,Name);
strcpy(num,Num);
money=Money;
strcpy(passwd,Password);
}
float consumer::get_money()
{
return money;
}
char* consumer::get_name()
{
return name;
}
char* consumer::get_num()
{
return num;
}
char* consumer::get_passwd()
{
return passwd;
}
void consumer::set_money(float m)
{
money-=m;
}
void consumer::set_passwd(char pwd[])
{
strcpy(passwd,pwd);
}
void ATM::welcome()
{
times=0;
cout<<" 欢迎使用tdap银行ATM自己主动取款机 "<< endl;
char pwd[8],num[20],ch;
int i=0;
do
{
i=0;
cout<<"请输入卡号:";
do
{
cin.get(ch);
num[i++]=ch;
}while(ch!='\n');
num[i-1]='\0';
i=0;
cout<<"请输入password:";
do
{
cin.get(ch);
pwd[i++]=ch;
}while(ch!='\n');
pwd[i-1]='\0';
if(!check_passwd(num,pwd))
{
cout<<"你输入的卡号或password有误。请又一次输入"<<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;
cout<<"1) 改动password "<<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: exit(0); break;
}
}while(true);
}
void ATM::change_passwd(){
char pwd[8],repwd[8];
times=0;
do{
cout<<"请输入旧password:";
cin>>pwd;
if(!check_passwd(cnsm.get_num(),pwd))
times++;
else
break;
}while(times<3);
if(times==3)
lock();
int t=0;
do{
cout<<"请输入新password:";
cin>>pwd;
cout<<"请再输入一次新password:";
cin>>repwd;
if((t=strcmp(pwd,repwd))!=0)
cout<<"你输入的两次password不一样,请从新输入!"<<endl;
}while(t!=0);
cnsm.set_passwd(pwd);
cout<<"password改动成功,请牢记!"<< endl;
}
void ATM::fetchmoney(){
int m;
char ch;
do{
cout<<"你要取多少钱:"<<endl<<"$>";
cin>>m;
while(m<=0 || m%100 !=0 ){
cout<<"请输入正确的数字!"<<endl;
cout<<"$> ";
cin>>m;
}
if(cnsm.get_money()-m<0){
cout<<"对不起,你的剩余金额不足!"<<endl;
}else{
cout<<"操作成功,请收好钱!"<<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; cout<<"请取卡……"<<endl; exit(0); } int main(){ consumer c1("jim","12345",10000.0f,"123"); // 先构造一个用户 ATM atm(c1); atm.welcome(); return 0; }
今天帮人写的第二篇课设 。 ;-) 机智的窝
要求:大概说一下吧,就是要创建一个用户(初始化一账户),模拟ATM的业务(取款,100的整数倍,改密 码,查剩余金额。等等,各种简单繁琐的操作 ;-) )
直接贴代码吧:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
class consumer;
class ATM // ATM取款机
{
public:
ATM(consumer& cn):cnsm(cn){}
void welcome(); // 登陆界面
bool check_passwd(char n[],char pwd[]); // 核对password
void change_passwd(); // 改动password
void fetchmoney(); // 取款
void information(); // 查询信息
void exitATM(); // 退出系统
void functionshow(); // 功能界面
void lock(); // 锁机
private:
int times; // 记录password输入次数
consumer& cnsm;
};
class consumer // 用户
{
public:
friend class ATM;
consumer(char Name[],char Num[],float Money,char Password[]);
protected:
char* get_name(); // 取得姓名
char* get_num(); // 取得卡号
char* get_passwd(); // 取得password
float get_money(); // 取得剩余金额
void set_passwd(char pwd[]); // 设置password
void set_money(float m); // 取钱
private:
char passwd[8]; // 用户password
char name[20]; // 用户姓名
char num[20];
float money;
};
consumer::consumer(char Name[],char Num[],float Money,char Password[])
{
strcpy(name,Name);
strcpy(num,Num);
money=Money;
strcpy(passwd,Password);
}
float consumer::get_money()
{
return money;
}
char* consumer::get_name()
{
return name;
}
char* consumer::get_num()
{
return num;
}
char* consumer::get_passwd()
{
return passwd;
}
void consumer::set_money(float m)
{
money-=m;
}
void consumer::set_passwd(char pwd[])
{
strcpy(passwd,pwd);
}
void ATM::welcome()
{
times=0;
cout<<" 欢迎使用tdap银行ATM自己主动取款机 "<< endl;
char pwd[8],num[20],ch;
int i=0;
do
{
i=0;
cout<<"请输入卡号:";
do
{
cin.get(ch);
num[i++]=ch;
}while(ch!='\n');
num[i-1]='\0';
i=0;
cout<<"请输入password:";
do
{
cin.get(ch);
pwd[i++]=ch;
}while(ch!='\n');
pwd[i-1]='\0';
if(!check_passwd(num,pwd))
{
cout<<"你输入的卡号或password有误。请又一次输入"<<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;
cout<<"1) 改动password "<<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: exit(0); break;
}
}while(true);
}
void ATM::change_passwd(){
char pwd[8],repwd[8];
times=0;
do{
cout<<"请输入旧password:";
cin>>pwd;
if(!check_passwd(cnsm.get_num(),pwd))
times++;
else
break;
}while(times<3);
if(times==3)
lock();
int t=0;
do{
cout<<"请输入新password:";
cin>>pwd;
cout<<"请再输入一次新password:";
cin>>repwd;
if((t=strcmp(pwd,repwd))!=0)
cout<<"你输入的两次password不一样,请从新输入!"<<endl;
}while(t!=0);
cnsm.set_passwd(pwd);
cout<<"password改动成功,请牢记!"<< endl;
}
void ATM::fetchmoney(){
int m;
char ch;
do{
cout<<"你要取多少钱:"<<endl<<"$>";
cin>>m;
while(m<=0 || m%100 !=0 ){
cout<<"请输入正确的数字!"<<endl;
cout<<"$> ";
cin>>m;
}
if(cnsm.get_money()-m<0){
cout<<"对不起,你的剩余金额不足!"<<endl;
}else{
cout<<"操作成功,请收好钱!"<<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; cout<<"请取卡……"<<endl; exit(0); } int main(){ consumer c1("jim","12345",10000.0f,"123"); // 先构造一个用户 ATM atm(c1); atm.welcome(); return 0; }