詳細解析C++編寫的ATM自動取款機模擬程序
更新時間: 2007-05-25 09:21:46來源: 粵嵌教育瀏覽量:748
學習c++有一段時間了,前兩天有個朋友要我幫她做個模擬ATM自動取款機的程序,于是花了一個晚上寫了出來,其實這個程序也很簡單,但是我覺得它對于剛學c++的人來說比較有用處,因為它可以幫助你更加深刻的理解面向對象程序設計的真諦-------以現實世界為模型編寫程序。學習c++的真正目的也就在于此,真正的理解面向對象程序設計!
// ************************************
// * *
// * function.h *
// * *
// ************************************
#include<iostream.h>
class consumer;
class ATM
// ATM取款機
{
public:
ATM(consumer& cn):cnsm(cn)
{
}
void welcome();
// 登陸界面
bool check_passwd(char n[],char pwd[]);
// 核對密碼
void change_passwd();
// 修改密碼
void fetchmoney();
// 取款
void information();
// 查詢信息
void exitATM();
// 退出系統
void functionshow();
// 功能界面
void lock();
// 鎖機
private:
int times;
// 記錄密碼輸入次數
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();
// 取得密碼
float get_money();
// 取得余額
void set_passwd(char pwd[]);
// 設置密碼
void set_money(float m);
// 取錢
private:
char passwd[8];
// 用戶密碼
char name[20];
// 用戶姓名
char num[20];
float money;
};
// ************************************
// * *
// * consumer類的成員函數 *
// * *
// ************************************
#include"function.h"
#include<string.h>
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);
}
// ************************************
// * *
// * 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);
}
// ************************************
// * *
// * ATM.cpp *
// * *
// ************************************
#include<iostream.h>
#include"function.h"
void main()
{
consumer c1("jim","12345",5200.3f,"123"); // 先構造一個用戶
ATM atm(c1);
atm.welcome();
}