ATM项目主要分为两人大模块:
客户端
1、进入时的功能开户、销户、登录、解锁
开户:输入姓名、身份证号、设置密码,如果开户成功,则服务器上保存一个账号信号(一个账号存一个文件,文件名建议是账号)。
销户:输入帐号、密码,服务器询问是否确认销户,如果确认则服务器删除帐号文件,并记录帐号。
登录:输入账号、密码,三次错误账号锁定。
解锁:输入账号、身份证号解锁。
2、登录成功:存钱、取钱、转账、查询、修改密码
存钱:输入存钱金额
取钱:输入取钱金额
转账:目标帐号和要转的金额
查询:不需要输入数据
修改密码:原密码和新密码
服务器
如果识别功能:根据消息的类型识别客户端请求的功能。
开启服务各项功能接收消息
代码如下
- 客户端
client.c
//登陆后的功能
#include "display.h"
#include "tools.h"
#include "struct.h"
int sockfd;
char* IP = "192.168.1.116";
short PORT = 2000;
typedef struct sockaddr SA;
//开户
void create(void)
{
struct User use;
printf("请输入你的姓名:");
scanf("%s",use.name);
printf("请输入你的身份证号码:");
scanf("%lld",&use.id);
printf("请输入你的密码:");
get_pd(use.pass,9);
printf("请输入开户预存金额:");
while(1)
{
scanf("%lf",&use.money);
if(use.money > 0 && use.money <10000000000000000) break;
else printf("金额输入不合法,请重新输入:");
}
clear_stdin();
use.cnt = 3;
use.lock = 0;
use.is_online = 0;
struct Msg msg = {1,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
printf("开户成功!\n");
printf("您的卡号:%s\n",msg.use.card_id);
clear_stdin();
anykey_continue();
}
//存款
void save(char* num)
{
struct User use;
strcpy(use.card_id,num);
printf("请输入您要储存的金额:");
while(1)
{
scanf("%lf",&use.money);
if(use.money > 0 && use.money <10000000000000000) break;
else printf("金额输入不合法,请重新输入:");
}
struct Msg msg = {2,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
printf("储存成功!\n");
printf("您的余额:%.2lf\n",msg.use.money);
clear_stdin();
anykey_continue();
}
//取款
void extract(char* num)
{
struct User use;
strcpy(use.card_id,num);
printf("请输入您要取出的金额:");
while(1)
{
scanf("%lf",&use.money);
if(use.money > 0 && use.money <10000000000000000) break;
else printf("金额输入不合法,请重新输入:");
}
struct Msg msg = {3,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
if(msg.use.money != -1)
{
printf("取款成功!\n");
printf("您的余额:%.2lf\n",msg.use.money);
clear_stdin();
anykey_continue();
}
else
{
printf("余额不足,取款失败!\n");
sleep(1);
}
}
//查询
void inquiry(char* num)
{
struct User use={};
strcpy(use.card_id,num);
struct Msg msg = {4,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
printf("您的卡号:%s\n",msg.use.card_id);
printf("您的姓名:%s\n",msg.use.name);
printf("您的余额:%.2lf\n",msg.use.money);
clear_stdin();
anykey_continue();
}
//转账
void transfer(char* num)
{
struct User use;
strcpy(use.card_id,num);
printf("请输入您要转账的金额:");
while(1)
{
scanf("%lf",&use.money);
if(use.money > 0 && use.money <10000000000000000) break;
else printf("金额输入不合法,请重新输入:");
}
while(1)
{
printf("请输入您要转账的卡号:");
scanf("%s",use.transfer_id);
if(0 != strcmp(use.transfer_id,num)) break;
else printf("无法给自己账户转账(无意义操作)\n");
}
struct Msg msg = {5,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
if(msg.use.money == use.money)
{
if(0 != strcmp(msg.use.transfer_id,"F"))
{
printf("转账成功!\n");
clear_stdin();
anykey_continue();
}
else
{
printf("转账账户不存在,转账失败!\n");
clear_stdin();
anykey_continue();
}
}
else
{
printf("卡号余额不足,转账失败!\n");
clear_stdin();
anykey_continue();
}
}
//修改密码
void changepass(char* num)
{
struct User use={};
strcpy(use.card_id,num);
char str1[9] = {},str2[9] = {};
while(1)
{
printf("请输入您的新密码:");
get_pd(str1,9);
printf("请再输入您的新密码:");
get_pd(str2,9);
if(0 == strcmp(str1,str2)) break;
else printf("两次密码输入不同,请重新输入!\n");
}
strcpy(use.pass,str1);
struct Msg msg = {8,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
printf("修改成功\n");
clear_stdin();
anykey_continue();
}
//下线
void quit(char* num)
{
struct User use={};
strcpy(use.card_id,num);
struct Msg msg = {10,use};
send(sockfd,&msg,sizeof(Msg),0);
recv(sockfd,&msg,sizeof(Msg),0);
printf("退出成功\n");
sleep(1);
}
void func(char* num)
{
while(1)
{
display();
switch(get_cmd('1','6'))
{
case '1':save(num);break;
case '2':extract(num);break;
case '3':inquiry(num);break;
case '4':transfer(num);break;
case '5':changepass(num);break;
case '6':quit(num);return;
}
}
}
//登录
v