(三)超市收银系统程序设计
要求:
1)都有菜单页,有用户登录和退出环节,有退出系统菜单项;
2)采用结构体数组变量完成数据的储存(用动态分配结构体数组或单向链表加分)
3)至少有15种商品,每次出现商品名称和代号
4)用户登录后能收银:输入代号和数量,计算总额,支持每次购买多种商品。有库存数据。
5)支持单用户模式,至少有5个用户,不可重复登录,必须前一一个用户退出后,才能登录下一个用户账号。
6)运行日志:记录用户的登录和退出情况(时间用户操作内容)
7)历史记录:所有购买记录(时间商品数量金额操作人)
/*结账办理*/
void checkout(pUser user, pGoodsInfo head) {
char bill[2048] = { 0 };
double total = 0;
printf("╔----------------------------╗\n");
printf(" $ 结账办理 $\n");
printf("╚----------------------------╝\n");
sprintf(bill, " %-16s %-16s %-16s %-16s\n", "编号", "名称", "价格", "购买数量");
while (1) {
pGoodsInfo goods = NULL;
char id[128] = { 0 };
printf("输入要购买的商品代号:");
scanf("%s", id);
goods = findGoodsInfoNodeByID(head, id);
if (goods) {
int count = 0;
showGoodsTitle();
showGoods(goods);
printf("输入购买份数:");
scanf("%d", &count);
if (count <= goods->stock) {
int option;
goods->stock -= count;
total += count * goods->price;
sprintf(bill + strlen(bill), " %-16s %-16s %-16.2lf %-16d\n", goods->id, goods->name, goods->price, count);
saveGoodsInfoFile(head);
addDealLog(user, "结账", goods->id, goods->name, goods->price, count);
printf("商品购买成功!");
printf("是否继续购买商品?(1 是 2 否)");
scanf("%d", &option);
if (option != 1) break;
} else {
printf(" 库存不足!\n");
}
} else {
printf(" 未找到商品信息!\n");
}
}
printf("╔----------------------------╗\n");
printf(" $ 结算清单 $\n");
printf(bill);
printf(" 消费合计:%.2lf\n", total);
printf("╚----------------------------╝\n");
}
/*退款办理*/
void drawback(pUser user, pGoodsInfo head) {
char bill[2048] = { 0 };
double total = 0;
printf("╔----------------------------╗\n");
printf(" $ 退款办理 $\n");
printf("╚----------------------------╝\n");
sprintf(bill, " %-16s %-16s %-16s %-16s\n", "编号", "名称", "价格", "购买数量");
while (1) {
pGoodsInfo goods = NULL;
char id[128] = { 0 };
printf("输入要购买的商品代号:");
scanf("%s", id);
goods = findGoodsInfoNodeByID(head, id);
if (goods) {
int count = 0;
showGoodsTitle();
showGoods(goods);
printf("输入退款份数:");
scanf("%d", &count);
if (count > 0 && count < salesCount(goods)) {
int option;
goods->stock += count;
total += count * goods->price;
sprintf(bill + strlen(bill), " %-16s %-16s %-16.2lf %-16d\n", goods->id, goods->name, goods->price, count);
saveGoodsInfoFile(head);
printf("商品退款成功!");
addDealLog(user, "退款", goods->id, goods->name, goods->price, count);
printf("是否继续退款商品?(1 是 2 否)");
scanf("%d", &option);
if (option != 1) break;
} else {
printf(" 数值不合法!\n");
}
} else {
printf(" 未找到商品信息!\n");
}
}
printf("╔----------------------------╗\n");
printf(" $ 退款清单 $\n");
printf(bill);
printf(" 退款合计:-%.2lf\n", total);
printf("╚----------------------------╝\n");
}
/*收营员菜单*/
void menuUser(pGoodsInfo head, pUserList user_list) {
pUser user = loginUser(user_list);
if (user) {
addUserLog(user, "登录");
/*循环菜单*/
while (1) {
int option;
printf("╔----------------------------╗\n");
printf(" $ 收营员功能菜单 $\n");
printf("\n");
printf(" 【1】 浏览商品\n");
printf(" 【2】 结账办理\n");
printf(" 【3】 退款办理\n");
printf(" 【0】 退出系统\n");
printf("\n");
printf("╚----------------------------╝\n");
printf(" 请选择:");
scanf("%d", &option);
switch (option) {
case 1:
browseGoodssOption(head);
break;
case 2:
checkout(user, head);
break;
case 3:
drawback(user, head);
break;
case 0:
addUserLog(user, "退出");
return;
}
}
}
}
/*用户注册*/
void registerUser(pUserList list) {
User user;
memset(&user, 0, sizeof(User));
printf("╔----------------------------╗\n");
printf(" $ 用户注册 $\n");
printf("╚----------------------------╝\n");
editUser(&user);
if (findUserListByID(list, user.id, 0) == -1) {
addUserList(list, &user);
saveUserFile(list);
printf("用户注册成功!\n");
} else {
printf("该账号已存在,注册失败!\n");
}
}
/*主菜单*/
void menu(pGoodsInfo* goods_head, pUserList user_list) {
while (1) {
int option;
printf("╔----------------------------╗\n");
printf(" $ 超市收银系统 $\n");
printf("\n");
printf(" 【1】 用户登录\n");
printf(" 【2】 用户注册\n");
printf(" 【3】 后台管理\n");
printf(" 【0】 退出系统\n");
printf("\n");
printf("╚----------------------------╝\n");
printf(" 请选择:");
scanf("%d", &option);
switch (option) {
case 1:
menuUser(*goods_head, user_list);
break;
case 2:
registerUser(user_list);
break;
case 3:
menuAdmin(goods_head, user_list);
break;
case 0:
return;
}
}
}
传送门:https://pan.baidu.com/s/1JJs9vbZahUCB6cQvXLgAVg?pwd=1111