标签:
不好意思!但是现在这个程序比上次那个好多了,而且没有重复,程序看起来比较简练,以下是新程序:
#include<iostream>
#include<string>
using namespace std;
class consumer
{
public:
void int_consumer(string N ,string I,string P,float M); //初始化
string get_name(); //读取姓名
string get_id(); //读取账号
string get_password(); //读取密码
float get_money(); //读取金额
void set_money(float MM);
private:
string name;
string id;
string password;
float money;
};
class bank
{
public:
void welcome(); //欢迎界面
void exit_system(); //退出系统
void menu(); //选择菜单
int seek(string); //查找卡号
bool check();
void int_consumer(int i,string N,string I,string P,float M);
void change_password(consumer &cs);
void show_me(consumer& cs);
void transfer_money(consumer& cs);
void withdraw_money(consumer& cs);
void deposit_money(consumer& cs);
private:
int i;
string pass;
consumer cs[4];
};
void consumer::set_money(float MM)
{
money=MM;
}
void consumer::int_consumer(string N,string I,string P,float M)
{
name=N;
id=I;
password=P;
money=M;
}
string consumer::get_name()
{
return name;
}
string consumer::get_id()
{
return id;
}
string consumer::get_password()
{
return password;
}
float consumer::get_money()
{
return money;
}
void bank::welcome()
{
cout<<"**********欢迎使用**********"<<endl;
cout<<"请输入卡号:"<<endl;
string id;
int flag=-1;
cin>>id;
flag=seek(id);
}
void bank::transfer_money(consumer& cs)
{
cout<<"请输入对方账号"<<endl;
int a1;
cin>>a1;
cout<<"请输入转账金额"<<endl;
float a;
cin>>a;
if(cs.get_money()-a<0)
{
cout<<"对不起你的余额不足"<<endl;
}
else
{
cs.set_money(cs.get_money()-a);
cout<<"转账成功"<<endl;
}
}
void bank::exit_system()
{
cout<<"**********谢谢使用**********"<<endl;
cout<<"********请收好您的卡********"<<endl;
exit(0);
}
void bank::menu()
{
int n;
do
{
cout<<"**********************"<<endl;
cout<<"*请输入相应的操作序号*"<<endl;
cout<<"**** 1 修改密码 ****"<<endl;
cout<<"**** 2 进行取款 ****"<<endl;
cout<<"**** 3 显示余额 ****"<<endl;
cout<<"**** 4 退出系统 ****"<<endl;
cout<<"**** 5 进行转账 ****"<<endl;
cout<<"**** 6 进行存款 ****"<<endl;
cout<<"**********************"<<endl;
cin>>n;
while(n<1||n>6)
{
cout<<"操作错误,请重新输入!"<<endl;
cin>>n;
}
switch(n)
{
case 1: change_password(cs[i]);break;
case 2: withdraw_money(cs[i]);break;
case 3: show_me(cs[i]);break;
case 4: exit_system();break;
case 5: transfer_money(cs[i]);break;
case 6: deposit_money(cs[i]);break;
}
}while(true);
}
int bank::seek(string id)
{
for(int j=0;j<4;j++)
{
if(id==cs[j].get_id())
{
i=j;
break;
}
}
if(j>=4)
{
i=-1;
}
return i;
}
void bank::int_consumer(int i,string N,string I,string P,float M)
{
cs[i].int_consumer(N,I,P,M);
}
bool bank::check()
{
cout<<"请输入密码:"<<endl;
cin>>pass;
if(pass==cs[i].get_password())
return true;
else
return false;
}
void bank::show_me(consumer& cs)
{
cout<<"**********************"<<endl;
cout<<"*****当前账号信息*****"<<endl;
cout<<"**用户姓名:"<<cs.get_name()<<endl;
cout<<"**银行卡号:"<<cs.get_id()<<endl;
cout<<"**卡内余额:"<<cs.get_money()<<endl;
cout<<"**********************"<<endl;
}
void bank::change_password(consumer& cs)
{
cout<<"*******欢迎进入更改密码界面*******"<<endl;
string p1;
cout<<"请输入原密码: ";
cin>>p1;
string p2,p3;
if(p1==cs.get_password())
{
cout<<"请输入您的新密码: ";
cin>>p2;
cout<<"请再次输入您的新密码: ";
cin>>p3;
if(p2==p3)
{
cs.get_password()=p3;
cout<<"密码修改成功"<<endl;
}
else
cout<<"您两次输入的密码不一致,修改失败!"<<endl;
}
else
cout<<"您输入的密码错误!"<<endl;
}
void bank::withdraw_money(consumer& cs)
{
float a=0.0;
cout<<"请输入取款金额:"<<endl;
cin>>a;
while(a<=0)
{
cout<<"输入金额有误,请重新输入"<<endl;
cin>>a;
}
if(cs.get_money()-a<0)
{
cout<<"对不起你的余额不足"<<endl;
cout<<"你可以透支3元,若要透支请按1"<<endl;
int b;
cin>>b;
if(b==1)
{
if(cs.get_money()-a<-3)
{
cout<<"余额不足"<<endl;
}
else
{
cout<<"正在取款中......"<<" 您的透支金额为"<<a-cs.get_money()<<endl;
cs.set_money(cs.get_money()-a);
}
}
else
cout<<"交易取消"<<endl;
}
else
{
cout<<"正在取款中......"<<endl;
cs.set_money(cs.get_money()-a);
cout<<"交易成功!"<<endl;
}
}
void bank::deposit_money(consumer& cs)
{
float a=0.0;
cout<<"请输入存款金额:"<<endl;
cin>>a;
while(a<=0)
{
cout<<"输入金额有误,请重新输入"<<endl;
cin>>a;
}
cout<<"正在存钱......"<<endl;
cs.set_money(cs.get_money()+a);
cout<<"交易成功"<<endl;
}
int main()
{
bank b;
b.int_consumer(0,"王璐瑶","001","111111",1000);
b.int_consumer(1,"赵龙辉","002","222222",2000);
b.int_consumer(2,"刘晓伟","003","333333",3000);
b.int_consumer(3,"焦晓东","004","444444",4000);
b.welcome();
if(b.check())
{
cout<<"密码正确!"<<endl;
b.menu();
}
else
cout<<"密码错误!"<<endl;
return 0;
}
关于上次我写的那个ATM程序 ,程序没有什么错,但是有些麻烦,两个类中有好多成员函数重复,因此我把ATM重新写了一边。
标签:
原文地址:http://www.cnblogs.com/1521299249study/p/4434525.html