#include"common.h"
static PNStation* ptrains = NULL; // 所有车次的数组
static PNCustomer pcustomers = NULL; // 所有用户信息的链表
static PNCustomer pOneCustomer = NULL; // 该登录用户
static PTicket* ptickets = NULL; // 自动生成的所有车票
static PCbuy pcbuys = NULL; // 购买了的用户链表
static int trainSize = 0; // 有多少班列车
static int bookCount = 0; // 一共订票的次数
static int ubookCount = 0; // 一共退票的次数
static PQueue *pqs = NULL;
static int queueSize = 0;
void init()
{
// 初始化函数,首先读取customer,trainnu等DB的信息
// 通过trainnu自动构建出ticket的链表
trainSize = 0; // 一共有多少班车次
// 从文件中读取train的信息,是一个train的数组
ptrains = readTrainnu(&trainSize);
// 生成所有train的车票
ptickets = createAllTrainTicket(ptrains, trainSize);
// 从文件中读取customer的信息,是一个customer的链表
pcustomers = readAllCustomer();
pqs = (PQueue *)malloc(sizeof(NQueue)* 20);
// --------------------读取车票内容------------------------
// 测试用
//printAllCustomer(pcustomers);
//traverseTrainnu(ptrains[2]);
//
//PNStation p = createTrainnu();
//writeTrainnu(p);
//PNStation p = createTrainnu();
//writeTrainnu(p);
}
int adminSearchTrainByNu()
{
title();
printf("请输入您需要查找的车次号 = ");
int x = selectIn();
PNStation psearch = searchTrainByNu(x, ptrains, trainSize);
printf("\n");
if (NULL == psearch)
{
printf("没有您查找的列次.....\n");
goback();
return A_T_MENU;
}
else
{
printf("您查找的车次路程为 :\n");
traverseTrainnu(psearch);
}
goback();
return A_T_MENU;
}
int adminSearchAllTrain()
{
title();
printf("\n");
printf("所有车次如下....\n");
traverseAllTrainnu(ptrains, trainSize);
goback();
return A_T_MENU;
}
int adminAddTrain()
{
title();
PNStation pnew = createTrainnu();
writeTrainnu(pnew);
ptrains = readTrainnu(&trainSize);
ptickets = createAllTrainTicket(ptrains, trainSize);
goback();
return A_T_MENU;
}
int adminDelTrain()
{
title();
printf("\n");
printf("所有车次如下....\n");
traverseAllTrainnu(ptrains, trainSize);
printf("请输入您需要删除的车次号 = ");
int x = selectIn();
delOneTrain(ptrains, trainSize, x);
writeAllTrainnu(ptrains, trainSize);
ptrains = readTrainnu(&trainSize);
ptickets = createAllTrainTicket(ptrains, trainSize);
goback();
return A_T_MENU;
}
int adminUpdate()
{
title();
goback();
return A_T_MENU;
}
int adminSearchByName()
{
title();
char name[10];
int size = 0;
int i = 0;
printf("请输入您希望查询的用户姓名\n");
scanf("%s", name);
PNCustomer *parr = searchByName(name, pcustomers, &size);
if (0 == size)
{
printf("没有您所要查找的用户\n");
}
else
{
for (i = 0; i < size; i++)
{
printOneCustomer(parr[i]);
}
}
goback();
free(parr);
return A_C_MENU;
}
int adminSearchAll()
{
title();
printf("所有用户信息.......\n");
printAllCustomer(pcustomers);
goback();
return A_C_MENU;
}
int adminAddUser()
{
title();
printf("请输入您要输入的用户信息\n");
PNCustomer pcnew = createCustomer();
writeOneCustomer(pcnew);
insertCustomer(pcustomers, pcnew);
printf("用户添加成功.......\n");
goback();
return A_C_MENU;
}
int adminDeleteUser()
{
title();
int id = 0;
int i = 0;
int isSuccess = FAILURE;
printf("所有用户信息.......\n");
printAllCustomer(pcustomers);
printf("请根据用户id对用户进行删除\n");
printf("请输入id = ");
id = selectIn();
pcustomers = deleteCustomer(pcustomers, id, &isSuccess);
if (FAILURE == isSuccess )
{
printf("没有您输入的id号\n");
}
else
{
printf("用户删除成功!!!!!");
printf("是否写入文件中?(1 写入 / 0 不写入)\n");
i = selectIn();
while (i != 1 && i != 0)
{
printf("请输入1或0\n");
i = selectIn();
}
if (1 == i)
{
writeAllCustomer(pcustomers);
printf("已经写入文件\n");
}
else
{
printf("未写入文件,下次启动时用户依然存在\n");
}
}
goback();
return A_C_MENU;
}
int adminUpdateUser()
{
title();
int sid = 0;
int isSuccess = FAILURE;
printf("所有用户信息.......\n");
printAllCustomer(pcustomers);
printf("请根据用户id对用户进行修改\n");
printf("请输入id = ");
sid = selectIn();
pcustomers = updateCustomer(pcustomers, sid, &isSuccess);
if (FAILURE == isSuccess )
{
printf("没有您输入的id号\n");
}
else
{
printf("更新成功,写入文件.....");
writeAllCustomer(pcustomers);
}
goback();
return A_C_MENU;
}
PBticket* selectTrain(int *ticketCounts)
{
int i = 0;
PBticket *pbtickets = NULL;
char start[10];
char end[10];
PNStation ptemp = NULL;
printf("请输入您的起始站\n");
scanf("%s", start);
printf("请输入您的终点站\n");
scanf("%s", end);
pbtickets = searchAllBticket(start, end, ptickets, trainSize);
printf("\n");
printf("满足您希望的列车信息.....\n");
*ticketCounts = printAllPBticket(pbtickets, trainSize);
if (0 == (*ticketCounts))
{
printf("没有符合您列车的信息..\n");
}
else
{
printf("\n");
printf("所有车次如下....\n");
traverseAllTrainnu(ptrains, trainSize);
}
return pbtickets;
}
int selfBookTrain()
{
title();
printf("\n");
printf("请输入您车票的起始站,终点站,系统将帮您查找车票\n");
int ticketCounts = 0;
PBticket *pbtickets = selectTrain(&ticketCounts);
PBticket pb = NULL;
if (ticketCounts == 0)
{
goback();
return CUSTOMER_MENU;
}
printf("\n");
printf("请选择您要预订的车票的编号[ ]\n");
int select = selectIn();
while (select >= ticketCounts || select < 0)
{
printf("请输入准确的编号\n");
select = selectIn();
}
// 实际选择的与真正存在链表中的顺序进行转换
pb = selectToRealPBticket(pbtickets, select, trainSize);
if (pcbuys == NULL)
{
bookTicket(pb);
pcbuys = createOneCbuy(pOneCustomer, pb);
pcbuys = createBuyLink(pcbuys);
printf("\n");
printf("您购买的票的信息为.....\n");
traverseBuyLink(pcbuys);
printf("\n");
printf("恭喜您订票成功!!!!\n");
bookCount ++;
}
else
{
if (FAILURE == judgeHadBuy(pcbuys, pOneCustomer))
{
printf("每个人只能购买一张票\n");
}
else
{
if (FAILURE == bookTicket(pb))
{
printf("该票已经被购买.....\n");
PCbuy pnew = createOneCbuy(pOneCustomer, pb);
isInsertInQueues(pb, pnew, trainSize);
}
else
{
PCbuy pnew = NULL;
pnew = createOneCbuy(pOneCustomer, pb);
pcbuys = insertBuyLink(pcbuys, pnew);
printf("\n");
printf("您购买的票的信息为.....\n");
printOneCbuy(pnew);
printf("恭喜您订票成功!!!!\n");
bookCount ++;
bookTicket(pb);
}
}
}
goback();
return CUSTOMER_MENU;
}
void isInsertInQueues(PBticket pb, PCbuy pcb, int trainSize)
{
printf("是否要排队(1 是/0 不是)? \n");
int select = selectIn();
while (select != 1 && select != 0)
{
printf("没有您输入的选项\n");
select = selectIn();
}
if (select == 1)
{
PTicket pt = searchTicketByNu(ptickets, pb->b_trainnu, trainSize);
int num = searchNumToQueue(pb->b_sstation, pb->b_tstation, pt);
int i = 0;
PQueue pq = isQueueExist(pqs, num , queueSize, &i);
if (pq != NULL)
{
pqs[i] = inCbuyQueue(pq, pcb);
}
else
{
PQueue p = createQueue(pcb, num);
pqs[queueSize ++] = p;
}
printf("如果有人退票,系统将第一时间通知您..........\n");
评论0