具有功能
- 查询余额
- 取款操作
- 存款操作
- 修改密码
- 退出系统
银行类,用于管理用户
// Bank.h
class Bank{
private:
string password;
string card;
double money;
public:
static int times;
Bank();
Bank(string,double,string);
void set_password(string);// 创建密码函数
void set_card(string);// 创建账户函数
void set_money(int);// 初始化余额 函数
int check(User &);// 核对账户密码函数
void lock();// 锁卡函数
int get_money();// 获取余额函数
void in_money();// 存入函数
void out_money();// 取出函数
int change_password();// 更改密码函数
void exit_system();// 退出用户操作界面函数
};
int Bank::times=0;
用户类
//User.h
class User:public Bank{
public:
User(string card,double money,string password)
:Bank(card,money,password){}
User(){}
void show();// 用户功能界面
};
//功能函数
void show_lock();// 锁卡提示
void show_change_success();// 功更改密码提示
void show_reEnter();// 重新输入提示
void show_reOperate();// 重新操作提示
void show_change_error();// 密码错误提示
void show_amount_error();// 金额错误提示
void show_beyond_amount();// 超额提取提示
void show_noEnough_amount();// 余额不足提示
Bank::Bank(){
card='0';
money=0;
password='0';
}
Bank::Bank(string card,double money,string password){
this->card =card;
this->money=money;
this->password=password;
}
void Bank::set_card(string str) {
card=str;
}
void Bank::set_money(int mon){
money=mon;
}
void Bank::set_password(string str){
password=str;
}
void Bank::lock() {
if(times==3){
show_lock();
exit_system();
}
}
int Bank::get_money(){
return money;
}
void Bank::exit_system(){
exit(0);
}
int Bank::check(User &user) {
string a,b;
while(1){
cout<<"请输入您的卡号:";
cin>>a;
cout<<"请输入您的密码:";
cin>>b;
if(user.card!=a||user.password!=b){
times++;
if(times==3){
lock();
exit_system();
}
cout<<endl<<"------------------------------------------------ - --------------------------------"<<endl<<endl;
cout<<" 卡号或密码有误,请重新输入!"<<endl;
cout<<" 剩余输入次数:"<<3-times<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
else break;
}
return 1;
}
int Bank::change_password() {
int flag=1;
cout<<"温馨提示:密码格式为6位数字"<<endl;
string old_password;
string new_password;
string new2_password;
cout<<"请输入初始密码:";
cin>>old_password;
if(password==old_password){
cout<<"请输入新密码:";
cin>>new_password;
if(new_password==password){
cout<<"前后密码一致,请重新输入:";
cin>>new_password;
while(flag){
cout<<"请确认新密码:";
cin>>new2_password;
if(new_password==new2_password){
show_change_success();
password=new_password;
flag=0;
}
else{
show_reEnter();
}
}
}
else if(new_password.length()==6){
while(flag){
cout<<"请确认新密码:";
cin>>new2_password;
if(new_password==new2_password){
show_change_success();
password=new_password;
flag=0;
}
else{
show_reEnter();
}
}
}
else{
show_reOperate();
}
}
else{
show_change_error();
}
}
void Bank::in_money() {
double mon;
char ch;
do{
cout<<"请输入存款金额:";
cin>>mon;
if(mon<0)
cout<<"金额错误,请重新输入"<<endl;
else{
cout<<endl<<"正在存款中,请稍后......"<<endl;
money+=mon;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 交易成功! "<<endl;
cout<<" 当前余额:"<<money<<"元"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
cout<<"是否继续? (Y/N)"<<endl;
cin>>ch;
while(ch!='y'&&ch!='Y'&&ch!='N'&&ch!='n'){
cout<<"请按操作输入,请重新输入:"<<endl;
cin>>ch;
}
}while(ch=='y'||ch=='Y');
}
void Bank::out_money(){
cout<<"(温馨提示:每日单笔取款最大金额5000元)"<<endl;
double mon;
char ch;
do{
cout<<"请输入取款金额:";
cin>>mon;
if(mon<0){
show_amount_error();
}
else if(mon>5000){
show_beyond_amount();
}
else if((money-mon)<0){
show_noEnough_amount();
}
else{
cout<<endl<<"正在取款中,请稍后......"<<endl;
money-=mon;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 交易成功! "<<endl;
cout<<" 当前余额:"<<money<<"元"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
cout<<endl<<"是否继续? (Y/N)"<<endl;
cin>>ch;
while(ch!='y'&&ch!='Y'&&ch!='N'&&ch!='n'){
cout<<"无效操作,请重新输入:"<<endl;
cin>>ch;
}
}while(ch=='y'||ch=='Y');
}
//User.cpp
void User::show() {
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 1.查询余额 "<<endl;
cout<<" 2.取款 "<<endl;
cout<<" 3.存款 "<<endl;
cout<<" 4.修改密码 "<<endl;
cout<<" 5.退出 "<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<"请输入操作:";
}
void show_menu(int i,User user[]){ // 开始、操作选项以及结束界面
int operation;
if(i>2){
cout<<endl<<"------------------------------"<<endl<<endl;
cout<<" 无此编号!"<<endl;
cout<<endl<<"------------------------------"<<endl<<endl;
cout<<"请重新输入编号:";
cin>>i;
}
if(user[i].check(user[i]));
do{
user[i].show();
cin>>operation;
switch(operation){
case 1:
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 您当前余额为:"<<user[i].get_money()<<"元"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
break;
case 2:user[i].out_money();
break;
case 3:user[i].in_money();
break;
case 4:user[i].change_password();
break;
case 5:
cout<<"--------------------------------请收好您的银行卡----------------------------------"<<endl;
break;
default:
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 无效操作,请重新输入! "<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
}while(operation!=5);
}
功能函数的实现
void show_lock(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 累计错误三次,该卡已冻结!"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_change_success(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 密码修改成功! "<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_reEnter(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 前后密码不一致,请重新输入:"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_reOperate(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 新密码格式错误,请重新操作!";
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_change_error(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 密码错误,修改失败!"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_amount_error(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 输入金额错误,请重新输入!"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_beyond_amount(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 超出单笔最大取款金额"<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}
void show_noEnough_amount(){
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
cout<<" 对不起,余额不足 "<<endl;
cout<<endl<<"---------------------------------------------------------------------------------"<<endl<<endl;
}