当前位置:文档之家› 银行账户管理程序

银行账户管理程序

#include<iostream>#include <string>#include<fstream>using namespace std;class consumer;class YH //银行类{public:void set_account();//银行开户功能void del_account(); //注销账户功能void transfer(int); //转账功能void enter_account(); //进入用户个人信息功能void addmoney(int,float);//存款功能void exitYH();// 退出系统void functionshow();void save();void load();// 功能界面protected:consumer *account[20];static int acnum;//账户数};class consumer:public YH//用户类,继承银行类的属性{public:friend class YH;consumer(int id,string Name,string PassWord,float m){ID=id;name=Name;money=m;passwd=PassWord;}consumer(){ID=0;name='0';money=0;passwd='0';}int get_id(){return ID;}void savemoney();// 取钱string get_passwd(){return passwd;}// 取得密码void display();void fetchmoney(); //取钱void change_passwd();void add_money(float);void dec_money(float);float get_money();//卡卡转帐private:int ID; //开户帐号string passwd; // 用户密码string name; // 用户姓名float money; //开户金额};void YH::save(){ofstream ofile("bankdat.dat",ios::out);ofstream outfile("bankdat.dat",ios::out);int n=0;outfile<<acnum<<" ";for(n=0;n<acnum;n++){outfile<<account[n]->ID<<" ";outfile<<account[n]->money<<" ";outfile<<account[n]->name<<" ";outfile<<account[n]->passwd<<" ";}outfile.close();}/*读入用户信息功能实现*/void YH::load(){ifstream infile("bankdat.dat",ios::in);if(!infile){cerr<<"读取错误,无资料中!"<<endl;return;}int n=0;int id,m;string nam,passw;infile>>acnum;for(n=0;n<acnum;n++){infile>>id;infile>>m;infile>>nam;infile>>passw;account[n]->passwd;consumer * acc = new consumer(id,nam,passw,m); account[n] = acc;}infile.close();cout<<"读取资料正常!"<<endl;}/*转账功能实现*/void YH::transfer(int x){int id;cout<<"请输入帐号:";cin>>id;int flag = 1;int i = 0;while((i<acnum)&&(flag)){if(id==account[i]->get_id()) flag = 0; else i++;}if(flag){cout<<"帐号不存在!"<<endl<<endl;return ;}float b;cout<<endl<<"请输入你要转帐的金额:";cin>>b;while(b<=0){cout<<"请输入正确的数字!"<<endl;cout<<"→";cin>>b;}if(account[x]->get_money()<b) cout<<"对不起,金额不够!!"<<endl;else {account[x]->dec_money(b);account[i]->add_money(b);}return;}/*账户金额计算*/void consumer::add_money(float x){money=x+money;}void consumer::dec_money(float x){money=money-x;}void YH::addmoney(int x,float y){account[x]->money=account[x]->money-y;}float consumer::get_money(){return money;}{YH yh;yh.functionshow();}/*主界面显示*/void YH::functionshow(){int n;do{system("cls"); load();cout<<endl<<"请输入相应的操作序号进行操作:"<<endl;cout<<"㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣"<<endl; cout<<"㊣㊣"<<endl; cout<<"㊣ 1. 开户㊣"<<endl; cout<<"㊣㊣"<<endl; cout<<"㊣ 2.账户登录㊣"<<endl; cout<<"㊣㊣"<<endl; cout<<"㊣ 3.账户注销㊣"<<endl; cout<<"㊣㊣"<<endl; cout<<"㊣ 4.退出系统㊣"<<endl; cout<<"㊣㊣"<<endl; cout<<"㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣㊣"<<endl; cout<<"→";cin>>n;while(n<1||n>4){cout<<"操作错误,请输入正确的操作序号!"<<endl;cout<<"→";cin>>n;}switch(n){case 1: set_account();//开户break;case 2:enter_account();//登录break;case 3: del_account();//注销break;case 4: exitYH();//退出break;}cin.get();}}void YH::enter_account(){int id;cout<<"请输入帐号:";cin>>id;int flag = 1;int i = 0; //__page_break__while((i<acnum)&&(flag)){if(id==account[i]->get_id()) flag = 0; else i++;}if(flag){cout<<"帐号不存在!"<<endl<<endl;return;}cout<<"请输入密码:";string passw;cin>>passw;if(passw!=account[i]->get_passwd()) return;account[i]->display();cin.get();cin.get();int n;do{system("cls");cout<<"请选择下列操作:"<<endl;cout<<"◎☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 1.查看账户信息◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 2.取款◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 3.存款◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 4.修改密码◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 5.转账◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎ 6.返回上一菜单◎"<<endl; cout<<"◎◎"<<endl; cout<<"◎☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆◎"<<endl; cout<<"→";cin>>n;{case 1: account[i]->display();break;case 2: account[i]->fetchmoney();save();break; case 3:account[i]->savemoney();save();break; case 4:account[i]->change_passwd();save();break; case 5:transfer(i);save();break;case 6:return;}cin.get();cin.get();}while(1);}void YH::set_account(){int id;string nam;string passw;float m;cout<<endl<<"请输入开户号:";cin>>id;cout<<endl<<"请输入开户人姓名:";cin>>nam;cout<<endl<<"请输入开户密码:";cin>>passw;cout<<endl<<"请输入存入金额:";cin>>m;while(m<=0){cout<<"请输入正确的数字!"<<endl;cin>>m;}consumer * acc = new consumer(id,nam,passw,m); account[acnum] = acc;cout<<"开户成功!!"<<endl<<endl;acnum++;save();cin.get();return;}void YH::del_account(){int id;cout<<endl<<"请输入你要注销的帐户号:";cin>>id;int flag = 1;int i = 0;while((i<acnum)&&(flag)){if(id == account[i]->get_id()){flag = 0;}else{i++;}}if(flag){cout<<"该帐号不存在,请重新输入!"<<endl; return;}for(int j=i;j<acnum;j++){account[j] = account[j+1];}account[acnum-1]=NULL;acnum--;cout<<"你的账号已注销!!"<<endl<<endl;save();cin.get();return;}void consumer::change_passwd(){string pwd,repwd;cout<<"请输入新密码:";cin>>pwd;cout<<"请再输入一次新密码:";cin>>repwd;if(pwd!=repwd)cout<<"你输入的两次密码不一样,按输入键返回上一层菜单!"<<endl; elsecout<<"密码修改成功,请牢记!"<<endl;cin.get();}void consumer::fetchmoney(){float m;char ch;do{cout<<endl<<"输入取款金额:"<<"¥>"<<endl ;cin>>m;while(m<=0){cout<<"请输入正确的数字!"<<endl;cout<<"→";cin>>m;}if(money<m){cout<<"对不起,你的余额不足!"<<endl;}else{money=money-m;cout<<endl<<"操作成功,请收好钱!"<<endl;}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 consumer::savemoney()//存钱函数功能实现{float c;char ch;do{cout<<endl<<"请输入要存入的金额:"<<"¥>"<<endl ; cin>>c;while(c<=0){cout<<"输入错误,请重新输入!"<<endl;cout<<"→";cin>>c;}money=money+c;cout<<"操作已成功!"<<endl;cout<<"是否要继续该项操作:(Y/N) "<<endl;cout<<"→";cin>>ch;while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y'){cout<<"→";cin>>ch;}}while(ch=='y'||ch=='Y');}int YH::acnum=0;void consumer::display()//用户信息界面{system("cls");cout<<"#####################################"<<endl; cout<<"#"<<endl;cout<<"# 用户姓名:"<<name<<endl;cout<<"#"<<endl;cout<<"# 帐号: "<<ID<<endl;cout<<"#"<<endl;cout<<"# 余额: "<<money<<endl;cout<<"#"<<endl;cout<<"# 按输入键回到上一菜单"<<endl;cout<<"#"<<endl;cout<<"#####################################"<<endl; cout<<"→";}void YH::exitYH()//退出系统{cout<<endl<<"感谢您对本银行的支持,欢迎下次光临!"<<endl; exit(0);}。

相关主题