-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.cpp
More file actions
executable file
·225 lines (188 loc) · 6.61 KB
/
Copy pathcart.cpp
File metadata and controls
executable file
·225 lines (188 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "cart.h"
#include "check_time.h"
#include "database.h"
#include "shopping.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cart_choose_2() {
char choose[10]; // 记录管理员操作时的选择
int choose_num;
// 用户界面
printf("\n---------------操作选项---------------\n");
printf("\n1. 购买.\n2. 移除.\n0. 返回.\n");
printf("\n-------------------------------------\n");
printf("请按数字键选择要执行的操作:\n");
scanf("%s", choose);
if (strcmp(choose, "1") != 0 && strcmp(choose, "2") != 0 &&
strcmp(choose, "0") != 0) {
printf("\n您的输入有误, 请按照操作选项再次输入:\n\n");
choose_num = cart_choose_2();
} else
choose_num = atoi(choose);
return choose_num;
}
void cart_result_2(char user_id[30]) {
int i = 0, k = 0;
printf("您的购物车信息如下:\n");
printf("|序号\t|菜品名\t\t|饭店名\t\t|购买数量\n");
while (shopping_cart[i].purchase_num != 0) {
printf("|%2d\t|%-15s\t|%-15s\t|%4d\n", i + 1, shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
i++;
}
int choise_num = i;
char choose_str[10];
int choose_num;
do {
printf("\n请选择您想查看的菜品(按0返回)\n");
scanf("%s", choose_str);
// 检验输入是否正确
if (strcmp(choose_str, "0") == 0)
return;
i = 0;
k = 0;
while (choose_str[i] != '\0') {
if (choose_str[i] >= '0' && choose_str[i] <= '9')
i++;
else {
printf("\n您的输入有误,请重新输入\n");
k = 1;
break;
}
}
if (k)
continue;
choose_num = atoi(choose_str);
if (choose_num > choise_num) {
printf("\n您的输入有误,请重新输入\n");
continue;
} else
break;
} while (1);
choose_num -= 1;
printf("您查看的信息为:\n");
printf("菜品名: %s\n饭店编号: %s\n购买数量: %d\n",
shopping_cart[choose_num].goods_name, // 货物ID
shopping_cart[choose_num].shop_id, // 饭店ID
shopping_cart[choose_num].purchase_num //购买数量
);
int choose_num_2 = cart_choose_2();
if (choose_num_2 == 1) {
int temp_return = shopping(user_id, shopping_cart[choose_num].goods_name,
shopping_cart[choose_num].shop_id,
shopping_cart[choose_num].purchase_num);
if (temp_return == 1) {
shopping_cart[choose_num].purchase_num = -1;
// 多次刷新数据确保完整
database_shopping_cart(user_id, 1);
database_shopping_cart(user_id, 0);
printf("您的菜品购买完成, "
"谢谢惠顾!\n详细信息请前往\"主菜单-查看已完成订单\".\n");
} else if (temp_return == 0)
printf("抱歉, 您选择的菜品存货不足, 请选择其他菜品.");
else
printf("抱歉, 您的余额不足, 请先充值.");
char screen[10];
printf("\n请输入任意字符并按回车键以继续...\n");
scanf("%s", screen); // 延长屏幕显示时间
}
if (choose_num_2 == 2) {
shopping_cart[choose_num].purchase_num = -1;
database_shopping_cart(user_id, 1);
database_shopping_cart(user_id, 0);
return;
}
return;
}
int cart_choose() {
char choose[10]; // 记录管理员操作时的选择
int choose_num;
// 用户界面
printf("\n---------------操作选项---------------\n");
printf("\n1. 全部购买.\n2. 查看详情.\n0. 返回.\n");
printf("\n-------------------------------------\n");
printf("请按数字键选择要执行的操作:\n");
scanf("%s", choose);
if (strcmp(choose, "1") != 0 && strcmp(choose, "2") != 0 &&
strcmp(choose, "0") != 0) {
printf("\n您的输入有误, 请按照���作选项再次输入:\n\n");
choose_num = cart_choose();
} else
choose_num = atoi(choose);
return choose_num;
}
int cart(char user_id[30]) {
int choose = cart_choose(); // 用户界面传入指令
int i = 0, k = 0; // 记录是否有购买完成
switch (choose) {
case 1: {
i = 0;
k = 0;
int temp_return;
printf("以下为购买状态(菜品名 : 饭店ID : 购买数量)\n");
do {
temp_return =
shopping(user_id, shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
if (temp_return == 1) {
printf("%s : %s : %d\n购买成功!\n", shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
//缓存
k += 1;
shopping_cart[i].purchase_num = -1;
} else if (temp_return == 0) {
printf("%s : %s : %d\n存货不足.\n", shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
} else {
printf("%s : %s : %d\n余额不足.\n", shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
}
i++;
} while (shopping_cart[i].purchase_num != 0);
if (k != 0)
printf("%d种菜品购买完成, "
"谢谢惠顾!\n详细信息请前往\"主菜单-查看已完成订单\".\n",
k);
else
printf("抱歉, 您选择的菜品均无法购买, 请先充值或删除部分订单.");
char screen[10];
printf("\n请输入任意字符并按回车键以继续...\n");
scanf("%s", screen); // 延长屏幕显示时间
return 1;
break;
}
case 2: {
cart_result_2(user_id);
cart(user_id);
break;
}
case 0: {
break;
}
}
return 0;
}
int cart_main(char user_id[30]) {
int i = 0;
char screen[10]; // 屏显字符串
database_shopping_cart(user_id, 0);
if (shopping_cart[0].purchase_num == 0) {
printf("您暂时没有购物车信息, 请先添加部分订单.\n");
printf("\n请输入任意字符并按回车键以继续...\n");
scanf("%s", screen); // 延长屏幕显示时间
return 0;
}
printf("您的购物车信息如下:\n");
printf("|序号\t|菜品名\t\t|饭店名\t\t|购买数量\n");
while (shopping_cart[i].purchase_num != 0) {
printf("|%2d\t|%-15s\t|%-15s\t|%4d\n", i + 1, shopping_cart[i].goods_name,
shopping_cart[i].shop_id, shopping_cart[i].purchase_num);
i++;
}
cart(user_id);
// 多次刷新数据确保完整
database_shopping_cart(user_id, 1);
database_shopping_cart(user_id, 0);
return 1; //指用户已经结算购物车
}