首先是在linux地下實現的,創建了四個文件,主要實現流程:
註冊-登陸-存款-取款-轉賬-更改密碼-查詢個人信息-顯示全部賬戶-退出系統
廢話不多說,直接看代碼:
Blank.h
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
#define MAX_SIZE 65535
//互斥鎖的初始化
static pthread_mutex_t mutex_lock=PTHREAD_MUTEX_INITIALIZER;
//定義用戶的兩種狀態,在線和已註銷
enum STATUS{ONLINE,LOGOUT};
//有一個缺點註銷後的賬號不能再使用
struct user
{
string _name; //用戶名
string _id; //身份證
string _block_name; //卡號
string _passwd; //密碼
int _money; //存的錢
int _total_money; // 總共餘額
// STATUS status; //用戶的狀態
void insert_user(const string &name="",const string &id="",const string &block_name="",\
const string &passwd="",int money=0)
{
_name=name;
_id=id;
_block_name=block_name;
_passwd=passwd;
_money=money;
_total_money=money;
//status=ONLINE;
}
bool operator !=(const user &s)
{
return (strcmp(_block_name.c_str(),(s._block_name).c_str())!=0);
}
};
//註冊--》登陸--》存款--》取款--》查詢--》顯示全部信息--》更改密碼--》註銷用戶--》退出系統
class Bank
{
private:
user value_user;
vector<struct user> vector_user;
private:
Bank(const Bank &b);
Bank& operator=(const Bank &b);
public:
Bank()
{}
~Bank() {}
bool operator !=(const user &s)
{
return value_user.operator!=(s);
}
public:
//轉賬
bool transfer_account(const string& block_name,const int number)
{
string tmp_block_name;
cout<<"please enter block_name:";
fflush(stdout);
cin>>tmp_block_name;
string passwd;
cout<<"please enter passwd:";
fflush(stdout);
cin>>passwd;
vector<struct user>::iterator tmp=Find(tmp_block_name,passwd);
if(tmp !=vector_user.end())
{
if(tmp->_total_money <number)
{
cout<<"餘額不足"<<endl;
return false;
}
vector<struct user>::iterator it=Find(block_name);
pthread_mutex_lock(&mutex_lock); //加鎖
tmp->_total_money-=number;
it->_total_money+=number;
pthread_mutex_unlock(&mutex_lock);//解鎖
return true;
}
return false;
}
//註銷用戶 註銷後的賬號不能再次使用
bool logout(const string &block_name,const string &passwd)
{
vector<struct user>::iterator tmp=Find(block_name,passwd);
if(tmp !=vector_user.end())
{
vector_user.erase(tmp);
return true;
}
return false;
}
//更改密碼
bool change_passwd(const string &block_name,const string &passwd)
{
vector<struct user>::iterator tmp=Find(block_name,passwd);
if(tmp==vector_user.end())
return false;
cout<<"please enter old passwd :";
fflush(stdout);
string old_passwd="";
cin>>old_passwd;
if(strcmp(old_passwd.c_str(),passwd.c_str())==0)
{
cout<<"please enter new passwd:";
fflush(stdout);
string new_passwd="";
cin>>new_passwd;
pthread_mutex_lock(&mutex_lock); //加鎖
tmp->_passwd=new_passwd;
pthread_mutex_unlock(&mutex_lock);//解鎖
}
return true;
}
//用戶登陸
bool log_in(const string &block_name,const string &passwd)
{
vector<struct user>::iterator tmp=Find(block_name,passwd);
if(tmp !=vector_user.end())
return true;
return false;
}
//判斷身份證是否有效,可以查看這張身份證開零幾張卡,但遺憾的是我沒有實現
bool effective(const string &id)
{
vector<struct user>::iterator it=vector_user.begin();
for(; it!=vector_user.end(); ++it)
{
if(strcmp((it->_id).c_str(),id.c_str())==0)
return true;
}
return true;
}
//將整數轉換成字符串
string get_string(int number)
{
char arr[MAX_SIZE]; //因爲最大的字符串值爲MAX_SIZE
memset(arr,'\0',sizeof(arr));
string name="";
sprintf(arr,"%d",number);//將整數轉換成字符串後存在arr數組中
name=arr;
return name;
}
//系統爲用戶分配一個賬號
string get_name()
{
static int tmp=1;
if(tmp >MAX_SIZE)
return "";
string name="";
pthread_mutex_lock(&mutex_lock); //加鎖
name+=get_string(tmp);
tmp++;
pthread_mutex_unlock(&mutex_lock);//解鎖
return name;
}
//系統分配的賬號都以622123開始
string get_block_name()
{
string name="622123";
pthread_mutex_lock(&mutex_lock); //加鎖
name+=get_name();
pthread_mutex_unlock(&mutex_lock);//解鎖
return name;
}
//用戶註冊函數
bool Register(const string &name,const string &id)//用戶註冊
{
if(!effective(id))//判斷身份證是否有效
{
cout<<"已經存在此身份證"<<endl;
return false;
}
string passwd; //初始狀態的密碼
string certain_passwd; //確定密碼
cout<<"please enter passwd:";
fflush(stdout);
cin>>passwd;
cout<<"please again enter passwd:";
fflush(stdout);
cin>>certain_passwd;
//兩次密碼一致才註冊成功
//然後把結構體保持在vector數組中
if(strcmp(passwd.c_str(),certain_passwd.c_str())==0)
{
//////////加鎖
pthread_mutex_lock(&mutex_lock); //加鎖
value_user.insert_user(name,id,get_block_name(),passwd);
vector_user.push_back(value_user);
pthread_mutex_unlock(&mutex_lock);//解鎖
return true;
}
else
return false;
}
//尋找已經在vector數組存在的用戶
vector<struct user>::iterator Find(const string &block_name,const string &passwd)
{
vector<struct user>::iterator it=Find(block_name);
if(it !=vector_user.end())
{
if(strcmp(it->_passwd.c_str(),passwd.c_str())==0)
return it;
}
return vector_user.end();
}
///重載Find
vector<struct user>::iterator Find(const string &block_name)
{
vector<struct user>::iterator it=vector_user.begin();
for(; it!=vector_user.end(); ++it)
{
if(strcmp(it->_block_name.c_str(),block_name.c_str())==0)
return it;
}
return vector_user.end();
}
//存款,存款之前保證用戶已經登陸
bool deposit(const string &block_name,const string &passwd,const int money)//存款
{
if(log_in(block_name,passwd))
{
vector<struct user>::iterator tmp=Find(block_name,passwd);
if(tmp !=vector_user.end())
{
pthread_mutex_lock(&mutex_lock); //加鎖
tmp->_money=money;
tmp->_total_money+=money;
pthread_mutex_unlock(&mutex_lock);//解鎖
}
return true;
}
return false;
}
//取款
bool withdraw_money(const string &block_name,const string &passwd,const int &money)
{
vector<struct user>::iterator tmp=Find(block_name,passwd);
if(tmp !=vector_user.end())
{
if(money > tmp->_total_money)
{
cout<<"餘額不足"<<endl;
return false;
}
//////////加鎖
pthread_mutex_lock(&mutex_lock); //加鎖
tmp->_money-=money;
tmp->_total_money-=money;
pthread_mutex_unlock(&mutex_lock);//解鎖
///////////
return true;
}
return false;
}
////////////////////////////
//
bool check(const string &block_name,const string &passwd)//用卡號查找
{
vector<struct user>::iterator it=Find(block_name,passwd);
if(it !=vector_user.end())
{
cout<<"用戶:"<<it->_name<<"\t身份證:"<<it->_id;
cout<<"\t卡號:"<<it->_block_name;
cout<<"\t餘額:"<<it->_total_money<<endl;
return true;
}
return false;
}
//顯示所有信息
void show()
{
vector<struct user>::iterator it=vector_user.begin();
int i=1;
for(; it !=vector_user.end(); ++it)
{
cout<<"開戶第 "<<i++<<" 人"<<endl;
cout<<"用戶:"<<it->_name<<"\t身份證:"<<it->_id<<"\t";
cout<<"卡號:"<<it->_block_name<<"\t密碼:"<<it->_passwd<<"\t";
cout<<"餘額:"<<it->_total_money<<endl;
}
cout<<"開戶總人數:"<<i-1<<endl;
i=0;
}
};
Blank.cpp
#include "Bank.h"
static string name;//用戶
static string id; //身份證
static string block_name;//卡號
static string passwd; //密碼
int output()
{
cout<<"******************************************"<<endl;
cout<<"* *"<<endl;
cout<<"* [1]:註冊 [2]:登陸 *"<<endl;
cout<<"* [3]:存款 [4]:取款 *"<<endl;
cout<<"* [5]:查詢 [6]:改密 *"<<endl;
cout<<"* [7]:註銷 [8]:轉賬 *"<<endl;
cout<<"* [9]:顯示 [0]:退出 *"<<endl;
cout<<"* *"<<endl;
cout<<"******************************************"<<endl;
char key;
cout<<" 請選擇:";
fflush(stdout);
cin>>key;
if(key >='0' && key <='9')
return key;
while(key <'0' || key >'9')
{
cout<<" 請重新選擇:";
fflush(stdout);
cin>>key;
}
return key;
}
void input()
{
cout<<" please enter block_name: ";
fflush(stdout);
cin>>block_name;
cout<<" please enter passwd: ";
fflush(stdout);
cin>>passwd;
}
void register_user()
{
cout<<" please enter name: ";
fflush(stdout);
cin>>name;
cout<<" please enter id: ";
fflush(stdout);
cin>>id;
}
void bank_function()
{
Bank atm;
size_t money=0;
bool flag=false;
while(1)
{
char res=output();
switch(res)
{
case '1':
register_user();
flag=atm.Register(name,id);//用戶註冊
if(flag)
cout<<" 【 註冊成功 】"<<endl;
else
cout<<" 【 註冊失敗 】"<<endl;
break;
case '2':
{
int count=3;
while(1)
{
if(count==0)
break;
input();
flag=atm.log_in(block_name,passwd);
if(flag)
{
cout<<" 【 登陸成功 】"<<endl;
break;
}
else
{
cout<<" 【 登陸失敗 】"<<endl;
cout<<"【 你還有** "<<--count<<" **次機會 】"<<endl;
}
}
}
break;
case '3':
input();
cout<<" please enter money: ";
fflush(stdout);
cout<<" 【請輸入小於 5000 的整數!】"<<endl;
cin>>money;
while(money > 5000)
{
cout<<"【 請輸入小於 5000 的整數!】"<<endl;
cin>>money;
}
if(atm.deposit(block_name,passwd,money))//存款
cout<<"【 存款成功 】"<<endl;
else
cout<<"【 存款失敗 】"<<endl;
break;
case '4':
input();
cout<<" please enter money: ";
fflush(stdout);
cout<<"【 請輸入小於 5000 的整數!】"<<endl;
cin>>money;
while(money > 5000)
{
cout<<"【 請輸入小於 5000 的整數!】"<<endl;
cin>>money;
}
flag=atm.withdraw_money(block_name,passwd,money);
if(flag)
cout<<"【 取款成功 】"<<endl;
else
cout<<"【 取款不成功 】"<<endl;
break;
case '5':
input();
flag=atm.check(block_name,passwd);//用卡號查找
if(flag)
cout<<"【 查詢成功 】"<<endl;
else
cout<<"【 查詢失敗 】"<<endl;
break;
case '6':
input();
flag=atm.change_passwd(block_name,passwd);
if(flag)
cout<<"【 更改密碼成功 】"<<endl;
else
cout<<"【 更改密碼失敗 】"<<endl;
break;
case '7':
input();
flag=atm.logout(block_name,passwd);
if(flag)
cout<<"【 註銷成功 】"<<endl;
else
cout<<"【 註銷失敗 】"<<endl;
break;
case '8':
cout<<" please enter card_number: ";
fflush(stdout);
cin>>block_name;
cout<<" please enter money: ";
fflush(stdout);
cout<<"【 請輸入小於 5000 的整數!】"<<endl;
cin>>money;
flag=atm.transfer_account(block_name,money);
if(flag)
cout<<"【 轉賬成功 】"<<endl;
else
cout<<"【 轉賬失敗 】"<<endl;
break;
case '9':
atm.show();
break;
case '0':
cout<<"【 歡迎使用ATM自能系統 】"<<endl;
return;
default:
system("clear");
break;
}
}
}
int main()
{
bank_function();
return 0;
}
makefile
DES=Bank.cpp
CER=Bank
$(CER):$(DES)
g++ -o $@ $^ -g
.PHONY:out
out:
mkdir out
mv $(CER) ./out
chmod 755 table.sh
cp table.sh ./out
cd out
sh table.sh
.PHONY:clean
clean:
rm -rf out
chmod 644 table.sh
table.sh
#/bin/bash
./out/Bank