-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.cpp
383 lines (358 loc) · 13.2 KB
/
admin.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "admin.h"
#include "md5.h"
#include "conio.h"
extern Log rec;
extern Library lib;
extern string account;
extern MD5 md5;
extern int user_index;
string Status[3] = {"Admin", "Staff", "Customer"}; // array of string, use to indicate the status of user
string Vip[4] = {"non-vip", "vip", "vvip", "vvvip"}; // array of string, use to indicate the status of user
void Admin::AddUser() // Add new user
{
User newuser; // Define 'newuser' to User class
string name, pwd;
int status, vip_level;
while (1){
system("cls");
cout << "Please input the username that you want to register:" << endl;
cin.ignore();
getline(cin, name); // Input username
if (lib.findUser(name) < 0) // findUser() will return -1 if not user with name name
break;
else
{
cout << "Username already exist! " << endl;
Sleep(2000);
}
}
cout << "Please input your password" << endl; // The case that user doesn't exist in User.txt
cin >> pwd;
while (1)
{
system("cls");
cout << "Please input the user type:" << endl;
cout << "1: Admin" << endl;
cout << "2: Employee" << endl;
cout << "3: Customer" << endl;
cout << "0: Back to previous page" << endl;
cin >> status;
if (status == 0) return;
else if (status == 1 || status == 2 || status == 3)
break;
else
cout << "Please input number from 0 to 3!" << endl;
}
while (1)
{
system("cls");
cout << "Please input the user's vip level:" << endl;
cout << "0: normal (non-vip)" << endl;
cout << "1: vip (10% discount)" << endl;
cout << "2: vvip (20% discount)" << endl;
cout << "3: vvvip (30% discount)" << endl;
cout << "9: Back to previous page" << endl;
cin >> vip_level;
if (vip_level == 9) return;
else if (vip_level == 0 || vip_level == 1 || vip_level == 2 || vip_level == 3)
break;
else
cout << "Please input number from 0 to 3!" << endl;
}
newuser.SetUsername(name);
md5.reset();
md5.update(pwd);
newuser.SetPassword(md5.toString());
newuser.SetStatus(status);
newuser.SetVip(vip_level);
lib.UserArray.push_back(newuser); // Add newuser to lib.UserArray
cout << "Add user succsessfully!" << endl;
Sleep(1000);
cout << "Automatic quit in 2 seconds." << endl;
Sleep(2000);
rec.UserLog(account, name, "add", 0); // Update UserLog.txt with "add" operation
PrintUserArray(); // Output user information to User.txt
}
void Admin::AddCustomer() // Add new user
{
User newuser; // Define 'newuser' to User class
string name, pwd;
while (1){
system("cls");
cout << "Please input the username that you want to register:" << endl;
cin.ignore();
getline(cin, name); // Input username
if (lib.findUser(name) < 0) // findUser() will return -1 if not user with name name
break;
else
{
cout << "Username already exist! " << endl;
Sleep(2000);
}
}
cout << "Please input your password" << endl; // The case that user doesn't exist in User.txt
cin >> pwd;
newuser.SetUsername(name);
md5.reset();
md5.update(pwd);
newuser.SetPassword(md5.toString());
newuser.SetStatus(3);
newuser.SetVip(0);
lib.UserArray.push_back(newuser); // Add newuser to lib.UserArray
cout << "Sign up succsessfully!" << endl;
Sleep(1000);
cout << "Automatic return to the Login page in 2 seconds." << endl;
Sleep(2000);
rec.UserLog(account, name, "add", 0); // Update UserLog.txt with "add" operation
PrintUserArray(); // Output user information to User.txt
}
void Admin::DelUser() // Delete old user
{
system("cls");
int choice, userPos;
string name;
cout << "Please input the username that you want to delete:" << endl;
cin.ignore();
getline(cin, name); // Input the username
userPos = lib.findUser(name);
if (userPos >= 0)
{
cout << "Are you sure you want to delete this user? (press 1: continue, 0: quit)" << endl; // comfirm delete
cin >> choice;
if (choice == 1) // Make sure want to delete this user
{
lib.UserArray[userPos].SetStatus(0);
cout << "Delete user succsessfully!" << endl;
PrintUserArray();
rec.UserLog(account, name, "del", 0); // Update UserLog.txt with "del" operation
}
}
else
cout << "User not exist! " << endl; // Username not exist in User.txt or status equal to 0
Sleep(1000);
}
void Admin::SetUser() // Change user's status (like Customer, Staff, Administartor)
{
system("cls");
int choice, userPos; // userPos: user's position in the library
string name;
cout << "Please input the username that you want to change its information:" << endl;
cin.ignore();
getline(cin, name); // Input the username
userPos = lib.findUser(name);
if (userPos >= 0) // user exist
{
cout << "The user's current status is: " << Status[lib.UserArray[userPos].GetStatus()-1] << endl;
cout << "Do you want to change it? (input 1 for YES and 0 for NO )" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Please input a number: 3 for Customer, 2 for Staff, 1 for Administartor, 0 for quit:" << endl;
cin >> choice;
if (choice != 0) // Make sure want to change this user's status
{
lib.UserArray[userPos].SetStatus(choice);
cout << "Change user status succsessfully!" << endl;
}
else return;
}
cout << "The user's current vip level is: " << Vip[lib.UserArray[userPos].GetVip()] << endl;
cout << "Do you want to change it? (input 1 for YES and 0 for NO )" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Please input a number: 0 for non-vip, 1 for vip, 2 for vvip, 3 for vvvip, 9 for quit:" << endl;
cin >> choice;
if (choice != 9) // Make sure want to change this user's status
{
lib.UserArray[userPos].SetVip(choice);
cout << "Change user vip level succsessfully!" << endl;
}
else return;
}
Sleep(1000);
cout << "Automatic quit in 2 seconds." << endl;
Sleep(2000);
PrintUserArray(); // Output user information to User.txt
rec.UserLog(account, name, "set", choice); // Update UserLog.txt with "set" operation
}
else
cout << "User not exist! " << endl; // Username not exist in User.txt or status equal to 0(deleted)
Sleep(1000);
}
void Admin::ListUser() // List all users' information
{
system("cls");
int temp;
cout << "This is the information of all the users:" << endl;
cout << "------------------------------------------------" << endl;
cout << "| Username | Status | Vip Level |" << endl;
cout << "------------------------------------------------" << endl;
for (size_t i = 0; i < lib.UserArray.size(); i++) // Output each user information
if (lib.UserArray[i].GetStatus() != 0)
cout << "|" << setw(21) << lib.UserArray[i].GetUsername() << "|" << setw(12)
<< Status[lib.UserArray[i].GetStatus() - 1] << "|" << setw(11)
<< Vip[lib.UserArray[i].GetVip()] << "|" << endl;
cout << "------------------------------------------------" << endl;
cout << endl;
cout << "Input 0 to exit" << endl;
cin >> temp;
while (temp != 0)
cin >> temp;
}
void Admin::ShowSalesData() // Show the sales data of each book
{
system("cls");
int temp;
cout << "This is the sales data of all the books:" << endl;
cout << "----------------------------------------------------------------------------------------------" << endl;
cout << "| ISBN | Name | Category | Quantity | Unit Price | Sales Volume |" << endl;
cout << "----------------------------------------------------------------------------------------------" << endl;
int maxSalesBookIndex = 0, maxSalesNumber = 0, minSalesBookIndex = 0, minSalesNumber = 0;
for (size_t i = 0; i < lib.BookArray.size(); i++) // Output each book information
{
cout << "|" << setw(12) << lib.BookArray[i].GetIsbn() << "|" << setw(25) << lib.BookArray[i].GetName() << "|" << setw(14) << lib.BookArray[i].GetCategoryName()<< "|"
<< setw(10) << lib.BookArray[i].GetNumber() << "|"<< setw(12) << setiosflags(ios::fixed) << setprecision(2) <<lib.BookArray[i].GetPrice() << "|"
<< setw(14) << lib.BookArray[i].GetNumberOfSale()<< "|" << endl;
if (lib.BookArray[i].GetNumberOfSale() > maxSalesNumber)
{
maxSalesBookIndex = i;
maxSalesNumber = lib.BookArray[i].GetNumberOfSale();
}
else if (lib.BookArray[i].GetNumberOfSale() < maxSalesNumber)
{
minSalesBookIndex = i;
minSalesNumber = lib.BookArray[i].GetNumberOfSale();
}
}
cout << "----------------------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "The best seller is \"" << lib.BookArray[maxSalesBookIndex].GetName() << "\", it's sales Volume is: "
<< lib.BookArray[maxSalesBookIndex].GetNumberOfSale() << endl;
cout << "The worst seller is \"" << lib.BookArray[minSalesBookIndex].GetName() << "\", it's sales Volume is: "
<< lib.BookArray[minSalesBookIndex].GetNumberOfSale() << endl << endl;
cout << "Tips: For the book which sells best, you may consider restocking more copies of this book. And for the book which sells worst, you may consider lowering it's pricee and not to restock it next time." << endl << endl;
while(1)
{
cout << "Input 0 to exit, 1 to display the books on web: " << endl;
cin >> temp;
if (temp == 1)
{
ofstream doc("html_file.html");
if (doc.fail())
{
cout << "ERROR: there was an error exporting the file" << endl;
abort(); // Exit
}
else
{
cout << "please wait..." << endl;
doc << "<!DOCTYPE html><html><head><title>Departmental Store Management System</title><style>td{border: solid 2px #ddd;}tr:hover{background:#ddd;}</style></head><body><h2>Books List</h2><table><tr> <td>ISBN</td> <td>Book name</td> <td>Category </td> <td>Quantity </td> <td>Unit price </td> <td>Sales Volume </td></tr> " << endl;
for (size_t i = 0; i < lib.BookArray.size(); i++)
doc << "<tr>" << "<td>" << lib.BookArray[i].GetIsbn() << " " << "</td>" << "<td>" << lib.BookArray[i].GetName() << " " << "</td>" << "<td>"<< lib.BookArray[i].GetCategoryName() << " " << "</td>" << "<td>" << lib.BookArray[i].GetNumber() << "</td>" << "<td>" << lib.BookArray[i].GetPrice() << "</td>" << "<td>" << lib.BookArray[i].GetNumberOfSale() << " " << "</td>" << "</tr>";
doc << "</table></body></html>";
doc << "<br>";
doc << "The best seller is \"" << lib.BookArray[maxSalesBookIndex].GetName() << "\", it's sales Volume is: "
<< lib.BookArray[maxSalesBookIndex].GetNumberOfSale() << "<br>";
doc << "The worst seller is \"" << lib.BookArray[minSalesBookIndex].GetName() << "\", it's sales Volume is: "
<< lib.BookArray[minSalesBookIndex].GetNumberOfSale() << "<br>";
doc << "<br>";
doc << "Tips: For the book which sells best, you may consider restocking more copies of this book. And for the book which sells worst, you may consider lowering it's pricee and not to restock it next time." << "<br>" << "<br>";
doc.close();
system("start html_file.html");
}
}
else if (temp == 0)
break;
else
cout << "Please input 0 or 1!" << endl;
}
}
void Admin::Interface() // Admin interface
{
LoadUser();
char command;
int stop = 0;
system("cls");
string str;
rec.CheckShortOfBookLog();
rec.CheckCommentLog();
while (1) {
system("cls");
cout << "Welcome " << lib.UserArray[user_index].GetUsername() << "! You are in the administartor mode" << endl;
cout << "Please choose what you want to do:" << endl;
while (1) { // The operation list
cout << "1 ----- Add new user" << endl;
cout << "2 ----- Delete user" << endl;
cout << "3 ----- Change user information" << endl;
cout << "4 ----- Add new book" << endl;
cout << "5 ----- Delete book" << endl;
cout << "6 ----- Change book information" << endl;
cout << "7 ----- List book (ALL)" << endl;
cout << "8 ----- List book (By Category)" << endl;
cout << "9 ----- List book (By Name)" << endl;
cout << "* ----- Show more book information by ISBN number" << endl;
cout << "| ----- List all users' information" << endl;
cout << "@ ----- Lookup sales data" << endl;
cout << "$ ----- Lookup net profit" << endl;
cout << "% ----- Change my password" << endl;
cout << "0 ----- Exit;" << endl;
cin >> command;
if (command != '@' && command != '*' && command != '|' && command != '%' && command != '$' && (command < '0' || command > '9')) { // If command doesn't match input rule
cout << endl << "Please input '@', '*','|','%','$' or number between 0-9:" << endl;
}
else break;
}
switch (command)
{
case '1':
AddUser(); // Add new user
break;
case '2':
DelUser(); // Delete old user
break;
case '3':
SetUser(); // Change user's status (like Customer, Staff, Administartor) and their vip level
break;
case '4':
AddBook(); // Add new books
break;
case '5':
DelBook(); // Delete some books
break;
case '6':
SetBook(); // Change books' information
break;
case '7':
ListBookAll(); // List all book
break;
case '8':
ListBookByC(); // List book by category
break;
case '9':
ListBookByN(); // List book by name
break;
case '*':
BookInfor(); // See more information of one book
break;
case '%':
ChangePsw(); // Change password
break;
case '$':
ViewProfit(); // Lookup profit
break;
case '@':
ShowSalesData(); // Show the sales data of each book
break;
case '|':
ListUser(); // List all users' information
break;
case '0':
stop = 1;
cout << "System will quit after 3 seconds " <<endl;
Sleep(3000);
break;
}
if (stop) break;
}
}