-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.c
More file actions
2722 lines (2362 loc) · 108 KB
/
Copy path1.c
File metadata and controls
2722 lines (2362 loc) · 108 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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Group ID= 07
Members:
1)Arunabh Aditya Roll Number: 114026 PRN:1032230178
2)M Aryane Roll Number: 114029 PRN:1032232583
3)Akshay Anurag Roll Number: 114028 PRN:1032231965
4)Fardeen Ali Roll Number: 114027 PRN:1032232038
5)Ayush Gaikwad Roll Number: 114039 PRN:1032232473
6)Shriharsh Deshmukh Roll Number: 114038 PRN:1032231397
Problem Statement:
write a C program to reserve train tickets with following functionality:
1)User System:
i)Login
ii)signup
iii)Reset Password
2)Admin Controls
i)add new trains
ii)add delayed trains
iii)see delayed trains
3)Tickets reservation:
i)With option to select preferred class, compartment,seat type (seater or sleeper) and number of seats
ii)With payment system.(with payment failure handling case).
iii)Once reserved, get PNR.
4)Get Information of reservations (By entering PNR):
i)To know in which compartment, class seat is reserved.
ii)To know if ticket is confirmed or in waiting list.
iii)Get reservation id or referrence number of the reservation.
iv)To know all about train (max speed, total dist to be travelled )
V)Option to print tickets in PDF format
5)Checking Train Status:
i)To check delayed trains
6)Option to cancel tickets:
i)option to cancel ticket.
ii)with proper refund messages.
***ADDITIONAL FEATURES***
1)QR code payment format
2)User profile update
3)Admin panel
4)ability to cancel tickets or go back to main menu at any step
5)Proper error handling.
6)proper user navigation allowing a smoother program experience.
7)Proper PDF generation.
8)Proper user interface allowing user to perform all actions without having to restart the program.
9)Menu Based program.
*****CAUTIONS*****
1)Input should be of the type that is asked.
2)All input should be in lowercase (unless stated otherwise and as per choice for name and other user details).
Input Required:
as per the program flow and user wishes.
Algorithms used (module use):
1)External library:
i)qrcodegen.h
ii)pdfgen.h
2)Internal header files:
i)stdio.h
ii)stdlib.h
iii)conio.h
iv)string.h
v)time.h
vi)windows.h
vii)ctype.h
Conclusion:
Thus implemented a complete error safe train ticket booking application with TUI (terminal user interface) allowing a menu based program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <ctype.h>
#include "header/qrcodegen.h"
#include "header/pdfgen.h"
#define MAX_PASSENGERS 100
typedef struct {
char userId[10];
char email[100];
char password[100];
char name[100];
int age;
char gender[10];
long long phone;
} User;
typedef struct {
char trainId[10];
char startingPoint[100];
char destination[100];
char departureTime[100];
int cost;
int compartments;
char seatType[100];
char name[100];
int maxSpeed;
int totalDist;
} Train;
typedef struct {
long long pnr;
char userId[10];
char name[100];
char trainId[10];
int compartment;
char seatType[10];
char status[10];
float cost;
int seats;
int noOfSeats;
char date[20];
int age;
char gender[10];
long long phone;
} Reservation;
typedef struct {
char trainId[20];
char startingPoint[20];
char destination[20];
char departureTime[15];
int cost;
int compartments;
char seatType[20];
int isDelayed;
int delayTime;
} DelayedTrain;
typedef struct PassengerTicket PassengerTicket;
typedef struct {
char StartingPoint[20];
char Destination[20];
char PNR[20];
char TrainName[20];
char TrainId[20];
char class[20];
char Date[20];
char dist[20];
char Cost[20];
char Compartment[20];
int totalSeats;
PassengerTicket* passengerTickets[10];
} Ticket;
struct PassengerTicket {
char passName[20];
char age[20];
char gender[20];
char seatNum[20];
};
int delayMilliseconds = 5000;
int logged = 0;
int *loggedPtr = &logged;
char userId[10];
char *userIdPtr = userId;
void sleepProgram(float seconds);
void PrintSleep(float seconds);
void clearTerminal();
void greenColor();
void redColor();
void resetColor();
void yellowColor();
void signup();
int userExists(const char* email);
void login();
void copyFile(const char *source, const char *destination);
void resetPass();
void LogOrSign();
void adminLogin();
void addTrain();
void displayAllTrains();
int compareByCostAsc(const void* a, const void* b);
int compareByCostDesc(const void* a, const void* b);
void sortByCost(int sortOrder);
void findTrainsByDestination(const char* destination);
void findTrainsByStartingPoint(const char* startingPoint);
void adminControls();
void addDelayedTrain();
void displayDelayedTrains(int isAdmin);
void trainListandBook();
void findTrain(const char* trainId);
void showCompartment( int compartment, const char* trainId);
void randomlyBookSeats(int* seats, int numSeats);
int isValidDate(const char *dateStr);
void payNow(const char* trainId, int ticketsToBuy, int choosenCompartment, int ticketsNums[MAX_PASSENGERS], const char* date);
void showTickets(int choosenCompartment, const char* trainId);
int isAllDigits(const char *str);
void paymentGateway(const char* trainId, int ticketsToBuy, int choosenCompartment, float totalPrice, int ticketsNums[MAX_PASSENGERS], const char* date);
long long generate10DigitRandomNumber();
long long generate15DigitRandomNumber();
void writeReservationToFile(const Reservation* passenger);
void confirmTickets(const char* trainId, int ticketsToBuy, int choosenCompartment, int* ticketsNums, float totalPrice, const char* date);
void PrintToBeDeletedReservation(long long pnr);
void deleteReservationByPnr(long long pnr);
void cancelBooking();
void seeReservationInfo(long long pnrInfo);
void reservationInfo();
void getTicketFormat();
void getTicketFormatAfterBooking(long long pnrInfo);
int writeTicketPDF(const Ticket* ticket);
void checkAllReservations();
void showUserInfo();
void changeUserInfo();
void mainMenu();
void sleepProgram(float seconds)
{
Sleep(seconds * 1000);
}
void PrintSleep(float seconds){
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("========");
sleepProgram(seconds);
printf("======\n");
}
void clearTerminal(){
system("cls");
}
void greenColor()
{
printf("\033[1;32m");
}
void redColor()
{
printf("\033[1;31m");
}
void resetColor()
{
printf("\033[0m");
}
void yellowColor() {
printf("\033[1;33m");
}
void signup() {
clearTerminal();
int SignedUp, AlrExists;
FILE *file = fopen("files/users.txt", "a");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
User newUser;
printf("Please enter your name: ");
scanf("%s", newUser.name);
printf("Please enter your Age: ");
scanf("%d", &newUser.age);
printf("Please enter your Gender: ");
scanf("%s", newUser.gender);
printf("Please enter your Phone: ");
scanf("%lld", &newUser.phone);
printf("Please enter your email: ");
scanf("%s", newUser.email);
if (userExists(newUser.email)) {
printf("\nUser with the same email already exists. Press 1 to log in. Press 2 to try again\n");
scanf("%d", &AlrExists);
if (AlrExists == 1) {
login();
} else if (AlrExists == 2) {
signup();
}
return;
}
printf("Please enter your password: ");
scanf("%s", newUser.password);
sprintf(newUser.userId, "%08d", rand() % 100000000);
fprintf(file, "%s %s %s %s %d %s %lld\n", newUser.userId, newUser.email, newUser.password, newUser.name, newUser.age, newUser.gender, newUser.phone);
printf("\nNew user created successfully!\n");
printf("\nHere are user Details:\n");
printf("User ID: %s\n", newUser.userId);
printf("Email: %s\n", newUser.email);
printf("Name: %s\n", newUser.name);
printf("Age: %d\n", newUser.age);
printf("Gender: %s\n", newUser.gender);
printf("Phone Number: +91 %lld\n", newUser.phone);
fclose(file);
printf("\nAccount created successfully! Please log in.\n");
printf("Press 1 to login or press 2 to create another account: ");
scanf("%d", &SignedUp);
if (SignedUp == 1) {
login();
} else if (SignedUp == 2) {
signup();
}
}
int userExists(const char* email) {
FILE *file = fopen("files/users.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
char line[500];
while (fgets(line, sizeof(line), file) != NULL) {
User user;
sscanf(line, "%s %s %s %s %d %s %lld", user.userId, user.email, user.password, user.name, &user.age, user.gender, &user.phone);
if (strcmp(user.email, email) == 0) {
fclose(file);
return 1;
}
}
fclose(file);
return 0;
}
void login() {
int wrongPass, wrongUser;
char email[100];
char password[100];
char line[500];
clearTerminal();
printf("\n\n\n\n\n==============================================================================================\n\n");
printf("\t\t\t\033[1;31mPlease Login:\033[0m\t\t");
printf("\n\n==============================================================================================\n");
printf("\nPlease enter your email:");
scanf("%s", email);
getchar();
FILE *file = fopen("files/users.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int userFound = 0;
while (fgets(line, sizeof(line), file) != NULL) {
User user;
sscanf(line, "%s %s %s %s %d %s %lld", user.userId, user.email, user.password, user.name, &user.age, user.gender, &user.phone);
if (strcmp(user.email, email) == 0) {
userFound = 1;
printf("\nUser Found\n");
printf("Please enter your password: ");
scanf("%s", password);
getchar();
if (strcmp(user.password, password) == 0) {
printf("\nLogin successful!\n");
*loggedPtr = 1;
strcpy(userIdPtr, user.userId);
yellowColor();
printf("\n\t\t\tYou are logged in and can proceed to book tickets!!");
resetColor();
sleepProgram(3);
mainMenu();
break;
} else {
printf("\nIncorrect password. Please try again.\n");
printf("\nPress 1 to try again or press 2 to reset password!\n");
scanf("%d", &wrongPass);
if (wrongPass == 1) {
login();
} else if (wrongPass == 2) {
resetPass();
}
}
break;
}
}
fclose(file);
if (!userFound) {
printf("\nUser not found. Please create an account.\n");
printf("\nPress 1 to create an account or press 2 to try again!\n");
scanf("%d", &wrongUser);
if (wrongUser == 1) {
signup();
} else if (wrongUser == 2) {
login();
}
}
}
void copyFile(const char *source, const char *destination) {
FILE *src = fopen(source, "rb");
FILE *dest = fopen(destination, "wb");
if (src == NULL || dest == NULL) {
printf("Error opening files for copying.\n");
return;
}
char ch;
while ((ch = fgetc(src)) != EOF) {
fputc(ch, dest);
}
fclose(src);
fclose(dest);
}
void resetPass() {
int resetNoUser, resetDoneUser;
char email[100];
char newPassword[100];
char line[500];
clearTerminal();
printf("\nPlease enter your email: ");
scanf("%s", email);
getchar();
FILE *file = fopen("files/users.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
FILE *tempFile = fopen("files/temp.txt", "w");
if (tempFile == NULL) {
printf("Error creating temporary file.\n");
fclose(file);
exit(1);
}
int userFound = 0;
while (fgets(line, sizeof(line), file) != NULL) {
User user;
sscanf(line, "%s %s %s %s %d %s %lld", user.userId, user.email, user.password, user.name, &user.age, user.gender, &user.phone);
if (strcmp(user.email, email) == 0) {
userFound = 1;
printf("\nUser Found\n");
printf("Please enter your new password: ");
scanf("%s", newPassword);
getchar();
strcpy(user.password, newPassword);
}
fprintf(tempFile, "%s %s %s %s %d %s %lld\n", user.userId, user.email, user.password, user.name, user.age, user.gender, user.phone);
}
fclose(file);
fclose(tempFile);
remove("files/users.txt");
fclose(fopen("files/users.txt", "w"));
copyFile("files/temp.txt", "files/users.txt");
remove("files/temp.txt");
if (!userFound) {
printf("\nUser not found. Please create an account.\n");
printf("\nPress 1 to create an account or press 2 to try again!\n");
scanf("%d", &resetNoUser);
if (resetNoUser == 1) {
signup();
} else if (resetNoUser == 2) {
resetPass();
}
} else {
printf("\nPassword Reset Successful!!");
printf("\n\n==============================================================================================\n");
printf("\n Do you want to continue to login? or signup? or exit?");
printf("\nPress 1 to login, Press 2 to signup, Press any other key to exit!\n");
scanf("%d", &resetDoneUser);
if (resetDoneUser == 1) {
login();
} else if (resetDoneUser == 2) {
signup();
} else {
exit(0);
}
}
}
void LogOrSign(){
int LogOrSignOpt;
clearTerminal();
printf("\n\n\n\n\n==============================================================================================\n\n");
printf("Please Login or Signup to continue booking tickets");
printf("\n\n==============================================================================================\n\n");
printf(" Select from the following options:\n\n");
printf(" \033[1;31m[1]\033[0m Login \n\n");
printf(" \033[1;31m[2]\033[0m Signup\n\n");
printf(" \033[1;31m[3]\033[0m Reset Password\n\n\n\n");
printf(" \033[1;31m[4]\033[0m ADMIN LOGIN\n\n");
scanf("%d", &LogOrSignOpt);
switch (LogOrSignOpt)
{
case 1:
login();
break;
case 2:
signup();
break;
case 3:
resetPass();
break;
case 4:
adminLogin();
break;
default:
printf("Please Choose Correct Option");
sleepProgram(1);
LogOrSign();
break;
}
}
void adminLogin(){
int AdminLoginOpt;
clearTerminal();
redColor();
char adminPass[100];
strcpy(adminPass, "G7");
printf("\n\n\n\n\n==============================================================================================\n\n");
printf("Please Login With ADMIN PASS to continue to ADMIN CONTROLS");
printf("\n\n==============================================================================================\n\n");
printf("Print Admin PASS (case-sensitive)[ \033[1;32m It is G7 :) \033[1;31m ] :");
scanf("%s", adminPass);
PrintSleep(0.18);
getchar();
if (strcmp(adminPass, "G7") == 0) {
printf("\n\n\n\n\n==============================================================================================\n\n");
printf("Welcome to ADMIN CONTROLS");
printf("\n\n==============================================================================================\n\n");
printf("You will be redirected to ADMIN CONTROLS in 3 seconds!");
sleepProgram(3);
adminControls();
} else {
printf("\n\n\n\n\n==============================================================================================\n\n");
printf("Incorrect Password");
printf("\n\n==============================================================================================\n\n");
printf("Do you want to try again? press 1 to try again, press any other key to return to login");
scanf("%d", &AdminLoginOpt);
if (AdminLoginOpt == 1) {
sleepProgram(1);
adminLogin();
} else {
sleepProgram(1);
LogOrSign();
}
}
}
void addTrain() {
FILE *file = fopen("files/train.txt", "r+");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int numTrains;
fscanf(file, "%d", &numTrains);
numTrains++;
fseek(file, 0, SEEK_SET);
fprintf(file, "%d\n", numTrains);
fseek(file, 0, SEEK_END);
Train newTrain;
printf("\nPlease enter the Train ID: ");
scanf("%s", newTrain.trainId);
printf("Please enter the Starting Point: ");
scanf("%s", newTrain.startingPoint);
printf("Please enter the Destination: ");
scanf("%s", newTrain.destination);
printf("Please enter the Departure Time: ");
scanf("%s", newTrain.departureTime);
printf("Please enter the Cost: ");
scanf("%d", &newTrain.cost);
printf("Please enter number of Compartments: ");
scanf("%d", &newTrain.compartments);
printf("Please enter Seat type:- sleeper or seater: ");
scanf("%s", newTrain.seatType);
printf("Please enter Name of Train: ");
scanf("%s", newTrain.name);
printf("Please enter max speed of train: ");
scanf("%d", &newTrain.maxSpeed);
printf("Please enter total distance to be travelled by train: ");
scanf("%d", &newTrain.totalDist);
fprintf(file, "%s %s %s %s %d %d %s %s %d %d\n", newTrain.trainId, newTrain.startingPoint, newTrain.destination, newTrain.departureTime, newTrain.cost, newTrain.compartments, newTrain.seatType, newTrain.name, newTrain.maxSpeed, newTrain.totalDist);
fclose(file);
printf("\nTrain added successfully!\n");
printf("\nPress 1 to add another train or press any other key to go to admin controls.\n");
int addAnotherTrain;
scanf("%d", &addAnotherTrain);
if (addAnotherTrain == 1) {
sleepProgram(1);
addTrain();
} else {
sleepProgram(1);
adminControls();
}
}
void displayAllTrains() {
clearTerminal();
FILE *file = fopen("files/train.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int numTrains;
fscanf(file, "%d", &numTrains);
printf("\n========================================================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5s | %-20s | %-20s | %-20s | %-5s | %-5s | ", "Train ID", "Starting Point", "Destination", "Departure Time", "Cost", "Compartments", "Seat Type", "Train Name", "Max Speed", "Total Distance");
printf("\n========================================================================================================================================================================================\n");
for (int i = 0; i < numTrains; i++) {
Train train;
fscanf(file, "%s %s %s %s %d %d %s %s %d %d", train.trainId, train.startingPoint, train.destination, train.departureTime, &train.cost, &train.compartments, train.seatType, train.name, &train.maxSpeed, &train.totalDist);
printf("| %-10s | %-20s | %-20s | %-15s | %-5d | %-20d | %-20s | %-20s | %-5dKM/H | %-12dKM |\n", train.trainId, train.startingPoint, train.destination, train.departureTime, train.cost, train.compartments, train.seatType, train.name, train.maxSpeed, train.totalDist);
}
printf("========================================================================================================================================================================================\n");
fclose(file);
}
int compareByCostAsc(const void* a, const void* b) {
return ((Train*)a)->cost - ((Train*)b)->cost;
}
int compareByCostDesc(const void* a, const void* b) {
return ((Train*)b)->cost - ((Train*)a)->cost;
}
void sortByCost(int sortOrder) {
FILE* file = fopen("files/train.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int numTrains;
fscanf(file, "%d", &numTrains);
Train* trains = (Train*)malloc(numTrains * sizeof(Train));
if (trains == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
exit(1);
}
for (int i = 0; i < numTrains; i++) {
fscanf(file, "%s %s %s %s %d %d %s %s %d %d", trains[i].trainId, trains[i].startingPoint, trains[i].destination, trains[i].departureTime, &trains[i].cost, &trains[i].compartments, trains[i].seatType, trains[i].name, &trains[i].maxSpeed, &trains[i].totalDist);
}
fclose(file);
if (sortOrder == 2) {
qsort(trains, numTrains, sizeof(Train), compareByCostDesc);
} else {
qsort(trains, numTrains, sizeof(Train), compareByCostAsc);
}
printf("\n========================================================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5s | %-20s | %-20s | %-20s | %-5s | %-5s | ", "Train ID", "Starting Point", "Destination", "Departure Time", "Cost", "Compartments", "Seat Type", "Train Name", "Max Speed", "Total Distance");
printf("\n========================================================================================================================================================================================\n");
for (int i = 0; i < numTrains; i++) {
printf("| %-10s | %-20s | %-20s | %-15s | %-5d | %-20d | %-20s | %-20s | %-5dKM/H | %-12dKM |\n", trains[i].trainId, trains[i].startingPoint, trains[i].destination, trains[i].departureTime, trains[i].cost, trains[i].compartments, trains[i].seatType, trains[i].name, trains[i].maxSpeed, trains[i].totalDist);
}
printf("\n========================================================================================================================================================================================\n");
printf("\nPress 1 to continue to booking or press 2 to go back to main menu: ");
int afterSortChoice;
scanf("%d", &afterSortChoice);
if (afterSortChoice == 1) {
printf("Please enter the train id: ");
getchar();
char trainId[10];
scanf("%s", trainId);
findTrain(trainId);
} else{
sleepProgram(1);
mainMenu();
}
free(trains);
}
void findTrainsByDestination(const char* destination) {
FILE* file = fopen("files/train.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int numTrains;
fscanf(file, "%d", &numTrains);
printf("\n========================================================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5s | %-20s | %-20s | %-20s | %-5s | %-5s | ", "Train ID", "Starting Point", "Destination", "Departure Time", "Cost", "Compartments", "Seat Type", "Train Name", "Max Speed", "Total Distance");
printf("\n========================================================================================================================================================================================\n");
Train train;
int found = 0;
while (fscanf(file, "%s %s %s %s %d %d %s", train.trainId, train.startingPoint, train.destination, train.departureTime, &train.cost, &train.compartments, train.seatType) != EOF) {
if (strcmp(train.destination, destination) == 0) {
found = 1;
printf("| %-10s | %-20s | %-20s | %-15s | %-5d | %-20d | %-20s | %-20s | %-5dKM/H | %-12dKM |\n", train.trainId, train.startingPoint, train.destination, train.departureTime, train.cost, train.compartments, train.seatType, train.name, train.maxSpeed, train.totalDist);
}
}
printf("\n========================================================================================================================================================================================\n");
fclose(file);
if(found){
printf("\nPress 1 to continue to booking or press 2 to go back to main menu: ");
int afterSortChoice;
scanf("%d", &afterSortChoice);
if (afterSortChoice == 1) {
printf("Please enter the train id: ");
getchar();
char trainId[10];
scanf("%s", trainId);
findTrain(trainId);
} else{
sleepProgram(1);
mainMenu();
}
}
if (!found){
printf("No trains found for the destination: %s\n", destination);
printf("You will be redirected to main menu in 3 seconds.\n");
sleepProgram(3);
mainMenu();
}
}
void findTrainsByStartingPoint(const char* startingPoint) {
FILE* file = fopen("files/train.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
int numTrains;
fscanf(file, "%d", &numTrains);
printf("\n========================================================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5s | %-20s | %-20s | %-20s | %-5s | %-5s | ", "Train ID", "Starting Point", "Destination", "Departure Time", "Cost", "Compartments", "Seat Type", "Train Name", "Max Speed", "Total Distance");
printf("========================================================================================================================================================================================\n");
Train train;
int found = 0;
while (fscanf(file, "%s %s %s %s %d %d %s", train.trainId, train.startingPoint, train.destination, train.departureTime, &train.cost, &train.compartments, train.seatType) != EOF) {
if (strcmp(train.startingPoint, startingPoint) == 0) {
found = 1;
printf("| %-10s | %-20s | %-20s | %-15s | %-5d | %-20d | %-20s | %-20s | %-5dKM/H | %-12dKM |\n", train.trainId, train.startingPoint, train.destination, train.departureTime, train.cost, train.compartments, train.seatType, train.name, train.maxSpeed, train.totalDist);
}
}
printf("\n========================================================================================================================================================================================\n");
fclose(file);
if(found){
printf("\nPress 1 to continue to booking or press 2 to go back to main menu: ");
int afterSortChoice;
scanf("%d", &afterSortChoice);
if (afterSortChoice == 1) {
printf("Please enter the train id: ");
getchar();
char trainId[10];
scanf("%s", trainId);
findTrain(trainId);
} else{
sleepProgram(1);
mainMenu();
}
}
if (!found){
printf("No trains found for the start point: %s\n", startingPoint);
printf("You will be redirected to main menu in 3 seconds.\n");
sleepProgram(3);
mainMenu();
}
}
void adminControls() {
clearTerminal();
greenColor();
printf("\n====================================================================================================================================\n");
printf("\n\t\t\tWelcome To Admin Controls\n");
printf(" *Please Select Appropriate Option:*\n");
printf("\n====================================================================================================================================\n");
printf(" \033[1;31m[1]\033[0m VIEW TRAIN LIST \n\n");
printf(" \033[1;31m[2]\033[0m ADD NEW TRAIN\n\n");
printf(" \033[1;31m[3]\033[0m Add Delayed Trains\n\n");
printf(" \033[1;31m[4]\033[0m Display Delayed Trains\n\n");
printf(" \033[1;31m[5]\033[0m GO BACK TO LOGIN SIGNUP WINDOW\n\n");
int adminChoice;
scanf("%d", &adminChoice);
switch (adminChoice) {
case 1:
sleepProgram(1);
displayAllTrains();
sleepProgram(10);
adminControls();
break;
case 2:
sleepProgram(1);
addTrain();
break;
case 3:
sleepProgram(1);
addDelayedTrain();
break;
case 4:
sleepProgram(1);
displayDelayedTrains(0);
break;
case 5:
sleepProgram(1);
LogOrSign();
break;
default:
printf("Oops Wrong Choice");
adminControls();
break;
}
}
void addDelayedTrain() {
FILE *file = fopen("files/delayed.txt", "a");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
DelayedTrain delayedTrain;
printf("\nPlease enter the Train ID: ");
scanf("%s", delayedTrain.trainId);
printf("Please enter the Starting Point: ");
scanf("%s", delayedTrain.startingPoint);
printf("Please enter the Destination: ");
scanf("%s", delayedTrain.destination);
printf("Please enter the Departure Time: ");
scanf("%s", delayedTrain.departureTime);
printf("Please enter the Cost: ");
scanf("%d", &delayedTrain.cost);
printf("Please enter number of Compartments: ");
scanf("%d", &delayedTrain.compartments);
printf("Please enter Seat type: sleeper or seater: ");
scanf("%s", delayedTrain.seatType);
printf("Is the train delayed? (1 for Yes, 0 for No): ");
scanf("%d", &delayedTrain.isDelayed);
if (delayedTrain.isDelayed) {
printf("Enter delay time (in minutes): ");
scanf("%d", &delayedTrain.delayTime);
} else {
delayedTrain.delayTime = 0;
}
fprintf(file, "%s %s %s %s %d %d %s %d %d\n", delayedTrain.trainId, delayedTrain.startingPoint, delayedTrain.destination, delayedTrain.departureTime, delayedTrain.cost, delayedTrain.compartments, delayedTrain.seatType, delayedTrain.isDelayed, delayedTrain.delayTime);
fclose(file);
printf("\nDelayed train added successfully!\n");
sleepProgram(3);
adminControls();
}
void displayDelayedTrains(int isAdmin) {
FILE *file = fopen("files/delayed.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
exit(1);
}
char delayedTrainID[10];
printf("Enter Delayed Train ID: ");
scanf("%s", delayedTrainID);
int delayedFoundTrain = 0;
DelayedTrain delayedTrain;
while (fscanf(file, "%s %s %s %s %d %d %s %d %d", delayedTrain.trainId, delayedTrain.startingPoint, delayedTrain.destination, delayedTrain.departureTime, &delayedTrain.cost, &delayedTrain.compartments, delayedTrain.seatType, &delayedTrain.isDelayed, &delayedTrain.delayTime) != EOF) {
if(strcmp(delayedTrain.trainId, delayedTrainID) == 0){
delayedFoundTrain = 1;
printf("\n===========================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5s | %-20s | %-20s | %-10s | %-15s |\n", "Train ID", "Starting Point", "Destination", "Departure Time", "Cost", "Compartments", "Seat Type", "Status", "Delay (min)");
printf("===========================================================================================================================================================\n");
printf("| %-10s | %-20s | %-20s | %-15s | %-5d | %-20d | %-20s | %-10s | %-15d |\n", delayedTrain.trainId, delayedTrain.startingPoint, delayedTrain.destination, delayedTrain.departureTime, delayedTrain.cost, delayedTrain.compartments, delayedTrain.seatType, delayedTrain.isDelayed ? "Delayed" : "On Time", delayedTrain.delayTime);
}
printf("===========================================================================================================================================================\n");
}
fclose(file);
if (delayedFoundTrain == 0) {
printf("Train with id %s is on time!!\n", delayedTrainID);
}
printf("\n\nPress 1 to check another train status or press 2 to go back to main menu: ");
int afterDelayed;
scanf("%d", &afterDelayed);
if (afterDelayed == 1) {
displayDelayedTrains(isAdmin);
} else {
if(isAdmin == 1){
mainMenu();
}else{
adminControls();
}
}
}
void trainListandBook(){
displayAllTrains();
int ifSort;
printf("\nThese trains are daily available trains!! Enter your preferred date of travel on ticket selection section!! \n");
printf("Do you want to sort trains? (1 for yes) Or Press 2 for ticket booking Or Press anu other key to go back to main menu: ");
scanf("%d", &ifSort);
if (ifSort == 1) {