-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainMenu.cpp
379 lines (347 loc) · 8.42 KB
/
mainMenu.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
#include "unp.h"
using namespace std;
// Function Headers
void studentLogin(int);
void studentSignup(int);
void checkGrades(int);
void registerClasses(int);
void payBills(int);
void vieworDropClasses(int);
string username, password, fullname; // store user detail on local
void mainMenu(FILE *fp, int sockfd)
{
char sendline[MAXLINE], recvline[MAXLINE]; // buffers for sending and receiving messages
char choice[5];
// Ask if user has an account
fputs("Do you have an account?\n1. Yes\n2. No\n", stdout);
fgets(choice, sizeof(choice), fp);
choice[strlen(choice) - 1] = '\0';
int option = atoi(choice);
if (option == 2) // if no account then sign up
{
studentSignup(sockfd);
}
else // otherwise user can login
{
fputs("\nYou can login\n", stdout);
}
studentLogin(sockfd);
// Show menu after successfully login
fputs("What would you like to do?\n1. Register for classes\n2. Check my grades\n3. Pay my bill\n4. View or Drop Classes\n5. Exit\n", stdout);
while (fgets(choice, sizeof(choice), fp) != NULL)
{
choice[strlen(choice) - 1] = '\0';
option = atoi(choice);
switch (option)
{
case 1:
registerClasses(sockfd);
break;
case 2:
checkGrades(sockfd);
break;
case 3:
payBills(sockfd);
break;
case 4:
vieworDropClasses(sockfd);
break;
case 5:
exit(0);
default:
cout << "Invalid option.\n";
break;
}
fputs("What would you like to do?\n1. Register for classes\n2. Check my grades\n3. Pay my bill\n4. View or Drop Classes\n5. Exit\n", stdout);
}
}
// Handle Signup
void studentSignup(int sockfd)
{
fputs("Signup\n", stdout);
bool signedUp = false;
do
{
// Ask user to provide their detail for creating account
char res[MAXLINE];
string user_name, pass_word, full_name;
cout << "Enter username: ";
cin >> user_name;
cout << "Enter password: ";
cin >> pass_word;
cout << "Enter your full name: ";
cin.ignore();
getline(cin, full_name);
// Send detail to server
string req = "SIGNUP|student|" + user_name + "|" + pass_word + "|" + full_name;
const char *sendReq = req.c_str();
write(sockfd, sendReq, strlen(sendReq));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
string resString(res);
vector<string> parts = split(resString, "|");
string signup = parts[0];
string msg = parts[1];
if (signup == "SUCCESS")
{
signedUp = true;
cout << msg << endl;
}
else
{
cout << msg << endl;
continue;
}
} while (!signedUp);
}
// Handle login
void studentLogin(int sockfd)
{
bool signedIn = false;
do
{
// Get detail for login
char res[MAXLINE];
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
// Send account detail to server
string req = "LOGIN|student|" + username + "|" + password;
cin.ignore();
const char *sendReq = req.c_str();
write(sockfd, sendReq, strlen(sendReq));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
string resString(res);
vector<string> parts = split(resString, "|");
string signin = parts[0];
string msg = parts[2]; // store msg from server
fullname = parts[1]; // store user full_name after successfully login
if (signin == "SUCCESS")
{
signedIn = true;
cout << "\n" << msg << endl;
}
else
{
cout << msg << endl;
continue;
}
} while (!signedIn);
}
// Handle Check Grade
void checkGrades(int sockfd)
{
// Send request to server
const char *req;
char res[MAXLINE];
string reqString = "GRADES|" + username;
req = reqString.c_str();
write(sockfd, req, strlen(req));
// Get response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
string resString(res);
resString = *(new string(res));
vector<string> parts = split(resString, "|");
string signup = parts[0];
string msg = parts[1];
if (signup == "SUCCESS")
{
cout << msg << endl;
}
else
{
cout << msg << endl;
}
}
// Handle Register
void registerClasses(int sockfd)
{
bool hasRegistered = false;
do
{
// Send request to server
const char *req;
char res[MAXLINE];
char choices[21];
string reqString = "CLASSES|" + username;
req = reqString.c_str();
write(sockfd, req, strlen(req));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
// show available classes
string resString(res);
vector<string> classes = split(resString, "|");
fputs("Available classes: \n", stdout);
for (int i = 0; i < classes.size(); i++)
{
cout << i + 1 << ". " << classes[i] << "\n";
}
cout << "\nEnter the number (1-10) and a space between each class you want to register for: ";
// ask what classes does user want and send that request
fgets(choices, sizeof(choices), stdin);
write(sockfd, choices, strlen(choices));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
resString = *(new string(res));
vector<string> parts = split(resString, "|");
string signup = parts[0];
string msg = parts[1];
if (signup == "SUCCESS")
{
hasRegistered = true;
cout << msg << endl;
}
else
{
cout << msg << endl;
continue;
}
} while (!hasRegistered);
}
// Handle pay bill
void payBills(int sockfd)
{
bool hasPay = false;
do
{
// Send request to server
const char *req;
char res[MAXLINE];
string reqString = "BILL|" + username;
req = reqString.c_str();
write(sockfd, req, strlen(req));
// Read response from server
memset(res,0,MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
string resString(res);
vector<string> parts = split(resString, "|");
if (parts[0] == "FAILURE") // error check
{
cout << parts[1];
return;
}
// print the amount send by the server
cout << resString;
// take input for how much user want to pay
cin >> reqString;
req = reqString.c_str();
write(sockfd, req, strlen(req));
cin.ignore();
// check if pay is success
memset(res, 0, MAXLINE); // clear buffer
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
resString = res;
parts = split(resString, "|");
if (parts[0] == "FAILURE")
{
cout << parts[1] << endl;
return;
}
else
{
hasPay = true;
cout << parts[1] << endl;
}
} while (!hasPay);
}
// Handle view or drop classes
void vieworDropClasses(int sockfd)
{
bool hasViewed = false;
do
{
// Send request to server
const char *req;
char res[MAXLINE];
char choices[21];
string reqString = "VIEW|" + username;
req = reqString.c_str();
write(sockfd, req, strlen(req));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
// Show classes that the user currently take
string resString(res);
vector<string> classes = split(resString, "|");
fputs("Available classes: \n", stdout);
for (int i = 1; i < classes.size(); i++)
{
cout << i << ". " << classes[i] << "\n";
}
if (classes.size() <= 1)
{
reqString = "0";
req = reqString.c_str();
write(sockfd, req, strlen(req));
read(sockfd, res, MAXLINE);
return;
}
// Ask if user want to drop any classes
cout << "\nEnter the number of all classes you want to drop or 0 if you don't want to drop any:";
fgets(choices, sizeof(choices), stdin);
choices[strlen(choices) - 1] = '\0';
hasViewed = true;
// Send request to server
write(sockfd, choices, strlen(choices));
// Read response from server
memset(res, 0, MAXLINE);
if (read(sockfd, res, MAXLINE) == 0)
{ // read from socket
printf("str_cli: server terminated prematurely");
exit(1);
}
resString = *(new string(res));
vector<string> parts = split(resString, "|");
string signup = parts[0];
string msg = parts[1];
if (signup == "SUCCESS")
{
hasViewed = true;
cout << msg << endl;
}
else
{
cout << msg << endl;
continue;
}
} while (!hasViewed);
}