main.cpp
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include "staff.h"
#include "bank.h"
#include "user.h"
#include <ctime>
using namespace std;
void ctime();
int main()
{
cout<<"+-----------------------------------+"<<endl;
cout<<"+ ※※※ +"<<endl;
cout<<"+ 歡迎來到暮光銀行 +"<<endl;
cout<<"+ ※※※ +"<<endl;
cout<<"+-----------------------------------+"<<endl;
//ctime();
Bank b;
if(!b.passtaff()) //業務員登錄
exit(1); //退出系統
cout<<endl;
b.showMenu(); //顯示菜單
b.manage(); //開始工作
return 0;
}
bank.cpp
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <stdio.h>
#include <fstream>
#include "staff.h"
#include "bank.h"
using namespace std;
/*功能描述:驗證密碼
*輸入參數:只能爲數字的密碼
*輸出參數:形式如******的密碼錶現形式
*/
int inputPassword()
{
char ch; //接收字符形式密碼
int iPass=0; //要轉換爲數字
int i;
while(1)
{
for(i=0; i<6; i++)
{
ch=getch(); //輸入但不顯示
putchar('*'); //輸出*
if(isdigit(ch))
iPass=iPass*10+(ch-'0');
else
{
iPass=0;
break; //退出for循環後,再次接受
}
}
fflush(stdin); //清除鍵盤緩存區中已經有的輸入
cout<<endl;
if(iPass==0) //此條件成立可能由兩種情況引起:輸入了非數字字符被直接重置爲0,或6位全0後正常退出for循環
{
cout<<"密碼要求全爲數字,且不能全0!"<<endl;;
cout<<"請重新輸入密碼: ";
}
else
break;
}
return iPass;
}
//功能描述:業務員登錄
bool Bank::passtaff()
{
Staff s1;
string acc;
char ch;
cout<<"請輸入業務員賬戶:";
cin>>acc;
for(int j=0; j<3; ++j)
{
int pw=0;
cout<<"請輸入密碼:";
for(int i=0; i<6; i++)
{
ch=getch(); //輸入但不顯示
putchar('*'); //輸出*
pw=pw*10+(ch-'0');
}
if(s1.pass(acc,pw))
{
return true;
}
else
{
cout<<"\n用戶名或密碼錯誤。"<<endl;
}
}
cout<<"\n錯誤三次,系統自動退出。"<<endl;
return false;
}
//顯示菜單
void Bank::showMenu()
{
cout<<endl;
cout<<"+--------------------------------+"<<endl;
cout<<"+ 1 開戶 2 銷戶 3 存款 +"<<endl;
cout<<"+ 4 取款 5 查詢 6 轉賬 +"<<endl;
cout<<"+ 7 掛失 8 解掛 0 退出 +"<<endl;
cout<<"+ 9 修改信息 10 個人信息 +"<<endl;
cout<<"+--------------------------------+"<<endl;
}
//開始工作
void Bank::manage()
{
int order=1;
do
{
openfile();
cout<<"請輸入操作指令:";
cin>>order;
switch(order)
{
case 1:
openAccount();
break;
case 2:
deleteAccount();
break;
case 3:
saveMoney();
break;
case 4:
drawMoney();
break;//取款
case 5:
refer();
break;//查詢
case 6:
transfer();
break;//轉賬
case 7:
reportLoss();
break;//掛失
case 8:
hangSolution();
break;//解掛
case 9:
changedisplays();
break;//修改個人信息
case 10:
display();
break;//顯示個人信息
case 0:
cout<<endl;
cout<<"+--------------------------------------+"<<endl;
cout<<"+ 謝謝您的使用,再見! +"<<endl;
cout<<"+--------------------------------------+"<<endl;
exit(1);
break;
}
savefile();
}
while(1);
}
void Bank::openAccount() //開戶
{
int acc; //賬號
char nam[20]; //姓名
int pw; //密碼
char di[30]; //地址
char pho[11];//電話
double mon; //金額
int sta; //狀態
char id[18]; //身份證號
cout<<"正在開戶"<<endl;
acc=10001+nownum;
cout<<"賬號:"<<acc<<endl;
cout<<"戶主姓名:";
cin>>nam;
cout<<"身份證號:";
for(int n=0; n<18; n++)
{
cin>>id[n];
}
cout<<"地址:";
cin>>di;
cout<<"聯繫電話(請輸入正確的手機號碼):";
for(int n=0; n<11; n++)
{
cin>>pho[n];
}
int Pass1, Pass2;
cout<<"密碼:";
Pass1=inputPassword(); //輸入密碼1
cout<<"確認密碼:";
Pass2=inputPassword(); //輸入密碼2
if(Pass1==Pass2)
{
pw=Pass1;
sta=0; //賬戶狀態爲“正常”
cout<<"存入金額:";
cin>>mon;
if(mon>50) //一般銀行開戶最低金額爲50
{
users[nownum].setUser(acc,nam,id,di,pho,pw,mon,sta);//出了好幾次錯的地方 id
++nownum; //正式用戶數增加1,確認了新用戶已經加入
cout<<"開戶成功!"<<endl;
}
else
cout<<"金額少於50元,開戶失敗"<<endl;
}
else
{
cout<<"兩次密碼不一致,未成功開戶!"<<endl; //沒有N++,則讀入的值無效
}
}
void Bank::deleteAccount()
{
int who; //查找到該賬號在數組中對應的下標
who=getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
users[who].showMoney("餘額:"); //提示"餘額"二字,顯示金額
cout<<"確認銷戶(y/n)?";
if(getchar()=='y'||getchar()=='Y')
{
users[who].showMoney("銷戶成功!本次取款金額爲:");
users[who].money=0; //取款後餘額變0
users[who].status=2; //狀態變爲註銷
}
else
{
cout<<"你取消了操作,銷戶失敗!"<<endl;
}
}
}
}
void Bank::saveMoney() //存款
{
int who;
double money;
who=getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
if(users[who].status==0)
{
users[who].showName();
//cout<<endl;
cout<<"輸入存款額:";
cin>>money;
users[who].money+=money;
users[who].showMoney("存款後,金額爲:");
}
else if(users[who].status==1)
{
cout<<"該用戶處於掛失狀態,存款失敗!"<<endl;
}
else if(users[who].status==2)
{
cout<<"該用戶已經銷戶,存款失敗!"<<endl;
}
}
return;
}
void Bank::drawMoney() //取款
{
int who;
double money;
who=getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
if(users[who].isNormalUser())
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"輸入取款額:";
cin>>money;
if(money>users[who].money)
{
cout<<"餘額不足,取款失敗!"<<endl;
}
else
{
users[who].money-=money;
users[who].showMoney("取款後,餘額爲:");
}
}
}
}
return;
}
void Bank::refer() //查詢
{
int who;
int cacc;
char pan;
string cnam;
string sta[3]= {"正常","掛失","已經銷戶"};
string hms;
string cyw;
who=getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
users[who].showMoney("餘額:");
cout<<"狀態:"<<sta[users[who].status]<<endl;
cin>>pan;
if(pan!='y')
{
if(users[who].account!=cacc)
{
cout<<"當前未辦理過任何業務。"<<endl;
}
}
else
{
cout<<"+-------------查詢結束-------------+"<<endl;
cout<<endl;
}
}
}
return;
}
void Bank::transfer() //轉賬
{
int whoout, whoin;
double money;
cout<<"轉出自";
whoout=getUser(); //根據賬號查詢用戶,返回用戶的下標
if(whoout>=0) //說明id賬戶存在
{
if(users[whoout].isNormalUser())
{
users[whoout].showName();
if(users[whoout].passwordIsRight())
{
cout<<"輸入轉賬金額:";
cin>>money;
if(money>users[whoout].money)
{
cout<<"餘額不足,轉賬失敗!"<<endl;
}
else
{
cout<<"轉出到";
whoin = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(whoin>=0) //說明id賬戶存在
{
if(users[whoin].isNormalUser())
{
users[whoout].money-=money;
users[whoin].money+=money;
users[whoout].showMoney("轉賬後,餘額爲:");
}
}
}
}
}
}
return;
}
void Bank::reportLoss() //掛失
{
int who;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
if(users[who].status==0)
{
users[who].status=1;
cout<<"掛失成功"<<endl;
}
else if(users[who].status==1)
{
cout<<"該賬戶已經處於掛失狀態"<<endl;
}
else if(users[who].status==2)
{
cout<<"該賬戶已銷戶,不能掛失"<<endl;
}
}
}
return;
}
void Bank::hangSolution() //解掛
{
int who;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
if(users[who].status==0)
{
cout<<"該賬戶處於正常狀態,不需要解除掛失"<<endl;
}
else if(users[who].status==1)
{
users[who].status=0;
cout<<"解除掛失成功"<<endl;
}
else if(users[who].status==2)
{
cout<<"該賬戶已銷戶,操作無效"<<endl;
}
}
}
return;
}
void Bank::changePassword() //改密
{
int who;
int iPass1, iPass2;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"新密碼:";
iPass1=inputPassword(); //輸入密碼1
cout<<"確認密碼:";
iPass2=inputPassword(); //輸入密碼2
if(iPass1==iPass2)
{
users[who].password=iPass1;
cout<<"密碼修改成功!"<<endl;
}
else
{
cout<<"兩次輸入不同,修改失敗!"<<endl;
}
}
}
return;
}
void Bank::changename() //修改用戶名
{
int who;
string nam;
char pan;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"新姓名:";
cin>>nam; //輸入用戶名
cout<<"確認修改姓名(y/n)?";
cin>>pan;
if(pan=='y')
{
users[who].name=nam;
cout<<"用戶名修改成功!"<<endl;
}
else
{
cout<<"你已取消修改!"<<endl;
}
}
}
return;
}
void Bank::changedizhi() //修改地址
{
int who;
char di[30];
char pan;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"新地址:";
gets(di); //輸入地址
cout<<"確認修改地址(y/n)?";
cin>>pan;
if(pan=='y')
{
for(int n=0; n<30; n++)
users[who].dizhi[n]=di[n];
cout<<"地址修改成功!"<<endl;
}
else
{
cout<<"你已取消修改!"<<endl;
}
}
}
return;
}
void Bank::changeidcard() //修改身份證號
{
int who;
char id[18];
char pan;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"新身份證號:";
gets(id); //輸入身份證號
cout<<"確認修改身份證號(y/n)?";
cin>>pan;
if(pan=='y')
{
for(int n=0; n<18; n++)
{
users[who].idcard[n]=id[n];
}
cout<<"身份證號修改成功!"<<endl;
}
else
{
cout<<"你已取消修改!"<<endl;
}
}
}
return;
}
void Bank::changephone() //修改手機號
{
int who;
char pho[11];
char pan;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].showName();
if(users[who].passwordIsRight())
{
cout<<"新電話號:";
gets(pho); //輸入電話號
cout<<"確認修改電話號(y/n)?";
cin>>pan;
if(pan=='y')
{
for(int n=0; n<11; n++)
{
users[who].phone[n]=pho[n];
}
cout<<"電話號修改成功!"<<endl;
}
else
{
cout<<"你已取消修改!"<<endl;
}
}
}
return;
}
void Bank::changedisplays() //修改個人信息
{
int n;
cout<<"+----請輸入想要修改的項目:----+"<<endl;
cout<<"+-----------1.姓名-------------+"<<endl;
cout<<"+-----------2.身份證號---------+"<<endl;
cout<<"+-----------3.密碼-------------+"<<endl;
cout<<"+-----------4.地址-------------+"<<endl;
cout<<"+-----------5.電話-------------+"<<endl;
cout<<"請輸入:";
cin>>n;
switch(n)
{
case 1:
changename();
break;
case 2:
changeidcard();
break;
case 3:
changePassword();
break;
case 4:
changedizhi();
break;
case 5:
changephone();
break;
}
}
int Bank::getUser() //查找用戶 二分法
{
int id;
cout<<"賬號:";
cin>>id;
int index=-1;
int low=0, high=nownum-1, mid;
while(low<=high)
{
mid = (low+high)/2;
if(users[mid].account==id)
{
index=mid;
break; //找到了,立即退出循環
}
else if (users[mid].account>id)
high=mid-1;
else
low=mid+1;
}
if (index<0)
cout<<"該用戶不存在,本次操作失敗!"<<endl;
return index; //若找到,其值在0~N-1間,否則,保持-1
}
void Bank::openfile()//導入文件
{
ifstream infile("users.txt",ios::in);
int i=0;
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
nownum=jis(); //jis裏面有return當前總用戶數
users=new User[nownum+1];
while(infile>>users[i].account>>users[i].name>>users[i].idcard>>users[i].dizhi>>users[i].phone>>users[i].password>>users[i].money>>users[i].status)
{
++i;
}
infile.close();
}
void Bank::savefile() //保存文件
{
ofstream qingkfile("users.txt",ios::trunc);//trunc 打開並清空文件
qingkfile.close();
ofstream outfile("users.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0; i<nownum; ++i) //注意這裏的空格即‘\t',關係到是否能正常保存用戶信息
{
outfile<<users[i].account<<'\t'<<users[i].name<<'\t';
for(int n=0; n<18; n++)
{
outfile<<users[i].idcard[n];
}
outfile <<'\t'<<users[i].dizhi<<'\t';
for(int n=0; n<11; n++)
{
outfile<<users[i].phone[n];
}
outfile<<'\t'<<users[i].password<<'\t'<<users[i].money<<'\t'<<users[i].status<<endl;
}
outfile.close();
delete []users; //釋放剩餘空間
return;
}
void Bank::display() //顯示個人信息
{
int who;
who = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(who>=0) //說明id賬戶存在
{
users[who].displays();
}
}
int Bank::jis()
{
int acc; //賬號
char nam[20]; //姓名
char id[18]; //身份證號
char di[30];//地址
char pho[11];//電話
int pw; //密碼
double mon; //金額
int sta; //狀態
int i=0;
int nownum;
ifstream infile("users.txt");
while(infile>>acc>>nam>>id>>di>>pho>>pw>>mon>>sta)
{
++i;
}
infile.close();
return i;
}
staff.cpp
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include "staff.h"
using namespace std;
bool Staff::pass(string sacc,int ps)
{
bool judge=false;
fstream infile("staff.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>stafft.account>>stafft.password;
if(stafft.account==sacc&&ps==stafft.password)
{
judge=true;
break;
}
}
infile.close();
return judge;
}
user.cpp
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <conio.h> //將密碼變成*號
#include "user.h"
using namespace std;
bool inputpassw()
{
char ch; //接收字符形式密碼(這裏必須輸入字符,因爲只有字符才能轉化成'*')
int iPass=0; //要轉換爲ascll的形式
int i;
while(1)
{
for(i=0; i<6; i++)
{
ch=getch(); //輸入但不顯示
putchar('*'); //輸出*
if(iPass=ch)
return true; //假設第一位輸入1,第二位輸入2,那麼此時密碼就應該是1*10+2=12####
else
{
iPass=0;
return false;
break; //退出for循環後,再次接受
}
}
}
return iPass;
}
void User::setUser(int acc,string nam,char id[18],char di[30],char pho[11],int pw, double mon,int sta)
{
account=acc;
name=nam;
for(int n=0; n<30; n++)
{
dizhi[n]=di[n];
}
for(int n=0; n<11; n++)
{
phone[n]=pho[n];
}
password=pw;
money=mon;
for(int n=0; n<18; n++)
{
idcard[n]=id[n];
}
status=sta;
}
bool User::passwordIsRight()
{
int iPass;
bool right=true;
cout<<"輸入密碼:";
iPass=inputpassw();
if(iPass!=password)
{
right = false;
cout<<"輸入密碼錯誤,不能繼續操作!"<<endl;
}
return right;
}
void User::showName()
{
cout<<"用戶名:"<<name<<endl;
}
void User::showMoney(string prompt)
{
cout<<prompt<<money<<endl;
}
bool User::isNormalUser() //判斷用戶狀態是否正確
{
bool normal = true;
if(status!=0)
{
normal = false;
cout<<"該賬戶處於"<<(status==1?"掛失":"銷戶")<<"狀態,不能繼續操作..."<<endl;
}
return normal;
}
void User::displays()
{
cout<<"卡號:"<<account<<endl;
cout<<"姓名:";
cout<<name<<endl;
cout<<"地址:";
cout<<dizhi<<endl;
cout<<"電話: ";
for(int n=0; n<11; n++)
{
cout<<phone[n];
}
cout<<endl;
}
bank.h
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include "user.h"
using namespace std;
class Bank
{
public:
Bank()
{
nownum=0;
}
bool passtaff(); //業務員信息
void openfile(); //導入文件
void savefile2(int acc, string nam, char id[18], char di[30],char pho[11],int pw, double mon,int sta); //保存一行數據
void caculfile(); //計算
void savefile(); //保存文件
void manage(); //開始工作
void showMenu(); //顯示菜單
void openAccount(); //開戶
void deleteAccount(); //銷戶
void saveMoney(); //存款
void drawMoney(); //取款
void refer(); //查詢
void transfer(); //轉賬
void reportLoss(); //掛失
void hangSolution(); //解掛
void changePassword(); //改密
void changename(); //改姓名
void changeidcard(); //改身份證
void changedizhi(); //改地址
void changephone(); //改電話
void display(); //顯示個人信息
void changedisplays(); //修改個人信息
int getUser(); //查找用戶
int jis(); //計算用戶數
private:
int nownum;
User *users;
//User數組,動態數組 體現在保存文件方面 delete[]users
};
#endif // BANK_H_INCLUDED
staff.h
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#ifndef STAFF_H_INCLUDED
#define STAFF_H_INCLUDED
#include <iostream>
#include <cmath>
using namespace std;
struct staffTatus
{
string account;
int password;
};
class Staff
{
public:
bool pass(string sacc,int ps);
private:
staffTatus stafft;
};
#endif // STAFF_H_INCLUDED
user.h
/*版權所有
*文件名稱:銀行儲蓄系統
*當前版本:V1.0
*作者:曹莉萍
*完成日期:2015.07.18
*/
#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED
#include <iostream>
#include <cmath>
using namespace std;
//定義user類
class User
{
public:
User() {}
void setUser(int acc, string nam,char id[18],char di[30],char pho[11],int pw, double mon,int sta);
bool isNormalUser(); //判斷用戶狀態是否正確
bool passwordIsRight(); //判斷密碼是否正確
void showName(); //輸出用戶名
void displays(); //輸出用戶個人信息
void showMoney(string prompt);//顯示餘額
friend class Bank; //友元類
private:
int account;
string name;
char dizhi[30];
char phone[11];
int password;
double money;
char idcard[18];
int status;
};
#endif // USER_H_INCLUDED