职员账户100~104,密码123456
客户账户10000~10100,密码123456
文件链接:http://pan.baidu.com/s/1FIO02
main.cpp
#include<iostream>
#include<fstream> //从文件中读数据包含的头文件
#include <windows.h> //system,Sleep,exit函数的调用需要的头文件
#include<conio.h> //调用getch需要用到的头文件
#include"bank.h"
void back_menu()
{
int iBack;
cout<<"\n0.返回工作菜单\n";
while((iBack=getch())!='0')
{
;
}
}
int main()
{
Bank a; //定义银行类
Officer * b=NULL; //定义一个职员对象指针,并初始化为NULL
b=a.find_officer(); //将find_officer()返回的指针赋值给c
if(b!=NULL) //首先判断b是否为空指针,如果为空直接退出
{
bool q=false;
q=(*b).login(); //职员登陆
if(q) //如果登陆,进入工作,否者直接退出系统
{
a.load_customer_search(); //加载customer的索引
Sleep(1000);
system("cls");
while(1)
{
int g=1;
cout<<"1.工作\t"<<"2.退出"<<endl;
cin>>g;
switch(g)
{
case 1:
{
Customer *c=NULL; //定义客户为的指针对象,并初始化为空
int i=1;
while(i!=10)
{
system("cls");
cout<<"*********程序员银行*********"<<endl;
cout<<" 当前操作员:"<<(*b).get_name()<<endl;
cout<<"1.开户\t"<<"2.存钱"<<endl;
cout<<"3.取钱\t"<<"4.信息查询"<<endl;
cout<<"5.转账\t"<<"6.改密"<<endl;
cout<<"7.挂失\t"<<"8.解挂"<<endl;
cout<<"9.销户\t"<<"10.退出"<<endl;
cin>>i;
switch(i)
{
case 1:
{
system("cls");
a.open_account();
back_menu();
break;
}
case 2:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).saveMoney();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 3:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).drawMoney();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 4:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).showCustomer();
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 5:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).transferAccount();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 6:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).changePassword();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 7:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).turnLost();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 8:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
(*c).turnUnlost();
a.save_customer(*c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 9:
{
system("cls");
c=new Customer;
c=a.find_customer();
if(c!=NULL)
{
a.closeAccount(c);
}
else
{
cout<<"该账户不存在"<<endl;
}
delete c;
back_menu();
break;
}
case 10:
{
system("cls");
cout<<"退出中";
Sleep(2000);
system("cls");
exit(1);
}
}
}
}
case 2:
exit(1);
}
}
}
}
else
{
cout<<"the account don't exist";
Sleep(2000);
exit(1);
}
}
bank.cpp
#include<iostream>
#include<fstream>
#include <windows.h> //清屏函数的调用需要的头文件
#include<conio.h> //调用getch需要用到的头文件
#include<stdlib.h>
#include"bank.h"
using namespace std;
//**************全局函数************
int get_password()
{
int password=0;
int m;
for(int i=0; i<6;)
{
m=getch()-48;
if(m>=0&&m<=9)
{
cout<<'*';
(password*=10)+=m;
i++;
}
}
return password;
}
//***************Bank类的成员函数的定义*********************
Bank::Bank()
{
ifstream infile("officer.txt",ios::ate|ios::binary);
if(!infile)
{
cerr<<"officer information load fail";
exit(1);
}
else
{
infile.seekg(ios::beg);
infile.read((char*)&off_NO,sizeof(off_NO));
off=new Officer[off_NO];
int i;
for(i=0; i<off_NO; i++)
{
infile.read((char*)&off[i],sizeof(off[i]));
}
infile.close();
}
cus=new Customer;
}
Bank::Bank(int a)
{
off=NULL;
Bank::load_customer_search();
}
void Bank::open_account() //银行的开户函数
{
char name[20]; //输入开户用户的信息
char idcard[18];
int password;
double money=0;
cout<<"******开户******";
cout<<"输入姓名:";
cin>>name;
cout<<"输入身份证号:";
int i=0;
char m;
while((m=getch())!=13&&i<18)
{
cout<<m;
idcard[i]=m;
i++;
}
cout<<endl<<"输入密码:";
password=get_password();
customer_search sear; //为开户用户建立索引
sear.num=search_NO;
sear.account=search_NO+10000;
sear.state=true;
ofstream outfile1("customersearch.txt",ios::ate|ios::binary|ios::in|ios::out);
if(!outfile1)
{
cerr<<"文件加载失败"<<endl;
exit(1);
}
int NO=search_NO;
search_NO++; //索引的数量增加1
outfile1.seekp(ios::beg); //存入索引
outfile1.write((char*)&search_NO,sizeof(search_NO));
outfile1.seekp(NO*sizeof(sear),ios::cur);
outfile1.write((char*)&sear,sizeof(sear));
outfile1.close();
delete cus_search;
load_customer_search(); //将修改后的索引读入
Customer p(sear.account,name,idcard,password,money,false); //建立一个customer类,
fstream outfile2("customer.txt",ios::ate|ios::binary|ios::in|ios::out);
if(!outfile2)
{
cerr<<"文件加载失败"<<endl;
exit(1);
}
outfile2.seekg(ios::beg);
outfile2.read((char*)&cus_NO,sizeof(cus_NO));
cus_NO++; //客户的数量增加1
outfile2.seekp(ios::beg);
outfile2.write((char*)&cus_NO,sizeof(cus_NO));
outfile2.seekp(NO*sizeof(p),ios::cur);
outfile2.write((char*)&p,sizeof(p));
outfile2.close();
system("cls");
cout<<"\n开户成功"<<endl;
cout<<"账户号码为:"<<sear.account<<endl;
}
void Bank::closeAccount(Customer*a) //银行类的销户函数
{
system("cls");
bool k;
k=(*a).login();
if(k)
{
if(!(*a).lost)
{
cus_search[cus_cur].num=cus_cur;
cus_search[cus_cur].account=(*a).account;
cus_search[cus_cur].state=false;
if((*a).money!=0)
{
cout<<"取出余额:"<<(*a).money<<endl;
}
ofstream outfile1("customersearch.txt",ios::binary|ios::ate|ios::in|ios::out);
if(!outfile1)
{
cerr<<"文件加载失败"<<endl;
exit(1);
}
outfile1.seekp(ios::beg);
outfile1.seekp(sizeof(int),ios::beg);
outfile1.seekp(cus_cur*sizeof(cus_search[cus_cur]),ios::cur);
outfile1.write((char*)&cus_search[cus_cur],sizeof(cus_search[cus_cur]));
outfile1.close();
Customer p((*a).account);
ofstream outfile2("customer.txt",ios::binary|ios::ate|ios::out|ios::in);
if(!outfile2)
{
cerr<<"文件加载失败"<<endl;
exit(1);
}
cus_NO--;
outfile2.seekp(ios::beg);
outfile2.write((char*)&cus_NO,sizeof(cus_NO));
outfile2.seekp(cus_cur*sizeof(p),ios::cur);
outfile2.write((char*)&p,sizeof(p));
outfile2.close();
cout<<"销户成功"<<endl;
}
else
cout<<"当前账号处于挂失状态,不能销户"<<endl;
}
else
cout<<"密码错误"<<endl;
delete cus;
}
void Bank::load_customer_search() //加载customer的索引
{
ifstream infile("customersearch.txt",ios::ate|ios::binary);
if(!infile)
{
cerr<<"customersearch information load fail";
exit(1);
}
else
{
int i;
infile.seekg(ios::beg);
infile.read((char*)&search_NO,sizeof(search_NO));
cus_search=new customer_search[search_NO];
for(i=0; i<search_NO; i++)
{
infile.read((char*)&cus_search[i],sizeof(cus_search[i]));
}
infile.close();
}
}
void Bank::save_customer(Customer&a) //存储用户信息
{
ofstream outfile("customer.txt",ios::ate|ios::binary|ios::in|ios::out);
if(!outfile)
{
cerr<<"save file load fail";
exit(1);
}
outfile.seekp(sizeof(int),ios::beg);
outfile.seekp(cus_cur*sizeof(a),ios::cur);
outfile.write((char*)&a,sizeof(a));
outfile.close();
delete cus;
}
Customer* Bank::find_customer() //查找客户的函数
{
cus=new Customer;
int a;
cout<<"输入账号:";
cin>>a;
int i=0;
for(i=0; i<search_NO; i++)
{
if(cus_search[i].account==a&&cus_search[i].state==1)
{
cus_cur=i;
ifstream infile("customer.txt",ios::binary);
infile.read((char*)&cus_NO,sizeof(cus_NO));
infile.seekg(i*sizeof(cus[0]),ios::cur);
infile.read((char*)&cus[0],sizeof(cus[0]));
infile.close();
return cus;
}
}
return NULL;
}
Officer* Bank::find_officer() //查找职员的函数
{
int a;
cout<<"输入账号:";
cin>>a;
int i=0;
for(i=0; i<off_NO; i++)
{
if(off[i].account==a)
return &off[i];
}
return NULL;
}
//*********officer类的成员函数**************
Officer::Officer() //对officer的初始化
{
account=-1;
*name='\0';
*idcard='\0';
password=-1;
}
Officer::Officer(int a,char *b,char *c,int d)
{
account=a;
for(int i=0; i<20; i++)
{
name[i]=b[i];
}
for(int j=0; j<18; j++)
{
idcard[j]=c[j];
}
password=d;
}
bool Officer::login() //登陆函数
{
int in_password=0,n=0;
bool b=false;
while(n<3&&!b)
{
cout<<"输入密码:";
in_password=get_password();
if(password==in_password)
{
b=true;
cout<<'\n'<<name<<endl;
cout<<"登陆成功"<<endl;
}
else
{
cout<<"\n密码错误"<<endl;
++n;
Sleep(500);
system("cls");
cout<<"今日还有"<<3-n<<"次输入机会"<<endl;
}
}
system("cls");
return b;
}
//***********customer类的成员函数的定义***********
Customer::Customer()
{
money=0;
lost=false;
}
Customer::Customer(int a)
{
account=a;
*name='\0';
*idcard='\0';
password=-1;
money=0;
lost=false;
}
void Customer::saveMoney()
{
double p_money;
cout<<"输入存款金额:";
cin>>p_money;
system("cls");
cout<<"姓名:"<<name<<endl;
int i;
cout<<"存款金额:"<<p_money<<endl;
cout<< "1.确认 2.取消"<<endl;
cin>>i;
system("cls");
switch(i)
{
case 1:
money+=p_money;
cout<<"存款成功"<<endl;
break;
case 2:
cout<<"取消成功"<<endl;
break;
}
}
void Customer::drawMoney() //客户类的取钱函数
{
bool k;
k=Customer::login();
if(k)
{
if(!lost)
{
double p_money;
cout<<"输入取款金额:";
cin>>p_money;
cout<<endl;
if(p_money<=money)
{
int i;
system("cls");
cout<<"取款金额:"<<p_money<<endl;
cout<< "1.确认 2.取消"<<endl;
cin>>i;
system("cls");
switch(i)
{
case 1:
money-=p_money;
cout<<"取款成功"<<endl;
break;
case 2:
cout<<"取消成功"<<endl;
break;
}
}
else
{
cout<<"余额不足"<<endl;
}
}
else
cout<<"当前账号处于挂失状态,不能取款"<<endl;
}
}
void Customer::showCustomer() //客户类的信息查询函数
{
bool k;
k=Customer::login();
system("cls");
if(k)
{
cout<<"账号:"<<account<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"身份证号码:"<<idcard<<endl;
cout<<"余额:"<<money<<endl;
cout<<"状态:";
if(lost)
cout<<"挂失"<<endl;
else
cout<<"正常"<<endl;
}
}
void Customer::transferAccount() //客户类的转账函数
{
if(Customer::login())
{
if(!lost)
{
Bank t(5);
Customer*k;
k=new Customer;
cout<<"转入:\n";
k=t.find_customer();
if(k!=NULL)
{
double p_money;
cout<<"输入转账金额:";
cin>>p_money;
cout<<endl;
if(p_money>0)
{
system("cls");
if(p_money<=money)
{
cout<<"转出金额:"<<p_money<<endl;
cout<<"转入账号:"<<(*k).get_account()<<endl;
int i;
cout<<"1.确认 2.取消"<<endl;
cin>>i;
system("cls");
switch(i)
{
case 1:
money-=p_money;
(*k).money+=p_money;
cout<<"转账成功"<<endl;
break;
case 2:
break;
}
}
else
cout<<"余额不足"<<endl;
}
else
cout<<"转账金额需大于0元"<<endl;
}
else
{
cout<<"你输入的账户不存在"<<endl;
}
t.save_customer(*k);
delete k;
}
else
{
cout<<"当前账号处于挂失状态,不能转账"<<endl;
}
}
else
cout<<"密码错误"<<endl;
}
void Customer::changePassword() //修改密码的函数
{
if(!lost)
{
int p,j,k;
cout<<"输入原密码:";
p=get_password();
system("cls");
if(password==p)
{
cout<<"\n输入新密码:";
j=get_password();
cout<<"\n再次输入密码:";
k=get_password();
system("cls");
if(j==k)
{
password=j;
cout<<"密码修改成功/n";
}
else
cout<<"\n两次输入的密码不相同修改失败";
}
else
cout<<"\n密码错误";
}
else
cout<<"\n当前用户处于挂失状态无法修改密码";
}
void Customer::turnLost() //客户类的挂失函数
{
bool k;
k=Customer::login();
if(k)
{
if(!lost)
{
lost=true;
cout<<"挂失成功"<<endl;
}
else
cout<<"你账号已挂失"<<endl;
}
else
cout<<"密码错误"<<endl;
}
void Customer::turnUnlost() //客户类的解挂函数
{
bool k;
k=Customer::login();
if(k)
{
if(lost)
{
lost=false;
cout<<"解挂成功"<<endl;
}
else
cout<<"当前账户无需解挂"<<endl;
}
else
cout<<"密码错误";
}
bank.h
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include<cstring>
using namespace std;
class Bank;
class Officer;
class Customer;
//定义的customer索引
struct customer_search
{
int num;
int account;
bool state;
};
//***********定义的银行类***********
class Bank
{
private:
int off_NO; //已存入的officer数量
int search_NO; //已存入的索引数量
int cus_NO; //已存在的customner账户数
int cus_cur; //当前操作的用户的索引序列
Customer *cus; //定义的Customer指针
Officer *off; //定义的Officer指针
customer_search *cus_search; //定义的customer_serach的指针
public:
Bank(); //默认构造函数
Bank(int a); //重载的构造函数
void open_account(); // 开户函数
void closeAccount(Customer*a); //销户函数
void save_customer(Customer&a); //存储用户信息
void load_customer_search(); //加载customer的索引
Officer* find_officer(); //查找职员的函数,
Customer* find_customer(); //查找用户的函数,并返回该用户的指针
~Bank() {};
};
//***************定义的职员类*****************
class Officer
{
protected:
int account; //职员账号
char name[20]; //姓名
char idcard[18]; //身份证
int password; //密码
public:
Officer(); //无参构造函数
Officer(int a,char* b,char* c,int d); //有参构造函数
bool login(); //用户登录函数
char* get_name()
{
return name;
}
~Officer() {};
//析构函数用于将保存用户的交易记录,但可行性有待考虑
friend Bank; //声明为bank类的友元类
};
//**************定义的客户类********
class Customer:public Officer
{
private:
double money; //客户的账户余额
bool lost; //挂失为true,没有挂失为false
public:
Customer(); //无参构造函数
Customer(int a);
Customer(int a,char* b,char* c,int d,double e,bool f):Officer(a,b,c,d) //有参构造函数
{
money=e,lost=f;
}
int get_account()
{
return account;
}
void saveMoney(); //存钱函数
void drawMoney(); //取钱函数
void showCustomer(); //信息查询函数
void transferAccount(); //转账函数
void changePassword(); //修改密码函数
void turnLost(); //挂失函数
void turnUnlost(); //解挂函数
void closeAccount(); //销户函数
~Customer() {}; //析构函数
friend Bank ; //声明为bank的友元类
};
#endif // BANK_H_INCLUDED