-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.cpp
More file actions
404 lines (341 loc) · 12.4 KB
/
Admin.cpp
File metadata and controls
404 lines (341 loc) · 12.4 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <iostream>
#include <string>
#include <iomanip>
#include "Admin.hpp"
#include "Customer.hpp"
#include "Feedback.hpp"
#include "Shared_Variables.hpp"
#include "Utilities.hpp"
#include <ctime>
using namespace std;
Admin::Admin()
{
this->adminName = "admin";
this->password = "123";
}
Admin::~Admin()
{
}
bool Admin::login(string adminName, string password)
{
if (adminName == this->adminName && password == this->password) return true;
else return false;
}
void Admin::logout()
{
cout << "Logout successful!" << endl;
}
void Admin::displayUniversity()
{
uniList.displayList(uniList.getHead(), -1, "Admin");
}
void Admin::sortUniversities()
{
uniList.sortUniversities(getSortField());
uniList.displayList(uniList.getHead(), -1, "Admin");
}
void Admin::searchUniversities()
{
uniList.searchUniversities(getSearchField(), "Admin");
}
void Admin::displayCustomerDetails()
{
customerList.displayList();
}
void Admin::modifyCustomerDetails()
{
cin.ignore();
string customerID;
string newUsername;
string newPassword;
string newEmail;
// Read customer ID to modify
cout << endl << "Please enter Customer ID : ";
getline(cin, customerID);
CustomerNode* temp = customerList.getCustomer(customerID);
while (temp == nullptr) {
cout << "Invalid Customer ID." << endl;
cout << "Please enter a valid Customer ID : ";
getline(cin, customerID);
temp = customerList.getCustomer(customerID);
}
// Read field to modify
cout << "1. Modify username" << endl;
cout << "2. Modify password" << endl;
cout << "3. Modify email" << endl;
cout << "4. Back" << endl;
int modifyCustomerOption = readInteger(1,4);
if (modifyCustomerOption == 1)
{
cout << "Please enter the new username: " << endl;
cin >> newUsername;
temp->customer.setUsername(newUsername);
cout << "Username modified successfully!" << endl;
}
else if (modifyCustomerOption == 2)
{
cout << "Please enter the new password: " << endl;
cin >> newPassword;
temp->customer.setPassword(newPassword);
cout << "Password modified successfully!" << endl;
}
else if (modifyCustomerOption == 3)
{
cout << "Please enter the new email: " << endl;
cin >> newEmail;
temp->customer.setEmail(newEmail);
cout << "Email modified successfully!" << endl;
}
else if (modifyCustomerOption == 4)
{
return;
}
}
void Admin::deleteCustomerAccount()
{
// Display all inactive customers
string customerID;
cout << endl << "Customer that logged in 180 days ago: " << endl;
time_t currentTime = time(nullptr);
CustomerNode* current = customerList.getHead();
CustomerNode* previous = nullptr;
cout << string(105, '-') << endl;
cout << std::left << setw(18) << "Customer ID" << setw(20) << "Username"
<< setw(30) << "Email" << setw(20) << "Password" << setw(15) << "Last Logged in" << endl;
cout << string(105, '-') << endl;
while (current != nullptr)
{
struct tm loginTime = current->customer.getLastLoginTime();
time_t loginTimeT = mktime(&loginTime);
char formattedTime[50];
strftime(formattedTime, sizeof(formattedTime), "%d-%m-%Y", &loginTime);
int elapsedDays = static_cast<int>((currentTime - loginTimeT) / (24 * 60 * 60));
if (elapsedDays >= 180)
{
cout << std::left << setw(18) << current->customer.getCustomerID() << setw(20) << current->customer.getUsername()
<< setw(30) << current->customer.getEmail() << setw(20) << current->customer.getPassword() << setw(15) << formattedTime << endl;
}
current = current->nextCustomer;
}
cout << string(105, '-') << endl;
cout << endl << "List ends here!" << endl << endl;
current = customerList.getHead();
previous = nullptr;
//Read customer ID to delete
cout << "Please enter customer ID to delete: ";
cin >> customerID;
while (current != nullptr)
{
if (current->customer.getCustomerID() == customerID)
{
if (previous == nullptr)
{
customerList.setHead(current->nextCustomer);
}
else
{
previous->nextCustomer = current->nextCustomer;
}
delete current;
cout << "User with customerID " << customerID << " has been deleted." << endl;
return;
}
previous = current;
current = current->nextCustomer;
}
cout << "User with customerID " << customerID << " not found." << endl;
}
void Admin::displayAllFeedbacks()
{
system("cls");
feedbackList.displayList();
FeedbackNode* current = feedbackList.getHead();
cout << "Please select a feedback to view (Press 0 to go back): ";
int viewFeedbackOption = readInteger(0, feedbackList.getSize());
if (viewFeedbackOption == 0) {
return;
}
displaySelectedFeedback(feedbackList.getFeedbackNode(viewFeedbackOption, &feedbackList));
}
void Admin::displaySelectedFeedback(FeedbackNode* feedback)
{
cout << "University Name: " << feedback->university->institutionName << endl;
cout << "Customer ID: " << feedback->customerID << endl;
char formattedTime[50];
strftime(formattedTime, sizeof(formattedTime), "%d-%m-%Y %a %H:%M:%S", &feedback->timePosted);
cout << "Last Updated: " << formattedTime << endl;
cout << "Feedback: " << feedback->feedbackContent << endl;
if (feedback->replies != nullptr) {
cout << "Replies: " << endl;
ReplyNode* currentReplies = feedback->replies;
while (currentReplies != nullptr) {
if (currentReplies->isAdmin) {
cout << "Admin: " << currentReplies->content << endl;
}
else {
cout << feedback->customerID << ": " << currentReplies->content << endl;
}
currentReplies = currentReplies->nextReply;
}
}
cout << endl;
cout << "1. Write a Reply" << endl;
cout << "2. View Previous Feedback" << endl;
cout << "3. View Next Feedback" << endl;
cout << "4. Back" << endl;
cout << "Please select an option: ";
int viewFeedbackOption = readInteger(1, 4);
switch (viewFeedbackOption)
{
case 1:
replyToFeedback(feedback);
displaySelectedFeedback(feedback);
break;
case 2:
if (feedback->prevFeedback != NULL) {
displaySelectedFeedback(feedback->prevFeedback);
break;
}
else {
cout << endl << "This is already the first feedback!" << endl;
system("pause");
displaySelectedFeedback(feedback);
break;
}
case 3:
if (feedback->nextFeedback != NULL) {
displaySelectedFeedback(feedback->nextFeedback);
break;
}
else {
cout << endl << "This is already the last feedback!" << endl;
system("pause");
displaySelectedFeedback(feedback);
break;
}
case 4:
displayAllFeedbacks();
default:
break;
}
}
void Admin::replyToFeedback(FeedbackNode* feedback)
{
string replyContent = "";
cin.ignore();
cout << "Please enter your reply: ";
getline(cin, replyContent);
time_t rawTime = time(nullptr);
struct tm* timeInfo = localtime(&rawTime);
feedbackList.addReply(replyContent, true, *timeInfo, feedback, false);
cout << "Your reply has been sent!" << endl;
system("pause");
}
void Admin::generateReport()
{
uniList.clearTempUniversityList();
UniversityNode* currentUniversity = tempUniversityList.getHead();
UniversityNode* insertUni;
FavouriteNode* currentFavourite;
bool alreadyExist = false;
int* favouriteUniIndex = new int[customerList.getSize() * 20];
int loopIndex = 0;
CustomerNode* currentCustomer = customerList.getHead();
if (currentCustomer == nullptr) {
cout << "No favourite university is saved by customers." << endl;
system("pause");
return;
}
// Summarize all the favourite universities by counting the number of favourites for each university
while (currentCustomer != nullptr) {
currentFavourite = currentCustomer->favourites;
while (currentFavourite != nullptr) {
loopIndex = 0;
alreadyExist = false;
currentUniversity = tempUniversityList.getHead();
while (currentUniversity != nullptr) {
if (currentFavourite->universityRank == currentUniversity->rank) {
favouriteUniIndex[loopIndex]++;
alreadyExist = true;
break;
}
currentUniversity = currentUniversity->nextUniversity;
loopIndex++;
}
if (!alreadyExist) {
insertUni = uniList.getUniversity(currentFavourite->universityRank);
tempUniversityList.insertEnd(to_string(insertUni->rank), to_string(insertUni->arScore), to_string(insertUni->erScore),
to_string(insertUni->fsrScore), to_string(insertUni->cpfScore), to_string(insertUni->ifrScore), to_string(insertUni->isrScore),
to_string(insertUni->irnScore), to_string(insertUni->gerScore), to_string(insertUni->scoreScaled), insertUni->institutionName,
insertUni->locationCode, insertUni->location, insertUni->arRank, insertUni->erRank, insertUni->fsrRank, insertUni->cpfRank, insertUni->ifrRank,
insertUni->isrRank, insertUni->irnRank, insertUni->gerRank);
favouriteUniIndex[loopIndex] = 1;
}
currentFavourite = currentFavourite->nextFavourite;
}
currentCustomer = currentCustomer->nextCustomer;
};
// Sort the summary by number of favourites in descending order
int temp;
bool swapResult = false;
UniversityNode* prev;
UniversityNode* current = tempUniversityList.getHead();
UniversityNode* next = current->nextUniversity;
for (int i = 0; i < tempUniversityList.getSize() - 1; i++) {
for (int j = 0; j < tempUniversityList.getSize() - 1; j++) {
if (favouriteUniIndex[j] < favouriteUniIndex[j + 1]) {
temp = favouriteUniIndex[j];
favouriteUniIndex[j] = favouriteUniIndex[j + 1];
favouriteUniIndex[j + 1] = temp;
current = tempUniversityList.getHead();
next = current->nextUniversity;
for (int k = 0; k < j; k++) {
prev = current;
current = next;
next = next->nextUniversity;
}
current->nextUniversity = next->nextUniversity;
next->nextUniversity = current;
if (j == 0) {
tempUniversityList.setHead(next);
}
else {
prev->nextUniversity = next;
}
swapResult = true;
}
}
if (!swapResult) {
break;
}
swapResult = false;
}
// Display the report
currentUniversity = tempUniversityList.getHead();
loopIndex = 0;
int maxNameLength = 18;
while (currentUniversity != nullptr) {
if (currentUniversity->institutionName.length() > maxNameLength) {
maxNameLength = currentUniversity->institutionName.length();
}
currentUniversity = currentUniversity->nextUniversity;
}
maxNameLength += 6;
currentUniversity = tempUniversityList.getHead();
system("cls");
cout << "Top 10 Favourite University Report" << endl << endl;
cout << string(maxNameLength + 50, '-') << endl;
cout << left << setw(6) << "Rank" << setw(maxNameLength) << "Institution Name" << setw(26)
<< "University Ranking" << setw(28) << "Number of Favourites" << endl;
cout << string(maxNameLength + 50, '-') << endl;
while (currentUniversity != nullptr) {
cout << left << setw(6) << loopIndex + 1 << setw(maxNameLength) << currentUniversity->institutionName
<< setw(26) << currentUniversity->rank << setw(28) << favouriteUniIndex[loopIndex] << endl;
loopIndex++;
currentUniversity = currentUniversity->nextUniversity;
}
cout << string(maxNameLength + 50, '-') << endl << endl;
system("pause");
uniList.clearTempUniversityList();
delete[] favouriteUniIndex;
}