-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadministrator.cpp
More file actions
319 lines (319 loc) · 10.7 KB
/
administrator.cpp
File metadata and controls
319 lines (319 loc) · 10.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include<iostream>
#include "administrator.h"
#include "function.h"
#include<cstring>
using namespace std;
bool administrator::log_in(char name[11],char password[21]) {
if (strcmp(name, "admin") == 0 ) {
if (strcmp(password, "123456") == 0) {
return true;
}
printf("-----Wrong password entered! Login failed! Return to the main menu!-----\n");
}
printf("-----Wrong username entered! Login failed! Return to the main menu!-----\n");
return false;
}
string administrator::write_check_all_goods() {
string str("SELECT * FROM commodity");
write_commands(str);
return str;
}
void administrator::check_all_goods() {
goods* p = create_goods(); goods* head = p;
if (!p) {
printf("No goods!\n");
return;
}
printf("**************************************************************************************************\n");
printf("%-14s %-14s %-10s %-10s %-14s %-12s %-10s\n", "commodityID", "commodityName", "price", "number", "sellerID", "addedDate", " state ");
for (; p; p = p->next)
printf("%-14s %-14s %-10.1lf %-10d %-14s %-12s %-10s\n", p->ID, p->name, p->price, p->items_number, p->seller_ID,p->time, p->state);
printf("**************************************************************************************************\n");
clear(head);
return;
}
string administrator::write_check_all_orders() {
string str("SELECT * FROM order");
write_commands(str);
return str;
}
void administrator::check_all_orders() {
orders* p = create_orders();
orders* head = p;
if (!p) {
printf("No orders!\n");
return;
}
printf("**************************************************************************************************\n");
printf("%-14s %-14s %-10s %-10s %-10s %-14s %-14s\n", "orderID", "commodityID", "unitPrice", "number", "date", "sellerID","buyerID");
for (; p; p = p->next)
printf("%-14s %-14s %-10.1lf %-10d %-10s %-14s %-14s\n", p->ID, p->goods_ID, p->currency, p->quantity, p->time, p->seller_ID, p->buyer_ID);
printf("**************************************************************************************************\n");
clear(head);
}
string administrator::write_check_all_users() {
string str("SELECT * FROM user");
write_commands(str);
return str;
}
void administrator::check_all_users() {
users* p = create_users();
users* head = p;
if (!p) {
printf("No user!\n");
return;
}
printf("************************************************************************************************************************\n");
printf("%-14s %-10s %-20s %-20s %-40s %-10s %-10s\n", "userID","username","password","phoneNumber","address","balance","userState" );
for(;p;p=p->next)
printf("%-14s %-10s %-20s %-20s %-40s %-10.1lf %-10s\n", p->ID, p->name, p->password, p->contact_details, p->address, p->money_rest, p->state);
printf("*************************************************************************************************************************\n");
clear(head);
}
string administrator::write_remove_goods(char ID[5]) {
string str("UPDATE commodity SET state = removed WHERE commodityID = ");
str.append(ID);
write_commands(str);
return str;
}
void administrator::remove_goods(const char ID[5]) {
goods* p = create_goods();
goods* head = p,*tmp=p;
for (; p && strcmp(p->ID, ID) != 0; p = p->next);
sprintf(p->state, "removed");
printf("Remove successfully!\n");
write_goods(head);
clear(tmp);
}
string administrator::write_block_users(char ID[5]) {
string str("UPDATE commodity SET state = removed WHERE sellerID = ");
str.append(ID);
write_commands(str);
string new_str("UPDATE user SET userState = inactive WHERE userID = ");
new_str.append(ID);
write_commands(new_str);
string ans(ID);
return ans;
}
void administrator::block_users(const char ID[5]) {
goods* p = create_goods();
goods* head = p;
users* q = create_users();
users* head1 = q,*tmp=q;
for (; p && strcmp(p->seller_ID, ID) != 0; p = p->next);
if (!p) {
for (; q; q = q->next) {
if (strcmp(q->ID, ID) == 0)
sprintf(q->state, "inactive");
}
write_users(head1);
clear(head1);
return;
}
else {
p = head;
for (; p; p = p->next) {
if (strcmp(p->seller_ID, ID) == 0)
sprintf(p->state, "removed");
}
for (; q; q = q->next) {
if (strcmp(q->ID, ID) == 0)
sprintf(q->state, "inactive");
}
write_users(head1);
write_goods(head);
clear(head1); clear(head);
printf("Block successful\n");
return;
}
}
string administrator::write_search_for_goods(char name[11]) {
string str("SELECT * FROM commodity WHERE commodityName CONTAINS ");
str.append(name);
write_commands(str);
return str;
}
void administrator::search_for_goods(const char name[11]) {
goods* p = create_goods();
goods* head = p;
for (; p && !strstr(p->name, name); p = p->next);
if (!p) {
printf("No matching items found!\n");
return;
}
p = head;
printf("**************************************************************************************************\n");
printf("%-14s %-14s %-10s %-10s %-14s %-12s %-14s\n", "commodityID", "commodityName", "price", "number", "sellerID", "addedDate", " state ");
for (; p; p = p->next) {
if(strstr(p->name,name))
printf("%-14s %-14s %-10.1lf %-10d %-14s %-12s %-14s\n", p->ID, p->name, p->price, p->items_number, p->seller_ID, p->time, p->state);
}
printf("**************************************************************************************************\n");
clear(head);
}
void administrator::read_commands_administrator(const string& strings) {
if (strcmp(strings.c_str(), "SELECT * FROM commodity") == 0) {
check_all_goods();
}
else if (strcmp(strings.c_str(), "SELECT * FROM order")==0) {
check_all_orders();
}
else if (strcmp(strings.c_str(), "SELECT * FROM user")==0) {
check_all_users();
}
else if (strstr(strings.c_str(), "SELECT * FROM commodity WHERE commodityName CONTAINS ")) {
int tmp = strings.find("CONTAINS");
string tp(strings.substr(tmp + 9, 200));
search_for_goods(tp.c_str());
}
else if (strstr(strings.c_str(), "UPDATE commodity SET state = removed WHERE commodityID = ")) {
int tmp = strings.find("commodityID");
string tp(strings.substr(tmp + 14, 4));
remove_goods(tp.c_str());
}
else {
block_users(strings.c_str());
}
}
void administrator::administrator_interface() {
int choice;
char check;
char ch;
char temp;
string str;
string ID;
string name;
string ID1;
while (true) {
administrator_title();
printf("Please enter your action:");
if (cin >> choice) {
check=getchar();
if (check != '\n') {
printf("Invalid input!\nPlease try again!\n");
cin.clear(); cin.sync();
continue;
}
switch (choice) {
case 1:
str = write_check_all_goods();
read_commands_administrator(str);
break;
case 2:
printf("Please enter the name of the item you want to search for:"); getline(cin,name);
if (name.size()>11) {
printf("Invalid input!\nFail to search!\n");
cin.clear(); cin.sync();
break;
}
if (judge_goods_name_valid((char*)name.c_str())) {
str = write_search_for_goods((char*)name.c_str());
read_commands_administrator(str);
}
break;
case 3:
printf("Please enter the ID of the product you want to remove:"); getline(cin, ID);
if (ID.size()>4) {
printf("Invalid input!\nFail to remove!\n");
cin.clear(); cin.sync();
break;
}
if (judge_goods_ID_valid((char*)ID.c_str())&&judge_goods_ID_exist((char*)ID.c_str())) {
goods* p = create_goods();
goods* tmp = p;
for (; p && strcmp(p->ID, (char*)ID.c_str()) != 0; p = p->next);
if (strcmp(p->state, "removed") == 0) {
printf("This product has already been removed!\n");
break;
}
printf("Confirm that if you want to remove the item:\n");
printf("***********************************************************************************************************************\n");
printf("%-14s %-14s %-10s %-10s %-14s %-12s %-10s\n", "commodityID", "commodityName", "price", "number", "sellerID", "addedDate", " state");
printf("%-14s %-14s %-10.1lf %-10d %-14s %-12s %-10s\n", p->ID, p->name, p->price, p->items_number, p->seller_ID, p->time, p->state);
printf("***********************************************************************************************************************\n");
printf("Please confirm: (y/n) :");
scanf("%c", &ch);
temp = getchar();
if (temp != 10 && temp != 13) {
printf("Invalid input!\nFail to remove!\n");
while ((temp = getchar()) != EOF && temp != '\n');
cin.clear(); cin.sync();
break;
}
if (ch== 'y') {
str = write_remove_goods((char*)ID.c_str());
read_commands_administrator(str);
}
else if (ch == 'n') {
printf("Fail to remove!\n");
}
else {
printf("Invalid input!\n");
}
clear(tmp);
}
break;
case 4:
str = write_check_all_orders();
read_commands_administrator(str);
break;
case 5:
str = write_check_all_users();
read_commands_administrator(str);
break;
case 6:
printf("Please enter the ID of the user you want to ban:"); getline(cin,ID1);
if (ID1.size()>4) {
printf("Invalid input!\nFail to ban!\n");
cin.clear(); cin.sync();
break;
}
if (judge_users_ID_valid((char*)ID1.c_str())&&judge_users_ID_exist((char*)ID1.c_str())) {
users* q = create_users();
users* tmp = q;
for (; q && strcmp(q->ID, (char*)ID1.c_str()) != 0; q = q->next);
if (strcmp(q->state, "inactive") == 0) {
printf("User has already been blocked!\n");
break;
}
printf("Confirm that if you want to ban this user:\n");
printf("**************************************************************************************************************************\n");
printf("%-14s %-10s %-20s %-20s %-40s %-10s %-10s\n", "userID", "username", "password", "phoneNumber", "address", "balance", "userState");
printf("%-14s %-10s %-20s %-20s %-40s %-10.1lf %-10s\n", q->ID, q->name, q->password, q->contact_details, q->address, q->money_rest, q->state);
printf("***************************************************************************************************************************\n");
printf("Please confirm: (y/n) :");
scanf("%c", &ch);
temp = getchar();
if (temp != 10 && temp != 13) {
printf("Invalid input!\nFail to ban!\n");
while ((temp = getchar()) != EOF && temp != '\n');
cin.clear(); cin.sync();
break;
}
if (ch == 'y') {
str = write_block_users((char*)ID1.c_str());
read_commands_administrator(str);
}
else if(ch=='n') {
printf("Fail to ban!\n");
}
else {
printf("Invalid input!\n");
}
}
break;
case 7:
return;
break;
default:
printf("Invalid input!\n");
}
}
else {
cin.clear(); cin.sync();
printf("Invalid input!\n");
char c;
while ((c = getchar()) != EOF && c != '\n');
}
}
}