forked from tatwik-sai/QuickTicket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
2089 lines (1881 loc) · 71.7 KB
/
interface.c
File metadata and controls
2089 lines (1881 loc) · 71.7 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"
#include "database.h"
#include "utils.h"
#include <unistd.h>
#include <sys/ioctl.h>
int console_width;
void setup_intro();
void user(char*);
void admin(char*);
void u_option1();
void u_option2_3(int option, char *date);
void u_option4(char *username);
void u_option5();
void u_option6_7(int option, char* username);
void u_option8(char *username);
void u_option9(char *username);
void a_option1();
void a_option2_3(int option, int id);
void a_option4();
void a_option5();
void a_option6();
void a_option7();
void a_option8_9(int option);
void print_line(int width, char *colour){
for (int i = 0; i < width; i++) {
#ifdef _WIN32
printf("%s%c", colour, 196);
#else
printf("%s\u2500", colour);
#endif
}
printf("\033[0m");
}
void print_table(char *headers[], int cols, char data[][cols][500], int rows) {
int col_widths[cols];
int spacing = 5;
char *line_color = HEADER_COLOR;
for (int j = 0; j < cols; j++) {
col_widths[j] = strlen(headers[j]) + spacing;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int data_len = strlen(data[i][j]) + spacing;
if (data_len > col_widths[j]) {
col_widths[j] = data_len;
}
}
}
printf(HEADER_COLOR);
for (int j = 0; j < cols; j++) {
printf("%-*s ", col_widths[j], headers[j]);
}
printf(RESET_COLOR "\n");
for (int j = 0; j < cols; j++) {
for (int k = 0; k < col_widths[j]; k++){
#ifdef _WIN32
printf("%s%c", line_color, 196);
#else
printf("%s\u2500", line_color);
#endif
}
printf(" ");
}
printf("\n");
printf(DATA_COLOR);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%-*s ", col_widths[j], data[i][j]);
}
printf("\n");
}
printf(RESET_COLOR);
}
void login_page(){
int choice;
char username[50];
char password[50];
authentication:
printf(CHOICE);
printf("\nEnter your choice: ");
printf(RESET_COLOR);
if (scanf("%d", &choice) != 1) {
printf(ERROR);
printf("[-] Choose from one of the above options only!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
goto authentication;
}
switch (choice){
case 1:
printf("\tTo login as user...\n");
user:
printf(INNER_QUERY);
printf("\tEnter your Username: ");
printf(RESET_COLOR);
scanf("%49s", username);
printf(INNER_QUERY);
printf("\tEnter your Password: ");
printf(RESET_COLOR);
printf("\033[8m");
scanf("%49s", password);
printf(RESET_COLOR);
if (validate_user_admin(username, password, 1)){
user(username);
}
else{
printf(ERROR);
printf("\t[-]Invalid Username or Password!\n");
printf(RESET_COLOR);
goto user;
}
break;
case 2:
printf("\tTo login as admin...\n");
admin:
printf(INNER_QUERY);
printf("\tEnter your Username: ");
printf(RESET_COLOR);
scanf("%49s", username);
printf(INNER_QUERY);
printf("\tEnter your Password: ");
printf(RESET_COLOR);
printf("\033[8m");
scanf("%49s", password);
printf(RESET_COLOR);
if (validate_user_admin(username, password, 2)){
admin(username);
}
else{
printf(ERROR);
printf("\t[-]Invalid Username or Password!\n");
printf(RESET_COLOR);
goto admin;
}
break;
case 3:
printf("\tTo Create a New User Account...\n");
create_account:
printf(INNER_QUERY);
printf("\tChoose your Username: ");
printf(RESET_COLOR);
scanf("%49s", username);
printf(INNER_QUERY);
printf("\tChoose your Password: ");
printf(RESET_COLOR);
printf("\033[8m");
scanf("%49s", password);
printf(RESET_COLOR);
char name[100], gender[10], email[100];
int age;
printf(INNER_QUERY);
printf("\tName: ");
printf(RESET_COLOR);
getchar();
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
printf(INNER_QUERY);
printf("\tEmail: ");
printf(RESET_COLOR);
scanf("%99s", email);
age:
printf(INNER_QUERY);
printf("\tAge: ");
printf(RESET_COLOR);
if (scanf("%d", &age) != 1) {
printf(ERROR);
printf("\t[-] Invalid Age!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
goto age;
}
if (age < 10 || age > 120){
printf(ERROR);
printf("\t[-]Either you have entered an invaid age or your not eligible to create an account!\n");
printf(RESET_COLOR);
goto age;
}
printf("\tWhats your gender...\n\t1 \u2192 Male\n\t2 \u2192 Female\n\t3 \u2192 Other\n");
int gen_code;
gender:
printf(INNER_QUERY);
printf("\tGender: ");
printf(RESET_COLOR);
if (scanf("%d", &gen_code) != 1) {
printf(ERROR);
printf("\t[-] Invalid input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
goto gender;
}
switch (gen_code){
case 1:
strcpy(gender, "Male");
break;
case 2:
strcpy(gender, "Female");
break;
case 3:
strcpy(gender, "Other");
break;
default:
printf(ERROR);
printf("\t[-]Invaid input!\n");
printf(RESET_COLOR);
goto gender;
}
if (user_or_email_exists(username, email)){
printf(ERROR);
printf("\t[-]User already exists with same email or username!\n");
printf(RESET_COLOR);
goto create_account;
}
else{
addUser(username, password, name, age, gender, email);
user(username);
}
break;
case 0:
close_db();
exit(0);
break;
default:
printf(ERROR);
printf("[-] Choose from one of the above options only!\n");
printf(RESET_COLOR);
goto authentication;
}
}
void user(char *username){
setup_intro();
printf("Hey, %s\n", username);
printf("[+] You have LoggedIn as User...\n\n");
printf("Main Menu:\n");
printf("\t1 \u2192 View all Trains\n"
"\t2 \u2192 Search Trains from Source to Destination\n"
"\t3 \u2192 Search Trains from Source to Destination on a Date\n"
"\t4 \u2192 Book a Ticket\n"
"\t5 \u2192 View Train Map\n"
"\t6 \u2192 View Reservation History\n"
"\t7 \u2192 View Active Reservations\n"
"\t8 \u2192 Cancel a Reservation\n"
"\t9 \u2192 Update Profile\n"
"\t0 \u2192 Exit\n");
int choice = -1;
choice:
while (choice != 0){
input:
printf(CHOICE);
printf("\nEnter your choice: ");
printf(RESET_COLOR);
if (scanf("%d", &choice) != 1) {
printf(ERROR);
printf("[-] Choose from one of the above options only!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
goto input;
}
switch (choice){
case 1:
u_option1();
break;
case 2:
u_option2_3(2, "");
break;
case 3:
printf("\tDate Format: yyyy-mm-dd\n");
printf(INNER_QUERY);
printf("\tDate of Travel: ");
printf(RESET_COLOR);
char date[20];
scanf("%s", date);
u_option2_3(3, date);
break;
case 4:
u_option4(username);
break;
case 5:
u_option5();
break;
case 6:
u_option6_7(6, username);
break;
case 7:
u_option6_7(7, username);
break;
case 8:
u_option8(username);
break;
case 9:
u_option9(username);
break;
case 0:
exit(0);
break;
default:
printf(ERROR);
printf("[-] Choose from one of the above options only!\n");
printf(RESET_COLOR);
goto choice;
}
}
}
void u_option1(){
char *query =
"SELECT Trains.t_name, TrainSchedules.id, TrainSchedules.dep_date, TrainSchedules.dep_time, "
"TrainSchedules.arr_date, TrainSchedules.arr_time, TrainSchedules.moving, "
"Trains.s_station, Trains.e_station "
"FROM TrainSchedules "
"JOIN Trains ON TrainSchedules.t_id = Trains.id "
"ORDER BY TrainSchedules.t_id, TrainSchedules.id, TrainSchedules.dep_date";
sqlite3_stmt *stmt = execute_command(query);
char last_train_name[300] = "";
char *headers[] = {"ID", "Departure", "Arrival", "Journey Type"};
int cols = sizeof(headers) / sizeof(headers[0]);
char data[1000][cols][500];
int rows = 0;
int any_data = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char *train_name = (const char *)sqlite3_column_text(stmt, 0);
int schedule_id = sqlite3_column_int(stmt, 1);
const char *dep_date = (const char *)sqlite3_column_text(stmt, 2);
const char *dep_time = (const char *)sqlite3_column_text(stmt, 3);
const char *arr_date = (const char *)sqlite3_column_text(stmt, 4);
const char *arr_time = (const char *)sqlite3_column_text(stmt, 5);
const char *moving = (const char *)sqlite3_column_text(stmt, 6);
const char *s_station = (const char *)sqlite3_column_text(stmt, 7);
const char *e_station = (const char *)sqlite3_column_text(stmt, 8);
if (strcmp(train_name, last_train_name) != 0) {
if (strlen(last_train_name) > 0) {
print_table(headers, cols, data, rows);
}
char form_t_name[200];
sprintf(form_t_name, "%s (%s \u21D4 %s)\n", train_name, s_station, e_station);
printf("\n");
center_text(form_t_name, 70, "\033[1;31m");
strcpy(last_train_name, train_name);
rows = 0;
}
if (is_date_time_in_future(dep_date, dep_time)){
any_data = 1;
sprintf(data[rows][0], "%d", schedule_id);
snprintf(data[rows][1], 100, "%s %s", dep_date, dep_time);
snprintf(data[rows][2], 100, "%s %s", arr_date, arr_time);
strcpy(data[rows][3], (strcmp(moving, "0") == 0) ? "Forward" : "Return");
rows++;
}
}
if (any_data){
print_table(headers, cols, data, rows);
}
else{
printf("\tNo Trains Found\n");
}
sqlite3_finalize(stmt);
}
// change to not to print train name if no schedule matches the user specification
void u_option2_3(int option, char *date) {
char from[200], to[200];
printf(INNER_QUERY);
printf("\tFrom Location: ");
printf(RESET_COLOR);
getchar();
fgets(from, 199, stdin);
from[strcspn(from, "\n")] = '\0';
printf(INNER_QUERY);
printf("\tTo Location: ");
printf(RESET_COLOR);
fgets(to, 199, stdin);
to[strcspn(to, "\n")] = '\0';
char *query =
"SELECT Trains.t_name, TrainSchedules.id, TrainSchedules.dep_date, TrainSchedules.dep_time, "
"TrainSchedules.arr_date, TrainSchedules.arr_time, TrainSchedules.moving, "
"Trains.s_station, Trains.e_station, Trains.stations "
"FROM TrainSchedules "
"JOIN Trains ON TrainSchedules.t_id = Trains.id "
"ORDER BY Trains.id, TrainSchedules.dep_date, TrainSchedules.dep_time, TrainSchedules.id";
sqlite3_stmt *stmt = execute_command(query);
char last_train_name[300] = "";
char *headers[] = {"ID", "Departure", "Arrival"};
int cols = sizeof(headers) / sizeof(headers[0]);
char data[500][cols][500];
int rows = 0;
int satifying_query = 0;
char output[MAX_STATIONS][MAX_STATION_NAME];
int forward;
int any_data = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char *train_name = (const char *)sqlite3_column_text(stmt, 0);
int schedule_id = sqlite3_column_int(stmt, 1);
const char *dep_date = (const char *)sqlite3_column_text(stmt, 2);
const char *dep_time = (const char *)sqlite3_column_text(stmt, 3);
const char *arr_date = (const char *)sqlite3_column_text(stmt, 4);
const char *arr_time = (const char *)sqlite3_column_text(stmt, 5);
const char *moving = (const char *)sqlite3_column_text(stmt, 6);
const char *s_station = (const char *)sqlite3_column_text(stmt, 7);
const char *e_station = (const char *)sqlite3_column_text(stmt, 8);
const char *stations = (const char *)sqlite3_column_text(stmt, 9);
if (strcmp(train_name, last_train_name) != 0) {
satifying_query = 0;
int count = split_string(stations, MAX_STATIONS, MAX_STATION_NAME, output);
int s = 0;
forward = 2737;
if (strcmp(moving, "0") == 0) {
for (int i = 0; i < count; i++) {
if (s == 1 && compare_strings(output[i], to)) {
forward = 0;
satifying_query = 1;
break;
} else if (compare_strings(output[i], from)) {
s = 1;
}
}
} else {
for (int i = count - 1; i >= 0; i--) {
if (s == 1 && compare_strings(output[i], to)) {
forward = 1;
satifying_query = 1;
break;
} else if (compare_strings(output[i], from)) {
s = 1;
}
}
}
if (satifying_query) {
if (strlen(last_train_name) > 0) {
print_table(headers, cols, data, rows);
}
char form_t_name[200];
sprintf(form_t_name, "%s (%s \u21D4 %s)\n", train_name, s_station, e_station);
printf("\n");
center_text(form_t_name, 50, "\033[1;31m");
strcpy(last_train_name, train_name);
rows = 0;
}
}
if (satifying_query && (strcmp(moving, (forward == 0)?"0":"1") == 0)) {
if (option == 2){
any_data = 1;
sprintf(data[rows][0], "%d", schedule_id);
snprintf(data[rows][1], 100, "%s %s", dep_date, dep_time);
snprintf(data[rows][2], 100, "%s %s", arr_date, arr_time);
rows++;
}
else if (option == 3 && (strcmp(date, dep_date)==0)){
any_data = 1;
sprintf(data[rows][0], "%d", schedule_id);
snprintf(data[rows][1], 100, "%s %s", dep_date, dep_time);
snprintf(data[rows][2], 100, "%s %s", arr_date, arr_time);
rows++;
}
}
}
if (any_data){
print_table(headers, cols, data, rows);
}
else{
printf("\tNo Trains Found.\n");
}
sqlite3_finalize(stmt);
}
void u_option4(char *username){
// Getting the schedule id
train_id:
printf(INNER_QUERY);
printf("\tEnter the Train ID: ");
printf(RESET_COLOR);
int id;
if (scanf("%d", &id) != 1) {
printf(ERROR);
printf("\t[-] Invalid Input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
// Getting the data no of seats of each type available
char *query1 =
"SELECT sleeper_count, ac3_count, ac2_count, ac1_count, general_count, canceled, t_id "
"FROM TrainSchedules WHERE id = ?";
sqlite3_stmt *stmt = execute_command(query1);
sqlite3_bind_int(stmt, 1, id);
int t_id = -1;
int sleeper_count = 0, ac3_count = 0, ac2_count = 0, ac1_count = 0, general_count = 0;
int coach_cap = 0, sleeper_seats = 0, ac3_seats = 0, ac2_seats = 0, ac1_seats = 0, general_seats = 0;
char *canceled = NULL;
if (sqlite3_step(stmt) == SQLITE_ROW) {
sleeper_count = sqlite3_column_int(stmt, 0);
ac3_count = sqlite3_column_int(stmt, 1);
ac2_count = sqlite3_column_int(stmt, 2);
ac1_count = sqlite3_column_int(stmt, 3);
general_count = sqlite3_column_int(stmt, 4);
t_id = sqlite3_column_int(stmt, 6);
const char *temp = (const char *)sqlite3_column_text(stmt, 5);
if (temp != NULL) {
canceled = malloc(strlen(temp) + 1);
strcpy(canceled, temp);
}
} else {
printf(ERROR);
printf("\t[-] Make sure you have entered a valid id(Check option 1, 2, 3 to get id)\n");
printf(RESET_COLOR);
goto train_id;
}
sqlite3_finalize(stmt);
char *query2 =
"SELECT coach_cap, sleeper_seats, ac3_seats, ac2_seats, ac1_seats, general_seats, t_name "
"FROM Trains WHERE id = ?";
stmt = execute_command(query2);
sqlite3_bind_int(stmt, 1, t_id);
if (sqlite3_step(stmt) == SQLITE_ROW) {
coach_cap = sqlite3_column_int(stmt, 0);
sleeper_seats = sqlite3_column_int(stmt, 1);
ac3_seats = sqlite3_column_int(stmt, 2);
ac2_seats = sqlite3_column_int(stmt, 3);
ac1_seats = sqlite3_column_int(stmt, 4);
general_seats = sqlite3_column_int(stmt, 5);
char *t_name = (char*)sqlite3_column_text(stmt, 6);
} else {
printf(ERROR);
printf("[-] No Train exist with ID: %d\n", t_id);
printf(RESET_COLOR);
}
sqlite3_finalize(stmt);
// No of seats left for each class
int rem_sle = sleeper_seats - sleeper_count;
int rem_ac3 = ac3_seats - ac3_count;
int rem_ac2 = ac2_seats - ac2_count;
int rem_ac1 = ac1_seats - ac1_count;
int rem_gen = general_seats - general_count;
char cancelled_seats[1000][20];
int count = split_string(canceled, 1000, 7, cancelled_seats);
for (int i = 0; i < count; i++){
if (cancelled_seats[i][0] == 's'){
rem_sle += 1;
}
else if (cancelled_seats[i][0] == 'a'){
if (cancelled_seats[i][2] == '1'){
rem_ac1 += 1;
}
else if (cancelled_seats[i][2] == '2'){
rem_ac2 += 1;
}
else if (cancelled_seats[i][2] == '3'){
rem_ac3 += 1;
}
}
else if (cancelled_seats[i][0] == 'g'){
rem_gen += 1;
}
}
// Taking the user input regarding passenger info and seat preferences
int n_tickets;
printf(INNER_QUERY);
printf("\tNumber of tickets: ");
printf(RESET_COLOR);
if (scanf("%d", &n_tickets) != 1) {
printf(ERROR);
printf("\t[-] Invalid Input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
if (n_tickets > 6){
printf(ERROR);
printf("\t[-] You are only allowed to book maximum of 20 tickets!\n");
printf(RESET_COLOR);
return;
}
printf("\tSeat Types: \n");
printf("\t 1 \u2192 Sleeper : %d\n", rem_sle);
printf("\t 2 \u2192 AC 3 Tier: %d\n", rem_ac3);
printf("\t 3 \u2192 AC 2 Tier: %d\n", rem_ac2);
printf("\t 4 \u2192 AC 1 Tier: %d\n", rem_ac1);
printf("\t 5 \u2192 General : %d\n", rem_gen);
char passengers[n_tickets][200];
int seat_type[n_tickets];
int age[n_tickets];
int gender[n_tickets];
int ts=rem_sle, ta1=rem_ac1, ta2=rem_ac2, ta3=rem_ac3, tg=rem_gen;
for (int i = 0; i < n_tickets; i++){
printf(INNER_QUERY);
printf("\tName of passenger %d: ", i+1);
printf(RESET_COLOR);
getchar();
fgets(passengers[i], 199, stdin);
passengers[i][strcspn(passengers[i], "\n")] = '\0';
printf(INNER_QUERY);
printf("\tAge of %s: ", passengers[i]);
printf(RESET_COLOR);
if (scanf("%d", &age[i]) != 1) {
printf(ERROR);
printf("\t[-] Invalid Age!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
printf("\t1 \u2192 Male\n\t2 \u2192 Female\n");
printf(INNER_QUERY);
printf("\tGender: ");
printf(RESET_COLOR);
if (scanf("%d", &gender[i]) != 1) {
printf(ERROR);
printf("\t[-] Invalid Input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
seat:
printf(INNER_QUERY);
printf("\tSeat type for %s: ", passengers[i]);
printf(RESET_COLOR);
if (scanf("%d", &seat_type[i]) != 1) {
printf(ERROR);
printf("\t[-] Invalid Input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
switch (seat_type[i])
{
case 1:
if (!(ts > 0)){
printf(ERROR);
printf("\t[-] This class is occupied. Please choose another!\n");
printf(RESET_COLOR);
goto seat;
}
ts -= 1;
break;
case 2:
if (!(ta3 > 0)){
printf(ERROR);
printf("\t[-] This class is occupied. Please choose another!\n");
printf(RESET_COLOR);
goto seat;
}
ta3 -= 1;
break;
case 3:
if (!(ta2 > 0)){
printf(ERROR);
printf("\t[-] This class is occupied. Please choose another!\n");
printf(RESET_COLOR);
goto seat;
}
ta2 -= 1;
break;
case 4:
if (!(ta1 > 0)){
printf(ERROR);
printf("\t[-] This class is occupied. Please choose another!\n");
printf(RESET_COLOR);
goto seat;
}
ta1 -= 1;
break;
case 5:
if (!(tg > 0)){
printf(ERROR);
printf("\t[-] This class is occupied. Please choose another!\n");
printf(RESET_COLOR);
goto seat;
}
tg -= 1;
break;
default:
printf(ERROR);
printf("\t[-] Invalid choice Enter one of the above mentioned choices!\n");
printf(RESET_COLOR);
goto seat;
}
printf("\n");
}
// Checking if the requested seats are present in canceled seats
int indexes[n_tickets];
memset(indexes, -1, sizeof(indexes));
for (int j = 0; j < n_tickets; j++){
for (int i = 0; i < count; i++){
int cont = 0;
for (int k = 0; k <= j; k++){
if (indexes[k] == i){
cont = 1;
break;
}
}
if (cont){
continue;
}
if (cancelled_seats[i][0] == 's' && seat_type[j] == 1){
indexes[j] = i;
rem_sle -= 1;
break;
}
else if (cancelled_seats[i][0] == 'a'){
if (cancelled_seats[i][2] == '1' && seat_type[j] == 4){
indexes[j] = i;
rem_ac1 -= 1;
break;
}
else if (cancelled_seats[i][2] == '2' && seat_type[j] == 3){
indexes[j] = i;
rem_ac2 -= 1;
break;
}
else if (cancelled_seats[i][2] == '3' && seat_type[j] == 2){
indexes[j] = i;
rem_ac3 -= 1;
break;
}
}
else if (cancelled_seats[i][0] == 'g' && seat_type[j] == 5){
indexes[j] = i;
rem_gen -= 1;
break;
}
}
}
// Alloting the canceled seats if prpesent some new seats
char seats_alloted[n_tickets][10];
for (int i = 0; i < n_tickets; i++){
if (indexes[i] == -1){
if (seat_type[i] == 1 && rem_sle > 0){
sleeper_count += 1;
int c_no = (sleeper_count/coach_cap) + 1;
char c[20] = "s-";
sprintf(c + strlen(c), "%d-", c_no);
sprintf(c + strlen(c), "%d", sleeper_count);
strcpy(seats_alloted[i], c);
rem_sle -= 1;
}
else if (seat_type[i] == 2 && rem_ac3 > 0){
ac3_count += 1;
int c_no = (ac3_count/coach_cap) + 1;
char c[20] = "ac3-";
sprintf(c + strlen(c), "%d-", c_no);
sprintf(c + strlen(c), "%d", ac3_count);
strcpy(seats_alloted[i], c);
rem_ac3 -= 1;
}
else if (seat_type[i] == 3 && rem_ac2 > 0){
ac2_count += 1;
int c_no = (ac2_count/coach_cap) + 1;
char c[20] = "ac2-";
sprintf(c + strlen(c), "%d-", c_no);
sprintf(c + strlen(c), "%d", ac2_count);
strcpy(seats_alloted[i], c);
rem_ac2 -= 1;
}
else if (seat_type[i] == 4 && rem_ac1 > 0){
ac1_count += 1;
int c_no = (ac1_count/coach_cap) + 1;
char c[20] = "ac1-";
sprintf(c + strlen(c), "%d-", c_no);
sprintf(c + strlen(c), "%d", ac1_count);
strcpy(seats_alloted[i], c);
rem_ac1 -= 1;
}
else if (seat_type[i] == 5 && rem_gen > 0){
general_count += 1;
int c_no = (general_count/coach_cap) + 1;
char c[20] = "g-";
sprintf(c + strlen(c), "%d-", c_no);
sprintf(c + strlen(c), "%d", general_count);
strcpy(seats_alloted[i], c);
rem_gen -= 1;
}
}
else{
strcpy(seats_alloted[i], cancelled_seats[indexes[i]]);
}
}
// Formatting data to update the database
// Canceled seats new data
char new_cancelled[1000] = "";
int is_f = 1;
for (int i = 0; i < count; i++){
if (indexes[i] == -1){
if (is_f){
is_f = 0;
}
else{
strcat(new_cancelled, ",");
}
strcat(new_cancelled, cancelled_seats[i]);
}
}
// Seats and the passenger data
char seats_data[2000] = "";
char passengers_data[2000] = "";
for (int i = 0; i < n_tickets; i++){
strcat(seats_data, seats_alloted[i]);
strcat(passengers_data, passengers[i]);
strcat(passengers_data, ",");
char buffer[20];
sprintf(buffer, "%d", age[i]);
strcat(passengers_data, buffer);
strcat(passengers_data, ",");
sprintf(buffer, "%d", gender[i]);
strcat(passengers_data, buffer);
if (i != n_tickets-1){
strcat(seats_data, ",");
strcat(passengers_data, ",");
}
}
// Date of reservation (Current date)
setenv("TZ", "Asia/Kolkata", 1);
tzset();
time_t t = time(NULL);
struct tm *tm_info = localtime(&t);
char date[11];
strftime(date, sizeof(date), "%Y-%m-%d", tm_info);
// Getting the user_id corresponding to username
int u_id = get_user_id(username);
char *status = "RESERVED";
// Inserting into Reservation Table
char *query4 = "INSERT INTO Reservation (t_id, sch_id, user_id, reservaton_date, seats, passengers, status) "
"VALUES (?, ?, ?, ?, ?, ?, ?)";
stmt = execute_command(query4);
sqlite3_bind_int(stmt, 1, t_id);
sqlite3_bind_int(stmt, 2, id);
sqlite3_bind_int(stmt, 3, u_id);
sqlite3_bind_text(stmt, 4, date, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 5, seats_data, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, passengers_data, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 7, status, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) != SQLITE_DONE) {
printf(ERROR);
printf("\t[-] SQL Error while inserting into Reservations.\n");
printf(RESET_COLOR);
}
sqlite3_finalize(stmt);
// Updating seats availablity in TrainSchedules
char *query5 = "UPDATE TrainSchedules SET sleeper_count = ?, ac3_count = ?, ac2_count = ?, "
"ac1_count = ?, general_count = ?, canceled = ? WHERE id = ?";
stmt = execute_command(query5);
sqlite3_bind_int(stmt, 1, sleeper_count);
sqlite3_bind_int(stmt, 2, ac3_count);
sqlite3_bind_int(stmt, 3, ac2_count);
sqlite3_bind_int(stmt, 4, ac1_count);
sqlite3_bind_int(stmt, 5, general_count);
sqlite3_bind_text(stmt, 6, new_cancelled, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 7, id);
if (sqlite3_step(stmt) != SQLITE_DONE) {
printf(ERROR);
printf("\tSQL Error while inserting into TrainSchedules.\n");
printf(RESET_COLOR);
}
sqlite3_finalize(stmt);
printf(SUCCESS);
printf("\t[+] Your ticket has been sucessfully reserved. View more under 'Reservations'.\n");
printf(RESET_COLOR);
if (canceled != NULL) {
free(canceled);
}
}
void u_option5(){
int id;
printf(INNER_QUERY);
printf("\tTrain ID: ");
printf(RESET_COLOR);
if (scanf("%d", &id) != 1) {
printf(ERROR);
printf("\t[-] Invalid Input!\n");
printf(RESET_COLOR);
while (getchar() != '\n');
return;
}
char *query =
"SELECT Trains.stations, TrainSchedules.moving , Trains.t_name "
"FROM TrainSchedules "
"JOIN Trains ON TrainSchedules.t_id = Trains.id "
"WHERE TrainSchedules.id = ?";
sqlite3_stmt *stmt = execute_command(query);
sqlite3_bind_int(stmt, 1, id);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const char *stations = (const char *)sqlite3_column_text(stmt, 0);
int moving = (int)sqlite3_column_int(stmt, 1);
const char *t_name = (const char *)sqlite3_column_text(stmt, 2);
char st_arr[MAX_STATIONS][MAX_STATION_NAME];
int c = split_string(stations,MAX_STATIONS, MAX_STATION_NAME, st_arr);
DoublyLinkedList *list = create_list();
for (int i = 0; i < c; i++){
insert_at_end(list, st_arr[i]);
}
printf("\n\033[1;31m%s\n", t_name);
printf(RESET_COLOR);
traverse_list(list, moving);
free_list(list);
} else {
printf(ERROR);
printf("\t[-] Make sure you have entered a valid id(Check option 1, 2, 3 to get id)\n");
printf(RESET_COLOR);
}
sqlite3_finalize(stmt);
}
void u_option6_7(int option, char *username){
char *query =
"SELECT "
" r.id AS reservation_id, "
" r.t_id AS train_id, "
" r.sch_id AS schedule_id, "