-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientUtils.h
577 lines (487 loc) · 15.4 KB
/
clientUtils.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include"struct.h"
#define PORT 8082
#define LEN 50
//-------------------------FUNCTION DECLARATIONS---------------------
int client_Entry(int scktDesc);
int signIN(int scktDesc);
int signUP(int scktDesc);
int MainMenu(int scktDesc,int acntType);
int user_function(int scktDesc,int choice);
int addTrain(int);
int viewTrain(int);
int updateTrain(int);
int deleteTrain(int);
int Opertn_train(int scktDesc,int choice);
int addUser(int);
int viewUser(int);
int updateUser(int);
int deleteUser(int);
int Opertn_user(int scktDesc,int choice);
//------------------------------------------------------------Entry point-------------------------------------------------------
int client_Entry(int scktDesc){
int choice,valid;
printf("-------------------------WELCOME--------------------------\n");
printf("\n---------------------TRAIN RESERVATION SYSTEM--------------------------\n");
printf("Choose from below:\n");
printf("1. SIGN IN\n");
printf("2. SIGN UP\n");
printf("3. EXIT\n");
printf("Enter Your Choice: ");
scanf("%d", &choice);
//write choice into socket
write(scktDesc, &choice, sizeof(choice));
int ret;
switch(choice)
{
case 1: ret=signIN(scktDesc); //------SIGN IN--------//
return ret;
case 2: ret=signUP(scktDesc); //-------SIGN UP--------//
return ret;
case 3: return 3; //-------LOGOUT---------//
default: printf("Enter correct option\n");
ret=client_Entry(scktDesc);
return ret;
}
}
//---------------------------------------------------------------SIGN IN-----------------------------------------------------------------//
int signIN(int scktDesc){
int login_id,acnt_type,valid;
char password[LEN];
printf("--------------------------SIGN IN---------------------\n");
printf("Enter login id: ");
scanf("%d", &login_id);
strcpy(password,getpass("Enter password: "));
//write login id and password onto socket
write(scktDesc, &login_id, sizeof(login_id));
write(scktDesc, &password, sizeof(password));
//read whether given credentials are valid
read(scktDesc, &valid, sizeof(valid));
if(valid){
printf("\n-----------------Login successfull------------\n");
read(scktDesc,&acnt_type,sizeof(acnt_type));
while(MainMenu(scktDesc,acnt_type)!=-1);
return 1;
}
else{
printf("\n------------Login Failed-------------\n");
printf("Enter correct user id and password\n");
return 1;
}
}
//--------------------------------------------------------------SIGN UP------------------------------------------------------------------//
int signUP(int scktDesc)
{
int acnt_type,login_id,valid;
char name[LEN],password[LEN];
system("clear");
printf("-------------------------SIGN UP---------------------\n");
printf("Choose Account type from below\n");
printf("0. Administrator\n1. Agent Account\n2. Customer Account\n");
//Enter credentials
printf("Enter Type Of your Account: \n");
scanf("%d", &acnt_type);
printf("Enter Your Name: ");
scanf("%s", name);
strcpy(password,getpass("Enter Password: "));
//write credentials onto socket
write(scktDesc, &acnt_type, sizeof(acnt_type));
write(scktDesc, &name, sizeof(name));
write(scktDesc, &password, strlen(password));
//read login id from socket
read(scktDesc, &login_id, sizeof(login_id));
printf("\nSign Up successful\n Your login ID is : %d\n", login_id);
return 2;
}
//------------------------------------------------------------------- Main menu function-----------------------------------------------------------//
int MenuAdmin()
{
int choice;
printf("Choose from below\n");
printf("\n1.Operations on train\n");
printf("2. Operations on user\n");
printf("3. Logout\n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
return choice;
}
int MenuCust()
{
int choice;
printf("\n\n------OPTIONS--------\n");
printf("1. Book Ticket\n");
printf("2. View Bookings\n");
printf("3. Update Booking\n");
printf("4. Cancel booking\n");
printf("5. Logout\n");
printf("\nEnter Your Choice: ");
scanf("%d",&choice);
return choice;
}
int details(int i){
char *str;
int choice;
if(i==1) str="train";
else str="user";
printf("1. Add %s\n",str);
printf("2. View %s\n",str);
printf("3. Modify %s\n",str);
printf("4. Delete %s\n",str);
printf("Enter Your Choice: ");
scanf("%d",&choice);
return choice;
}
int MainMenu(int scktDesc,int type){
int choice;
if(type==1 || type==2){
choice=MenuCust();
write(scktDesc,&choice,sizeof(choice));
return user_function(scktDesc,choice);
}
else if(type==0){ // Admin
choice=MenuAdmin();
write(scktDesc,&choice,sizeof(choice));
if(choice==1)
{
choice=details(choice);
write(scktDesc,&choice,sizeof(choice));
return Opertn_train(scktDesc,choice);
}
else if(choice==2)
{
choice=details(choice);
write(scktDesc,&choice,sizeof(choice));
return Opertn_user(scktDesc,choice);
}
else if(choice==3)
return -1;
}
}
//--------------------------------------------------------------------- Operations on train---------------------------------------------------------------//
//-------------------------------------------------------------------------ADD TRAIN---------------------------------------------------
int addTrain(int scktDesc)
{
char tname[LEN];
int valid;
printf("\nEnter train name: ");
scanf("%s",tname);
write(scktDesc, &tname, sizeof(tname));
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\nTrain added successfully\n");
return valid;
}
//-----------------------------------------------------------------------VIEW TRAIN-------------------------------------------------------
int viewTrain(int scktDesc)
{
int no_of_trains;
int tno;
char tname[LEN];
int tot_seats;
int avail_seats;
int valid=0;
read(scktDesc,&no_of_trains,sizeof(no_of_trains));
printf("\tTrain no\tTrain name\tTotal seats\tAvail seats\n");
while(no_of_trains--){
read(scktDesc,&tno,sizeof(tno));
read(scktDesc,&tname,sizeof(tname));
read(scktDesc,&tot_seats,sizeof(tot_seats));
read(scktDesc,&avail_seats,sizeof(avail_seats));
if(strcmp(tname, "deleted")!=0)
printf("\t%d\t\t%s\t\t%d\t\t%d\n",tno,tname,tot_seats,avail_seats);
}
return valid;
}
//--------------------------------------------------------------------UPDATE TRAIN---------------------------------------------------
int updateTrain(int scktDesc)
{
int tot_seats,choice=2,valid=0,tid;
char tname[LEN];
write(scktDesc,&choice,sizeof(int));
Opertn_train(scktDesc,choice);
printf("\n Enter the train number you want to modify: ");
scanf("%d",&tid);
write(scktDesc,&tid,sizeof(tid));
printf("Choose below to update\n");
printf("\n1. Train Name\n\t2. Total Seats\n");
printf(" Your Choice: ");
scanf("%d",&choice);
write(scktDesc,&choice,sizeof(choice));
if(choice==1){
read(scktDesc,&tname,sizeof(tname));
printf("\n Current name: %s",tname);
printf("\n Updated name:");
scanf("%s",tname);
write(scktDesc,&tname,sizeof(tname));
}
else if(choice==2){
read(scktDesc,&tot_seats,sizeof(tot_seats));
printf("\n Current value: %d",tot_seats);
printf("\n Updated value:");
scanf("%d",&tot_seats);
write(scktDesc,&tot_seats,sizeof(tot_seats));
}
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\n Train data updated successfully\n");
else
printf("Error in updating\n");
return valid;
}
//--------------------------------------------------------------------------DELETE TRAIN------------------------------------------------
int deleteTrain(int scktDesc)
{
int choice=2,tid,valid=0;
write(scktDesc,&choice,sizeof(int));
Opertn_train(scktDesc,choice);
printf("\n Enter the train number you want to delete: ");
scanf("%d",&tid);
write(scktDesc,&tid,sizeof(tid));
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\nTrain deleted successfully\n");
else
printf("Train with given id not present\n");
return valid;
}
int Opertn_train(int scktDesc,int choice){
int valid = 0;
switch(choice)
{
//----------------------------ADD TRAIN---------------------------
case 1: valid=addTrain(scktDesc);
return valid;
//----------------------------VIEW TRAIN-------------------------
case 2: valid=viewTrain(scktDesc);
return valid;
//--------------------------UPDATE TRAIN--------------------------
case 3: valid=updateTrain(scktDesc);
return valid;
//--------------------------DELETE TRAIN---------------------------
case 4: valid=deleteTrain(scktDesc);
return valid;
}
}
//----------------------------------------------------------------------- Operations on user--------------------------------------------------------//
//--------------------------------------------------------------------------ADD USER------------------------------------------
int addUser(int scktDesc)
{
int acntType,login_id,valid=0;
char name[LEN],password[LEN];
printf("\nChoose account type from below \n");
printf("1. Agent\n2. Customer\n");
printf("\nEnter The Type Of Account: \n");
scanf("%d", &acntType);
printf("Enter User Name: ");
scanf("%s", name);
strcpy(password,getpass("Enter Password: "));
write(scktDesc, &acntType, sizeof(acntType));
write(scktDesc, &name, sizeof(name));
write(scktDesc, &password, strlen(password));
read(scktDesc,&valid,sizeof(valid));
if(valid){
read(scktDesc,&login_id,sizeof(login_id));
printf("Your login id is: %d\n", login_id);
}
return valid;
}
//----------------------------------------------------------------------VIEW USER------------------------------------------------
int viewUser(int scktDesc)
{
int no_of_users,valid=0,login_id,acntType;
char uname[LEN];
read(scktDesc,&no_of_users,sizeof(no_of_users));
printf("\tUser id\tUser name\tAccount type\n");
while(no_of_users--){
read(scktDesc,&login_id,sizeof(login_id));
read(scktDesc,&uname,sizeof(uname));
read(scktDesc,&acntType,sizeof(acntType));
if(strcmp(uname, "deleted")!=0 && acntType!=0)
printf("\t%d\t\t%s\t\t%d\n",login_id,uname,acntType);
}
return valid;
}
//---------------------------------------------------------------------UPDATE USER-------------------------------------------------
int updateUser(int scktDesc)
{
int choice=2,valid=0,uid;
char name[LEN],password[LEN];
write(scktDesc,&choice,sizeof(int));
Opertn_user(scktDesc,choice);
printf("\n Enter the User id you want to modify: ");
scanf("%d",&uid);
write(scktDesc,&uid,sizeof(uid));
printf("\n1. User Name\n2. Password\n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
write(scktDesc,&choice,sizeof(choice));
if(choice==1){
read(scktDesc,&name,sizeof(name));
printf("\n Current name: %s",name);
printf("\n Updated name:");
scanf("%s",name);
write(scktDesc,&name,sizeof(name));
read(scktDesc,&valid,sizeof(valid));
}
else if(choice==2){
printf("\n Enter Current password: ");
scanf("%s",password);
write(scktDesc,&password,sizeof(password));
read(scktDesc,&valid,sizeof(valid));
if(valid){
printf("\n Enter new password:");
scanf("%s",password);
}
else
printf("\nIncorrect password\n");
write(scktDesc,&password,sizeof(password));
}
if(valid){
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\n User data updated successfully\n");
}
return valid;
}
//-----------------------------------------------------------------DELETE USER--------------------------------------
int deleteUser(int scktDesc)
{
int choice=2,uid,valid=0;
write(scktDesc,&choice,sizeof(int));
Opertn_user(scktDesc,choice);
printf("\n Enter the id you want to delete: ");
scanf("%d",&uid);
write(scktDesc,&uid,sizeof(uid));
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\n User deleted successfully\n");
else
printf("\nUser with given id not present\n");
return valid;
}
int Opertn_user(int scktDesc,int choice){
int valid = 0;
switch(choice)
{
//------------------ADD USER------------
case 1: valid=addUser(scktDesc);
return valid;
//------------------VIEW USER-----------
case 2: valid=viewUser(scktDesc);
return valid;
//------------------UPDATE USER----------
case 3: valid=updateUser(scktDesc);
return valid;
//-----------------DELETE USER----------
case 4: valid=deleteUser(scktDesc);
return valid;
}
}
//-------------------------------------------------------------------------- User function to book tickets ----------------------------------------------------//
//----------------------------------------------------------------------------BOOK TICKET----------------------------------------------------
int bookTickets(int scktDesc)
{
int view=2,tid,seats;
int valid=0;
write(scktDesc,&view,sizeof(int));
Opertn_train(scktDesc,view);
printf("\nEnter the train number you want to book: ");
scanf("%d",&tid);
write(scktDesc,&tid,sizeof(tid));
printf("\nEnter the no. of seats you want to book: ");
scanf("%d",&seats);
write(scktDesc,&seats,sizeof(seats));
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\nTicket booked successfully.\n");
else
printf("\nSeats were not available.\n");
return valid;
}
//----------------------------------------------------------------------------VIEW BOOKINGS---------------------------------------------------------------
int viewBookings(int scktDesc)
{
int no_of_bookings;
int id,tid,seats;
int valid=0;
read(scktDesc,&no_of_bookings,sizeof(no_of_bookings));
printf("\n\tBooking id\tTicket no\tSeats\n");
while(no_of_bookings--){
read(scktDesc,&id,sizeof(id));
read(scktDesc,&tid,sizeof(tid));
read(scktDesc,&seats,sizeof(seats));
if(seats!=0)
printf("\t%d\t\t%d\t\t%d\n",id,tid,seats);
}
return valid;
}
//--------------------------------------------------------------------------------------UPDATE BOOKING------------------------------------------------------------
int updateBooking(int scktDesc)
{
int choice = 2,bid,val,valid;
user_function(scktDesc,choice);
printf("\n Enter the Booking id you want to modify: ");
scanf("%d",&bid);
write(scktDesc,&bid,sizeof(bid));
printf("Choose from below\n");
printf("\n1. Increase number of seats\n2. Decrease number of seats\n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
write(scktDesc,&choice,sizeof(choice));
if(choice==1){
printf("Enter number of tickets to increase");
scanf("%d",&val);
write(scktDesc,&val,sizeof(val));
}
else if(choice==2){
printf("\nEnter number of tickets to decrease");
scanf("%d",&val);
write(scktDesc,&val,sizeof(val));
}
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\nBooking updated successfully.\n");
else
printf("\nUpdation failed. No more seats available.\n");
return valid;
}
//-----------------------------------------------------------------------------CANCEL BOOKING--------------------------------------
int cancelBooking(int scktDesc)
{
int choice = 2,bid,valid;
user_function(scktDesc,choice);
printf("\n Enter the Booking id you want to cancel: ");
scanf("%d",&bid);
write(scktDesc,&bid,sizeof(bid));
read(scktDesc,&valid,sizeof(valid));
if(valid)
printf("\nBooking cancelled successfully.\n");
else
printf("\nCancellation failed.\n");
return valid;
}
int user_function(int scktDesc,int choice){
int valid =0;
switch(choice)
{
//-------------BOOK TICKET-----------------
case 1: valid=bookTickets(scktDesc);
return valid;
//-------------VIEW BOOKINGS--------------
case 2: valid=viewBookings(scktDesc);
return valid;
//-------------UPDATE BOOKING-------------
case 3: valid=updateBooking(scktDesc);
return valid;
//-------------CANCEL BOOKING------------
case 4: valid=cancelBooking(scktDesc);
return valid;
//-------------LOGOUT------------------
case 5: return -1;
}
}