10
10
#include <fcntl.h>
11
11
12
12
/* Length of variable*/
13
+ #define IP_ADDR "192.168.43.147"
13
14
#define PASSWORD_LEN 16
14
15
#define USERNAME_LEN 20
15
16
#define TRNAME_LEN 30
@@ -114,18 +115,23 @@ void display_booking(struct_booking booking){
114
115
printf ("\t%d\t\t%d\t\t%d\t%s\t %d\t\t %d\t %d\n" ,booking .book_no ,booking .acc_no ,booking .trn_no ,booking .name ,booking .age ,booking .seats ,booking .status );
115
116
}
116
117
117
- void show_all_booking (int conn_fd ){
118
+ int show_all_booking (int conn_fd ){
118
119
struct_booking response ;int moreflg ;
119
120
if (write (conn_fd , & CURRENT_USER .acc_no , sizeof (int ))== -1 ) ERR_EXIT ("read()" );
120
121
if (read (conn_fd , & moreflg , sizeof (int ))== -1 ) ERR_EXIT ("read()" );
121
122
moreflg = ntohl (moreflg );
123
+ if (moreflg == 0 ){
124
+ printf ("\nNO BOOKING HISTORY!!\n\n" );
125
+ return 0 ;
126
+ }
122
127
printf ("Booking No | Booking Account No | Train No | Traveller Name | Age | Seats Booked | Status\n" );
123
128
while (moreflg ){
124
129
if (read (conn_fd , & response , sizeof (struct_booking ))== -1 ) ERR_EXIT ("read()" );
125
130
display_booking (response );
126
131
if (read (conn_fd , & moreflg , sizeof (int ))== -1 ) ERR_EXIT ("read()" );
127
132
moreflg = ntohl (moreflg );
128
133
}
134
+ return 1 ;
129
135
}
130
136
131
137
void book_ticket (int conn_fd ){
@@ -160,6 +166,30 @@ void book_ticket(int conn_fd){
160
166
printf ("b) Requirement of your seats does not meet availability. Please check in the train list(shown above).\n\n" );
161
167
}
162
168
169
+ void cancel_booking (int conn_fd ){
170
+ show_all_booking (conn_fd );
171
+ int moreflg ;
172
+ if (read (conn_fd , & moreflg , sizeof (int ))== -1 ) ERR_EXIT ("read()" );
173
+ moreflg = ntohl (moreflg );
174
+ if (moreflg == 0 ){
175
+ return ;
176
+ }
177
+
178
+ int booking_no ;
179
+ printf ("Enter Booking No: " );scanf ("%d" ,& booking_no );
180
+ booking_no = htonl (booking_no );
181
+ int act_no = htonl (CURRENT_USER .acc_no );
182
+ if (write (conn_fd , & act_no , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
183
+ if (write (conn_fd , & booking_no , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
184
+
185
+ if (read (conn_fd , & moreflg , sizeof (int ))== -1 ) ERR_EXIT ("read()" );
186
+ moreflg = ntohl (moreflg );
187
+ if (moreflg == 1 )
188
+ printf ("Booking has been cancelled.\n" );
189
+ if (moreflg == 0 )
190
+ printf ("Booking was already cancelled.\n" );
191
+ }
192
+
163
193
int menu_user (int conn_fd ){
164
194
printf ("1. Book Ticket\n2. View Booking History\n3. Cancel Booking\n4. Exit\n\n" );
165
195
int choice ;int option ;
@@ -176,6 +206,9 @@ int menu_user(int conn_fd){
176
206
show_all_booking (conn_fd );
177
207
break ;
178
208
case 3 :
209
+ option = htonl (choice );
210
+ if (write (conn_fd , & option , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
211
+ cancel_booking (conn_fd );
179
212
break ;
180
213
case 4 :
181
214
option = htonl (choice );
@@ -273,6 +306,37 @@ void delete_user(int conn_fd){
273
306
printf ("No such record with this account no!!\n\n" );
274
307
}
275
308
309
+ void modify_user (int conn_fd ){
310
+ int accno ;struct_customer response ;int presentflg = 0 ;
311
+
312
+ printf ("Enter Account no: " );
313
+ scanf ("%d" ,& accno );
314
+ accno = htonl (accno );
315
+ if (write (conn_fd , & accno , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
316
+
317
+ if (read (conn_fd , & presentflg , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
318
+
319
+ presentflg = ntohl (presentflg );
320
+ if (presentflg ){
321
+ if (read (conn_fd , & response , sizeof (struct_customer ))== -1 ) ERR_EXIT ("read()" );
322
+ printf (" Account No | Account Type | Account Username | Account Password \t| Account Status \t\n" );
323
+ display_customer (response );
324
+
325
+ printf ("\nModifying Detals...\n" );
326
+ char updated_name [USERNAME_LEN ],updated_pwd [PASSWORD_LEN ];
327
+ printf ("Enter Updated Account Type(0 for ADMIN, 1 for NORMAL, 2 for AGENT): " );scanf ("%d" ,& response .type );
328
+ printf ("Enter Updated Account Name: " );scanf ("%s" ,updated_name );
329
+ strcpy (response .cust_username ,updated_name );
330
+ printf ("Enter Updated Account Password: " );scanf ("%s" ,updated_pwd );
331
+ strcpy (response .cust_password ,updated_pwd );
332
+ if (write (conn_fd , & response , 1 * sizeof (struct_customer )) == -1 ) ERR_EXIT ("write()" );
333
+ printf ("\nAccount has been updated\n" );
334
+ return ;
335
+ }
336
+
337
+ printf ("No such record with this account no!!\n\n" );
338
+ }
339
+
276
340
struct_train get_train (){
277
341
struct_train new ;char train_name [30 ];
278
342
printf ("Enter train name : " );
@@ -328,16 +392,50 @@ void delete_train(int conn_fd){
328
392
329
393
presentflg = ntohl (presentflg );
330
394
if (presentflg ){
331
- printf ("Account has been deleted.\n\n" );
395
+ printf ("Train Record has been deleted.\n\n" );
396
+ return ;
397
+ }
398
+
399
+ printf ("No such train record with this train no!!\n\n" );
400
+ }
401
+
402
+ void modify_train (int conn_fd ){
403
+ int trn_no ;struct_train response ;int presentflg = 0 ;
404
+ printf ("Enter Train no: " );
405
+ scanf ("%d" ,& trn_no );
406
+ trn_no = htonl (trn_no );
407
+ if (write (conn_fd , & trn_no , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
408
+
409
+ if (read (conn_fd , & presentflg , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
410
+
411
+ presentflg = ntohl (presentflg );
412
+ if (presentflg ){
413
+ if (read (conn_fd , & response , sizeof (struct_train ))== -1 ) ERR_EXIT ("read()" );
414
+ printf (" Train No | Train Name\t | Available Seats | Booked Seats | Status \t\n" );
415
+ display_train (response );
416
+
417
+ printf ("Modifying Train Details...\n" );
418
+ char up_trname [TRNAME_LEN ];int up_seats ;
419
+ printf ("Enter Updated Train Name: \n" );scanf (" %[^\n]" ,up_trname );
420
+ if (write (conn_fd , & up_trname , strlen (up_trname )+ 1 ) == -1 ) ERR_EXIT ("write()" );
421
+
422
+ do {
423
+ printf ("Enter number of seats to increase: " );scanf ("%d" ,& up_seats );
424
+ if (up_seats < 0 )
425
+ printf ("Seats to increase cannot be negative.\n" );
426
+ }while (up_seats < 0 );
427
+
428
+ if (write (conn_fd , & up_seats , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
429
+ printf ("Train has been updated.\n" );
332
430
return ;
333
431
}
334
432
335
433
printf ("No such train record with this train no!!\n\n" );
336
434
}
337
435
338
436
int menu_admin (int conn_fd ){
339
- printf ("1. Add User\n2. Search User \n3. Delete User\n4. View Users\n" );
340
- printf ("5 . Add Train\n6 . Search Train \n7 . Delete Train\n8. View Trains\n9 . Exit\n\n" );
437
+ printf ("1. Add User\n2. Search User \n3. Delete User\n4. Modify User\n5. View Users\n" );
438
+ printf ("6 . Add Train\n7 . Search Train \n8 . Delete Train\n9. Modify Train\n10. View Trains\n11 . Exit\n\n" );
341
439
int choice ;int option ;
342
440
scanf ("%d" ,& choice );
343
441
switch (choice ){
@@ -356,32 +454,42 @@ int menu_admin(int conn_fd){
356
454
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
357
455
delete_user (conn_fd );
358
456
break ;
359
- case 4 :
457
+ case 4 :
458
+ option = htonl (choice + 4 );
459
+ if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
460
+ modify_user (conn_fd );
461
+ break ;
462
+ case 5 :
360
463
option = htonl (choice + 4 );
361
464
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
362
465
show_all_customer (conn_fd );
363
466
break ;
364
- case 5 :
467
+ case 6 :
365
468
option = htonl (choice + 4 );
366
469
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
367
470
add_train (conn_fd );
368
471
break ;
369
- case 6 :
472
+ case 7 :
370
473
option = htonl (choice + 4 );
371
474
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
372
475
search_train (conn_fd );
373
476
break ;
374
- case 7 :
477
+ case 8 :
375
478
option = htonl (choice + 4 );
376
479
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
377
480
delete_train (conn_fd );
378
481
break ;
379
- case 8 :
482
+ case 9 :
483
+ option = htonl (choice + 4 );
484
+ if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
485
+ modify_train (conn_fd );
486
+ break ;
487
+ case 10 :
380
488
option = htonl (choice + 4 );
381
489
if (write (conn_fd , & option , 1 * sizeof (int )) == -1 ) ERR_EXIT ("write()" );
382
490
show_all_train (conn_fd );
383
491
break ;
384
- case 9 :
492
+ case 11 :
385
493
option = htonl (choice + 4 );
386
494
if (write (conn_fd , & option , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
387
495
close (conn_fd );
@@ -411,17 +519,16 @@ int main(int argc,char* argv[])
411
519
server_addr .sin_family = AF_INET ;
412
520
server_addr .sin_port = htons (PORT_NO );
413
521
414
- if (inet_pton (AF_INET , "0.0.0.0" , & server_addr .sin_addr ) <= 0 )
522
+ if (inet_pton (AF_INET , IP_ADDR , & server_addr .sin_addr ) <= 0 )
415
523
ERR_EXIT ("inet_pton()" );
416
524
if (connect (socket_fd , (struct sockaddr * )& server_addr , sizeof (server_addr )) == -1 )
417
525
ERR_EXIT ("connect()" );
418
- printf ( "conn_fd: %d\n" , socket_fd );
526
+
419
527
response_flg = login (socket_fd );
420
528
if (response_flg == 0 ){
421
529
if (write (socket_fd , & choice , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
422
530
}
423
531
424
- printf ("Inside main %d" ,response_flg );
425
532
while (response_flg ){
426
533
system ("clear" );
427
534
0 commit comments