您的位置:首页 > 运维架构 > Linux

linux ATM自定取款机简单实现

2016-05-08 10:23 1086 查看
首先是在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




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: