介绍:
银行储蓄系统包括:支持多名储户开户、存款、取款、转帐、改密、挂失、解挂、销户等功能。
登录
输入业务员用户名和密码。3次输入不正确,强制退出程序。
基本要求
可以只设一名业务员,用全局变量表示其用户名和密码,写在程序中。
系统菜单
当业务员信息验证后,进入到系统主控菜单,由业务员为储户办理各种业务,直到按0后退出。
基本要求
每次启动系统,都是从0用户开始办理业务
开户
账号由系统自动分配
确认密码和密码不同时要拒绝。
开户后,账号状态为0-正常
销户
输入账号,确认后,将余额全部取完,并将状态state置为2-销户。
办理销户的账号,其状态必须为“0-正常“。
存款
输入账号、金额,记录存款后的余额。
取款
输入账号、金额,记录取款后的余额。
取款额不能超过余额。
查询
输入账号,显示账户信息。
转账
输入转出账号、金额、转入账户,记录转账后的余额。
账户必须存在,扣除转出账户的金额,要与转入账户增加的金额相同。
挂失
输入账号,将状态改变为 1-挂失
解挂
输入账号,将状态为 1-挂失 的账户状态改为 0-正常
改密
用新密码替代旧密码。
新密码必须与确认密码相同。
源代码:
下面是源代码:
main.cpp
/***********************************************************
* 版权所有 (C)2015,侯鑫行
*
* 文件名称:main.cpp
* 文件标识:无
* 内容摘要:该代码为main函数
* 其它说明:无
* 当前版本: V1.0
* 作 者:侯鑫行
* 完成日期: 20150717
*
* 修改记录1:
* 修改日期: 20150717
* 版本号: V1.0
* 修改人: houxinhang
* 修改内容:创建
**********************************************************/
#include <iostream>
#include"bank.h"
#include<cstdlib>
using namespace std;
int main()
{
cout<<"+---------------------------------+"<<endl;
cout<<"+ 欢迎光临 H X H 银行 +"<<endl;
cout<<"+---------------------------------+"<<endl;
Bank a;
while(pass())
{
a.work();
char o;
cout<<"是否退出?(y/n)"<<endl;
cin>>o;
if(o=='y')
{
a.baocun();
exit(1);
}
}
return 0;
}
bank.h
/***********************************************************
* 版权所有 (C)2015,侯鑫行
*
* 文件名称:bank.h
* 文件标识:无
* 内容摘要:该代码为user和银行柜台(bank)函数的声明
* 其它说明:无
* 当前版本: V1.0
* 作 者:侯鑫行
* 完成日期: 20150717
*
* 修改记录1:
* 修改日期: 20150717
* 版本号: V1.0
* 修改人: houxinhang
* 修改内容:创建
**********************************************************/
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <cstring>
using namespace std;
const int upNum=2000;
class User
{
public:
User() {}
User(int n,string nam,char pass[20],int ba,int st,string c,string add);
void set_user(int n,string nam,char pass[20],int ba,int st,string c,string add);
bool jianyanmima();//检验密码
friend class Bank;
private:
int number;//账号
string name;//姓名
char password[20];//密码
int balance;//金额
int status;//状态
string card;//身份证号
string address;
};
class Bank:public User
{
public:
Bank(); //开始前从文件中读数据,存在数组中
~Bank(); //程序结束前,将数组中的数据写入到文件中
void work(); //业务驱动
void openAccount(); //开户
void cancelAccount(); //注销账户
void save(); //存款
void withdraw(); //取款
void showAccount(); //查询余额
void transferAccounts(); //转账
void reportLoss(); //挂失
void cancelLoss(); //解除挂失
void updatePassword(); //更改密码
int getUser(); //输入账号查询用户,返回用户在对象数组中的下标
int baocun();//保存信息
private:
int N;
User users[upNum];
};
int pass();//判断账户
int show_window();//操作窗口
int record(char a[20]);
void lixi();//利息
#endif // BANK_H_INCLUDED
bank.cpp
/***********************************************************
* 版权所有 (C)2015,侯鑫行
*
* 文件名称:bank.cpp
* 文件标识:无
* 内容摘要:该代码为user和银行柜台(bank)函数的定义
* 其它说明:无
* 当前版本: V1.0
* 作 者:侯鑫行
* 完成日期: 20150717
*
* 修改记录1:
* 修改日期: 20150717
* 版本号: V1.0
* 修改人: houxinhang
* 修改内容:创建
**********************************************************/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "bank.h"
#include<time.h>
#include<conio.h>
#include <ctype.h>
using namespace std;
/*********************************************************
* 功能描述:显示银行柜台的操作
* 输入参数:无
* 输出参数:无
* 返回值:柜台操作(0-9)
* 其它说明:
************************************************************/
int show_window()
{
int choose;
while(1)
{
cout<<"+----------------------------------+"<<endl;
cout<<"+ 1开户 2销户 3存款 +"<<endl;
cout<<"+ 4取款 5查询 6转账 +"<<endl;
cout<<"+ 7挂失 8解挂 9改密 +"<<endl;
cout<<"+ 0退出 +"<<endl;
cout<<"请输入指令: ";
cin>>choose;
if(choose>=0&&choose<=9)
break;
else
cout<<"输入有误,请重新输入: ";
}
return choose;
}
/*********************************************************
* 功能描述:user(成员)的构造函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
User::User(int n,string nam,char pass[20],int ba,int st,string c,string add)
{
number=n;
name=nam;
strcpy(password,pass);
balance=ba;
status=st;
card=c;
address=add;
}
/*********************************************************
* 功能描述:user(成员)读入信息
* 输入参数:无
* 输出参数:无
* 返回值:
* 其它说明:
************************************************************/
void User::set_user(int n,string nam,char pass[20],int ba,int st,string c,string add)
{
number=n;
name=nam;
strcpy(password,pass);
balance=ba;
status=st;
card=c;
address=add;
}
/*********************************************************
* 功能描述:记录用户的操作
* 输入参数:无
* 输出参数:无
* 返回值:0
* 其它说明:
************************************************************/
int event(string a,int b,int c)
{
time_t curtime=time(0);//调用时间函数
tm tim =*localtime(&curtime);
int day,mon,year,hour,minute,second;
day=tim.tm_mday;//提取当前的日期
mon=tim.tm_mon;
year=tim.tm_year;
hour=tim.tm_hour;
minute=tim.tm_min;
second=tim.tm_sec;
fstream outfile2;
outfile2.open("event.txt",ios::out|ios::in);
if(!outfile2)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile2.seekp(0,ios::end);
outfile2<<" "<<year+1900<<" 年"<<mon+1<<"月"<<day<<"日 "<<hour<<":"<<minute<<":"<<second<<" "<<a<<b<<"使用账号("<<c<<")"<<endl;
outfile2.close();
return 0;
}
/*********************************************************
* 功能描述:验证成员账号密码
* 输入参数:无
* 输出参数:无
* 返回值:true or false
* 其它说明:
************************************************************/
int pass()
{
int trys=3;
int trues=0;
int right=1;
char name[20];
char password[20];
char c;
fstream infile;
infile.open("user.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(trys>0)
{
int n=0;
cout<<"请输入业务员用户名:";
cin>>name;
cout<<"请输入密码: ";
while((c=getch())!='\r')
{
password[n]=c;
putchar('*');
n++;
}
password[n]='\0';
infile.seekg(0,ios::beg);
while(!infile.eof())
{
int num;
char nam[20];
char pass[20];
int money;
int status;
string card;
string add;
infile>>num>>nam>>pass>>money>>status>>card>>add;
if(strcmp(nam,name)==0&&strcmp(pass,password)==0)
trues=1;
}
if(trues==1)
{
right=1;
string a;
a="用户登录";
event(a,0,0);
break;
}
else
{
trys--;
cout<<"用户名或密码输入错误。"<<endl;
cout<<"你还有"<<trys<<"次机会"<<endl;
if(trys==0)exit(1);
}
}
infile.close();
return right;
}
/*********************************************************
* 功能描述:检验密码是否正确
* 输入参数:无
* 输出参数:无
* 返回值:ture 或者 false
* 其它说明:
************************************************************/
bool User::jianyanmima()
{
char pass[20];
cout<<"输入密码··";
cin>>pass;
if(strcmp(password,pass)==0)
return true;
else
{
cout<<"密码有误!"<<endl;
return false;
}
}
/*********************************************************
* 功能描述:银行柜台的构造函数,实现文件的读入
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明: 使用文件输入流
************************************************************/
Bank::Bank()
{
fstream infile2,infile3;
infile2.open("user.txt",ios::in);
if(!infile2)
{
cerr<<"open error!"<<endl;
exit(1);
}
infile3.open("user.txt",ios::in);
if(!infile3)
{
cerr<<"open error!"<<endl;
exit(1);
}
int number;
int i=0;
string name;
char password[20];
int balance;
int status;
string c;
string add;
while(infile2>>number>>name>>password>>balance>>status>>c>>add)
{
users[i].set_user(number,name,password,balance,status,c,add);
i++;
}
N=i;
infile2.close();
infile3.close();
}
/*********************************************************
* 功能描述:把用户的结果输出到文件
* 输入参数:无
* 输出参数:无
* 返回值:0
* 其它说明:
************************************************************/
int Bank::baocun()
{
fstream outfile;
outfile.open("2.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0; i<N; i++)
{
outfile<<users[i].number<<" ";
outfile<<users[i].name<<" ";
outfile<<users[i].password<<" ";
outfile<<users[i].balance<<" ";
outfile<<users[i].status<<" ";
outfile<<users[i].card<<" ";
outfile<<users[i].address<<endl;
}
outfile.close();
}
/*********************************************************
* 功能描述:银行柜台的析构函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
Bank::~Bank()
{}
/*********************************************************
* 功能描述:银行柜台工作,进行柜台操作
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::work()
{
int choose=100;
do
{
choose=show_window();
switch(choose)
{
case 1:
openAccount();
break; //开户
case 2:
cancelAccount();
break; //注销账户
case 3:
save();
break; //存款
case 4:
withdraw();
break; //取款
case 5:
showAccount();
break; //查询余额
case 6:
transferAccounts();
break; //转账
case 7:
reportLoss();
break; //挂失
case 8:
cancelLoss();
break; //解除挂失
case 9:
updatePassword();
break; //更改密码
case 0:
cout<<"+----------------------------------+"<<endl;
cout<<"+--------欢迎下次光临!------------+"<<endl;
break;
}
}
while(choose);
}
/*********************************************************
* 功能描述:根据二分法得到账号的下标
* 输入参数:无
* 输出参数:无
* 返回值:账号的下标
* 其它说明:
************************************************************/
int Bank::getUser()
{
int zh;
cout<<"账号:";
cin>>zh;
int index=-1;
int low=0, high=N-1, mid;
while(low<=high)
{
mid = (low+high)/2;
if(users[mid].number==zh)
{
index=mid;
break;
}
else if (users[mid].number>zh)
high=mid-1;
else
low=mid+1;
}
if (index<0)
cout<<"该用户不存在,本次操作失败!"<<endl;
return index;
}
/*********************************************************
* 功能描述:开户操作
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::openAccount()
{
int num;
string nam;
char pass[20];
char pas[20];
int bal;
int sta;
string cad;
string add;
cout<<"正在开户······"<<endl;
cout<<"请输入姓名:";
cin>>nam;
cout<<"输入密码:";
cin>>pass;
cout<<"确认密码:";
cin>>pas;
if(strcmp(pass,pas)!=0)
cout<<"密码不一致,开户失败!"<<endl;
else
{
cout<<"确认密码正确,请牢记您的密码!"<<endl;
cout<<"请输入你的身份证号:";
cin>>cad;
cout<<"家庭地址:";
cin>>add;
cout<<"输入存入金额:";
cin>>bal;
cout<<"您的账号为:"<<10001+N<<endl;
sta=1;
users[N].set_user(10001+N,nam,pass,bal,sta,cad,add);
N++;
cout<<"恭喜,开户成功!"<<endl;
cout<<"如果您想使用刚开的用户,请退出从新使用业务"<<endl;
string a;
a="开户成功";
int b,c=0;
b=10000+N;
event(a,c,b);
a="存入金额 ";
c=bal;
a=a;
event(a,c,b);
}
}
/*********************************************************
* 功能描述:注销账户
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::cancelAccount()
{
int q=0;
q=getUser();
if(q>=0)
{
if(users[q].jianyanmima()>0)
{
cout<<"确定要注销?(y/n)"<<endl;
char a;
cin>>a;
if(a=='y')
{
users[q].status=0;
cout<<"注销成功!"<<endl;
string a;
int b;
a="用户注销";
b=users[q].number;
event(a,0,b);
}
}
}
}
/*********************************************************
* 功能描述:存款
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::save()
{
int q=0;
int money;
q=getUser();
if(q>=0)
{
if(users[q].status==1)
{
cout<<"输入要存入的金额··";
cin>>money;
users[q].balance+=money;
cout<<"存款成功,现余金额"<<users[q].balance<<"元."<<endl;
string a;
int b,c;
a="存款";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
}
else if(users[q].status==0)
cout<<"该用户已注销,无法存钱"<<endl;
else
cout<<"该用户已挂失,无法存钱"<<endl;
}
}
/*********************************************************
* 功能描述:取款
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::withdraw()
{
int q=0;
int money;
q=getUser();
if(q>=0)
{
if(users[q].jianyanmima()>0)
{
if(users[q].status==1)
{
cout<<"输入要取出的金额··";
cin>>money;
users[q].balance-=money;
cout<<"取款成功,现余金额"<<users[q].balance<<"元."<<endl;
string a;
int b,c;
a="取款";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
}
else if(users[q].status==0)
cout<<"该用户已注销,无法存钱"<<endl;
else
cout<<"该用户已挂失,无法存钱"<<endl;
}
}
}
/*********************************************************
* 功能描述:查询
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::showAccount()
{
int q=0;
q=getUser();
if(q>=0)
if(users[q].jianyanmima()>0)
{
cout<<"当前金额为:"<<users[q].balance<<endl;
string a;
int b,c;
a="查询金额";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
}
}
/*********************************************************
* 功能描述:转账
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::transferAccounts()
{
int q=0,p=0;
int money;
q=getUser();
if(q>=0)
{
if(users[q].jianyanmima()>0)
{
if(users[q].status==1)
{
p=getUser();
if(users[p].status==1)
{
string a;
int b,c;
a="转账(前)";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
a="转账对象";
b=users[p].balance;
c=users[p].number;
event(a,b,c);
cout<<"转账金额:";
cin>>money;
users[q].balance-=money;
users[p].balance+=money;
cout<<"转账成功,剩余"<<users[q].balance<<"元."<<endl;
a="转账金额";
b=money;
c=users[q].number;
event(a,b,c);
a="转账(后)";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
}
else if(users[p].status==0)
cout<<"转账账户已注销,转账失败"<<endl;
else
cout<<"转账账号已挂失,转账失败"<<endl;
}
else if(users[p].status==0)
cout<<"该账户已注销,转账失败"<<endl;
else
cout<<"该账号已挂失,转账失败"<<endl;
}
}
}
/*********************************************************
* 功能描述:挂失
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::reportLoss()
{
int q=0;
char c;
q=getUser();
if(q>=0)
if(users[q].jianyanmima()>0)
{
cout<<"确定要挂失?(y/n)"<<endl;
cin>>c;
if(c=='y')
{
users[q].status=2;
cout<<"已挂失"<<endl;
//
string a;
int b,c;
a="挂失";
b=users[q].balance;
c=users[q].number;
event(a,b,c);
}
else
cout<<"操作结束"<<endl;
}
}
/*********************************************************
* 功能描述:解挂
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::cancelLoss()
{
int q=0;
q=getUser();
if(q>=0)
if(users[q].jianyanmima()>0)
{
if(users[q].status==2)
{
users[q].status==1;
cout<<"解挂成功"<<endl;
event("解挂",users[q].balance,users[q].number);
}
else if(users[q].status==0)
{
cout<<"该账户已注销"<<endl;
}
else
cout<<"该账户未挂失"<<endl;
}
}
/*********************************************************
* 功能描述:改密
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:
************************************************************/
void Bank::updatePassword()
{
int q=0;
char c[20];
char d[20];
q=getUser();
if(q>=0)
if(users[q].jianyanmima()>0)
{
if(users[q].status==1)
{
cout<<"输入新密码:";
cin>>c;
cout<<"验证新密码:";
cin>>d;
if(strcmp(c,d)==0)
{
strcpy(users[q].password,c);
cout<<"密码设置成功"<<endl;
event("修改密码为",users[q].balance,users[q].number);
event(c,users[q].balance,users[q].number);
}
else
cout<<"密码错误!"<<endl;
}
}
}