-
Notifications
You must be signed in to change notification settings - Fork 10
/
booking.c
1868 lines (1658 loc) · 59.7 KB
/
booking.c
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 <stdbool.h>
#include <ctype.h>
#include <time.h>
#define MAX_NAME_LENGTH 50
#define MAX_COMMENT_LENGTH 200
#define MAX_DESTINATION_LENGTH 50
#define MAX_SEATS 50
#define MAX_ROUTES 100
#define FILENAME "bookings.dat"
#define PARTIAL_BOOKING_FILENAME "partial_booking.dat"
#define PROGRESS_FILENAME "booking.det"
#define POINTS_PER_BOOKING 100
#define POINT_VALUE 100
#define MIN_BOOKING_PRICE 1800
struct Booking
{
int ticketID;
char name[MAX_NAME_LENGTH];
char currentLocation[MAX_DESTINATION_LENGTH];
char destination[MAX_DESTINATION_LENGTH];
int price;
char category[MAX_NAME_LENGTH];
int seats[MAX_SEATS];
int bookedSeat;
bool returnTicket;
char mode[MAX_NAME_LENGTH];
int numTravelers;
};
struct PartialBooking
{
bool inProgress;
struct Booking booking;
int stage; // 0: ticketID, 1: name, 2: destination, 3: price
};
struct Routs
{
char currentLocation[MAX_DESTINATION_LENGTH];
char destination[MAX_DESTINATION_LENGTH];
bool seatAvailability[MAX_SEATS]; // Array to track availability for this specific route
};
struct RouteSeatAvailability
{
char currentLocation[MAX_DESTINATION_LENGTH];
char destination[MAX_DESTINATION_LENGTH];
bool seatAvailability[MAX_SEATS]; // Array to track availability for this specific route
};
struct Feedback {
int ticketID;
char name[MAX_NAME_LENGTH];
int rating; // Rating out of 5
char comments[200];
};
struct PromoCode
{
char code[20];
int discount;
};
struct User {
char name[MAX_NAME_LENGTH];
int points;
};
struct Calendar {
char date[11];
char currentLocation[MAX_NAME_LENGTH];
char destination[MAX_NAME_LENGTH];
int standardPrice;
int vipPrice;
bool available;
};
const char *indianCities[] = {
"Mumbai", "Delhi", "Bangalore", "Hyderabad", "Chennai",
"Kolkata", "Jaipur", "Ahmedabad", "Pune", "Lucknow"};
// int ticketPrices[] = {1500, 1300, 1200, 1100, 1150, 1250, 1400, 1350, 1600, 900};
const char *ticketCategories[] = {"Standard", "VIP"}; // Ticket categories
int ticketPrices[][2] = {
// Prices for each category
{1500, 2500}, // Standard and VIP prices for Mumbai
{1300, 2300}, // Delhi
{1200, 2200}, // Bangalore
{1100, 2100}, // Hyderabad
{1150, 2150}, // Chennai
{1250, 2250}, // Kolkata
{1400, 2400}, // Jaipur
{1350, 2350}, // Ahmedabad
{1600, 2600}, // Pune
{900, 1900} // Lucknow
};
int busPrices[][2] = {
{800, 1500},
{700, 1200},
{600, 1100},
{550, 1000},
{600, 1100},
{650, 1200},
{700, 1300},
{650, 1250},
{800, 1400},
{500, 1000}
};
int Transport_Choice;
void clearInputBuffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void handleInput();
void recordFeedback(int ticketID, const char* name);
void TransportMode(struct PartialBooking* partial) {
while(1){
int Transport_Choice;
printf("\nSelect mode of transport:\n");
printf("1. Bus\n");
printf("2. Train\n");
printf("Enter your choice: ");
if (scanf("%d", &Transport_Choice) != 1 || (Transport_Choice < 1 || Transport_Choice > 2)) {
printf("Invalid choice. Please choose 1 for Bus or 2 for Train.\n");
clearInputBuffer();
continue;
}
clearInputBuffer();
strcpy(partial->booking.mode, (Transport_Choice == 1) ? "Bus" : "Train");
printf("You selected: %s\n", Transport_Choice == 1 ? "Bus" : "Train");
break;
}
}
void showMenu();
void handleInput();
const int numCities = sizeof(indianCities) / sizeof(indianCities[0]);
struct Routs routeSeatAvailability[MAX_ROUTES];
int routeCount = 0; // Keep track of unique routes
void printCentered(const char *str, int width)
{
int len = strlen(str);
int spaces = (width - len) / 2;
if (spaces < 0)
spaces = 0; // In case the string is longer than width
printf("%*s%s\n", spaces, "", str); // Print spaces and the string
}
struct Calendar tickets[MAX_ROUTES];
void Initilize_Calendar() {
int dayCount = 30;
for (int i = 0; i < dayCount; i++) {
snprintf(tickets[i].date, sizeof(tickets[i].date), "2024-10-%02d", i + 1); // Dates from Oct 01 to Oct 30
strcpy(tickets[i].currentLocation, indianCities[i % (sizeof(indianCities) / sizeof(indianCities[0]))]);
strcpy(tickets[i].destination, indianCities[(i + 1) % (sizeof(indianCities) / sizeof(indianCities[0]))]);
tickets[i].standardPrice = ticketPrices[i % (sizeof(ticketPrices) / sizeof(ticketPrices[0]))][0];
tickets[i].vipPrice = ticketPrices[i % (sizeof(ticketPrices) / sizeof(ticketPrices[0]))][1];
tickets[i].available = (i % 2 == 0); // Alternate availability for demonstration
}
}
void initializeSeats()
{
for (int i = 0; i < MAX_ROUTES; i++)
{
for (int j = 0; j < MAX_SEATS; j++)
{
routeSeatAvailability[i].seatAvailability[j] = true; // Initialize all seats to available
}
}
struct Booking booking;
FILE *file = fopen(FILENAME, "rb");
if (file != NULL)
{
while (fread(&booking, sizeof(struct Booking), 1, file) == 1)
{
for (int j = 0; j < MAX_SEATS && booking.seats[j] != 0; j++)
{
// Find the route and mark the seat as booked
for (int r = 0; r < routeCount; r++)
{
if (strcmp(routeSeatAvailability[r].currentLocation, booking.currentLocation) == 0 &&
strcmp(routeSeatAvailability[r].destination, booking.destination) == 0)
{
routeSeatAvailability[r].seatAvailability[booking.seats[j] - 1] = false;
break; // Break to prevent unnecessary looping
}
}
}
}
fclose(file);
}
}
void addRoute(const char *currentLocation, const char *destination)
{
// Initialize a new route if it doesn't exist
for (int i = 0; i < routeCount; i++)
{
if (strcmp(routeSeatAvailability[i].currentLocation, currentLocation) == 0 &&
strcmp(routeSeatAvailability[i].destination, destination) == 0)
{
return; // Route already exists
}
}
// If route does not exist, initialize it
strcpy(routeSeatAvailability[routeCount].currentLocation, currentLocation);
strcpy(routeSeatAvailability[routeCount].destination, destination);
for (int i = 0; i < MAX_SEATS; i++)
{
routeSeatAvailability[routeCount].seatAvailability[i] = true; // Set all seats to available
}
routeCount++;
}
bool isSeatAvailableForRoute(const char *currentCity, const char *destinationCity, int seatNum)
{
for (int i = 0; i < routeCount; i++)
{
if (strcmp(routeSeatAvailability[i].currentLocation, currentCity) == 0 &&
strcmp(routeSeatAvailability[i].destination, destinationCity) == 0)
{
return routeSeatAvailability[i].seatAvailability[seatNum - 1]; // Checking availability for specific route
}
}
return false; // If route is not found, return false
}
void bookSeatRoute(const char *currentCity, const char *destinationCity, int seatNum)
{
for (int i = 0; i < routeCount; i++)
{
if (strcmp(routeSeatAvailability[i].currentLocation, currentCity) == 0 &&
strcmp(routeSeatAvailability[i].destination, destinationCity) == 0)
{
routeSeatAvailability[i].seatAvailability[seatNum - 1] = false; // Mark the seat as booked
return; // Break once you've found the route
}
}
}
void freeSeatRoute(const char *currentCity, const char *destinationCity, int seatNum)
{
for (int i = 0; i < routeCount; i++)
{
if (strcmp(routeSeatAvailability[i].currentLocation, currentCity) == 0 &&
strcmp(routeSeatAvailability[i].destination, destinationCity) == 0)
{
routeSeatAvailability[i].seatAvailability[seatNum - 1] = true; // Mark the seat as available again
return; // Exit once you've freed the seat
}
}
}
void displayAvailableSeats(const char *currentCity, const char *destinationCity)
{
printf("\n +-------------------------------------------------------------------------------------------------------------------+\n");
printf(" | Available Seats for %s to %s: | \n", currentCity, destinationCity);
printf(" +-------------------------------------------------------------------------------------------------------------------+\n");
for (int i = 0; i < routeCount; i++)
{
if (strcmp(routeSeatAvailability[i].currentLocation, currentCity) == 0 &&
strcmp(routeSeatAvailability[i].destination, destinationCity) == 0)
{
printf(" | ");
bool foundSeat = false;
for (int j = 0; j < MAX_SEATS; j++)
{
if (routeSeatAvailability[i].seatAvailability[j])
{
printf("%d ", (j + 1)); // 1-indexed seat numbers
foundSeat = true;
}
}
if (!foundSeat)
{
printf("No available seats.");
}
printf("\n +-------------------------------------------------------------------------------------------------------------------+\n");
break; // Break once you've found the relevant route
}
}
}
bool isValidName(const char *name)
{
if (isspace(name[0]))
{
return false;
}
for (const char *p = name; *p; p++)
{
if (*p != ' ' && !isalpha(*p))
{
return false;
}
}
// Check if name contains at least one non-space character
for (const char *p = name; *p; p++)
{
if (*p != ' ')
{
return true;
}
}
return false;
}
void savePartialBooking(struct PartialBooking *partial)
{
FILE *file = fopen(PARTIAL_BOOKING_FILENAME, "wb");
if (file == NULL)
{
printf("Error: Unable to save partial booking. Please try again.\n");
return;
}
if (fwrite(partial, sizeof(struct PartialBooking), 1, file) != 1)
{
printf("Error: Failed to write partial booking data. Please try again.\n");
}
fclose(file);
}
bool loadPartialBooking(struct PartialBooking *partial)
{
FILE *file = fopen(PARTIAL_BOOKING_FILENAME, "rb");
if (file == NULL)
{
return false;
}
bool success = (fread(partial, sizeof(struct PartialBooking), 1, file) == 1);
fclose(file);
return success;
}
void saveBookingProgress(struct PartialBooking *partial)
{
FILE *file = fopen(PROGRESS_FILENAME, "wb");
if (file == NULL)
{
printf("Error: Unable to save booking progress. Please try again.\n");
return;
}
if (fwrite(partial, sizeof(struct PartialBooking), 1, file) != 1)
{
printf("Error: Failed to write booking progress data. Please try again.\n");
}
else
{
printf("Booking progress saved successfully.\n");
}
fclose(file);
}
bool loadBookingProgress(struct PartialBooking *partial)
{
FILE *file = fopen(PROGRESS_FILENAME, "rb");
if (file == NULL)
{
return false;
}
bool success = (fread(partial, sizeof(struct PartialBooking), 1, file) == 1);
fclose(file);
return success;
}
void generateReferenceNumber(char *refNumber, int ticketID)
{
time_t t;
srand((unsigned)time(&t));
int random = rand() % 1000;
sprintf(refNumber, "REF-%d-%03d", ticketID, random);
}
int unique_id()
{ // Automatically Generate Unique, Non-Repeating Ticket IDs #16 by Vasu
static int counter = 0;
time_t now = time(NULL);
return ((int)(now % 1234) | counter++);
}
void printBookingSummary(const struct PartialBooking *partial, int n)
{
printf("\n +--------------------------------------------------+\n");
printf(" | Booking Summary |\n");
printf(" +--------------------------------------------------+\n");
printf(" | Ticket ID: %-35d |\n", partial->booking.ticketID);
printf(" | Name: %-42s |\n", partial->booking.name);
printf(" | Current Location: %-30s |\n", partial->booking.currentLocation);
printf(" | Destination: %-34s |\n", partial->booking.destination);
printf(" | Mode: %-36s |\n", partial->booking.mode);
printf(" | Number of Travelers: %-27d |\n", n);
printf(" | Category: %-36s |\n", partial->booking.category);
printf(" | Seats Booked: ");
for (int j = 0; j < n; j++)
{
printf("%d ", partial->booking.seats[j]);
}
printf("%*s|\n", 39 - (2 * n), ""); // Adjust spacing based on seat numbers
printf(" | Price: Rs.%-35d |\n", partial->booking.price);
printf(" +--------------------------------------------------+\n");
}
void addBooking()
{
struct PartialBooking partial;
bool resuming = false;
char bookingReference[20]; // To store the generated booking reference number
char userName[MAX_NAME_LENGTH];
strcpy(userName, partial.booking.name);
TransportMode(&partial);
if (loadPartialBooking(&partial) && partial.inProgress)
{
printf("You have a partially completed booking. Do you want to resume? (1: Yes, 0: No): ");
int choice;
if (scanf("%d", &choice) != 1)
{
printf("Error: Invalid input. Starting a new booking.\n");
clearInputBuffer();
partial.inProgress = false;
partial.stage = 0;
}
else if (choice == 1)
{
resuming = true;
}
else
{
partial.inProgress = false;
partial.stage = 0;
}
clearInputBuffer();
}
else
{
partial.inProgress = true;
partial.stage = 0;
}
if (!resuming || partial.stage == 0)
{
partial.booking.ticketID = unique_id();
partial.stage = 1;
}
if (!resuming || partial.stage <= 1)
{
printf("\n");
// Get Name
do
{
printf("Enter Name (alphabets and spaces only, or 'pause' to pause): ");
if (fgets(partial.booking.name, MAX_NAME_LENGTH, stdin) == NULL)
{
printf("Error: Failed to read input. Please try again.\n");
continue;
}
partial.booking.name[strcspn(partial.booking.name, "\n")] = 0; // Remove newline
if (strcmp(partial.booking.name, "pause") == 0)
{
savePartialBooking(&partial);
printf("Booking paused. You can resume later.\n");
return;
}
if (!isValidName(partial.booking.name))
{
printf("Error: Invalid name. Please use only alphabets and spaces.\n");
continue;
}
break;
} while (1);
partial.stage = 2;
}
printf("\n");
if (!resuming || partial.stage <= 2)
{
int currentChoice;
char returnChoice[4]; // return ticket implementation
do
{
printf("Do you want return ticket(yes or no) : ");
if (fgets(returnChoice, sizeof(returnChoice), stdin) == NULL)
{
printf("Error: Failed to read input. Please try again.\n");
continue;
}
// Remove newline character that fgets adds
returnChoice[strcspn(returnChoice, "\n")] = '\0';
if (strcmp(returnChoice, "yes") == 0)
{
partial.booking.returnTicket = true;
printf("Return ticket selected.\n");
break;
}
else if (strcmp(returnChoice, "no") == 0)
{
partial.booking.returnTicket = false;
printf("One-way ticket selected.\n");
break;
}
else
{
printf("Invalid input. Please enter 'yes' or 'no'.\n");
}
} while (1);
// Current Location Input
printf("Enter your current location:\n");
for (int i = 0; i < numCities; i++)
{
printf("%d. %s\n", i + 1, indianCities[i]);
}
do
{
printf("Enter the number of your current location (1-%d), or 0 to pause: ", numCities);
if (scanf("%d", ¤tChoice) != 1 || currentChoice < 0 || currentChoice > numCities)
{
printf("Error: Invalid choice. Please enter a number between 0 and %d.\n", numCities);
clearInputBuffer();
continue;
}
if (currentChoice == 0)
{
savePartialBooking(&partial);
printf("Booking paused. You can resume later.\n");
return;
}
break;
} while (1);
strcpy(partial.booking.currentLocation, indianCities[currentChoice - 1]);
addRoute(partial.booking.currentLocation, ""); // Add route initialization
clearInputBuffer();
int choice;
printf("\n");
// Destination Input
printf("Select Destination:\n");
for (int i = 0; i < numCities; i++)
{
printf("%d. %s\n", i + 1, indianCities[i]);
}
do
{
printf("Enter the number of your destination (1-%d), or 0 to pause: ", numCities);
if (scanf("%d", &choice) != 1 || choice < 0 || choice > numCities)
{
printf("Error: Invalid choice. Please enter a number between 0 and %d.\n", numCities);
clearInputBuffer();
continue;
}
if (choice == 0)
{
savePartialBooking(&partial);
printf("Booking paused. You can resume later.\n");
return;
}
break;
} while (1);
printf("\n");
// Check for same pickup and destination
if (currentChoice - 1 == choice - 1)
{
printf("Pickup point and destination cannot be the same.\n");
return;
}
strcpy(partial.booking.destination, indianCities[choice - 1]);
// Add the route for new booking
addRoute(partial.booking.currentLocation, partial.booking.destination);
clearInputBuffer();
int n;
printf("\n");
do
{
// Traveler Count Input
printf("Enter how many travelers: ");
if (scanf("%d", &n) != 1 || n <= 0)
{
printf("Invalid input. Please enter a valid number of travelers (greater than zero).\n");
clearInputBuffer();
}
else
{
break;
}
} while (1);
clearInputBuffer();
int categoryChoice;
printf("\n");
// Ticket Category Selection
printf("Select Ticket Category:\n");
for (int i = 0; i < sizeof(ticketCategories) / sizeof(ticketCategories[0]); i++)
{
printf("%d. %s\n", i + 1, ticketCategories[i]);
}
printf("\n");
do
{
printf("Enter the number of your selected category (1-%ld): ", sizeof(ticketCategories) / sizeof(ticketCategories[0]));
if (scanf("%d", &categoryChoice) != 1 || categoryChoice < 1 || categoryChoice > sizeof(ticketCategories) / sizeof(ticketCategories[0]))
{
printf("Error: Invalid choice. Please enter a number between 1 and %ld.\n", sizeof(ticketCategories) / sizeof(ticketCategories[0]));
clearInputBuffer();
continue;
}else{
strcpy(partial.booking.category, ticketCategories[categoryChoice - 1]);
}
break;
} while (1);
// Initialize booked seats to 0
for (int i = 0; i < MAX_SEATS; i++)
{
partial.booking.seats[i] = 0; // Initialize unbooked seats with 0
}
for (int i = 0; i < n; i++)
{
int seatNum;
displayAvailableSeats(partial.booking.currentLocation, partial.booking.destination); // Show available seats before booking
do
{
printf("Enter seat number for traveler %d: ", i + 1);
if (scanf("%d", &seatNum) != 1 || seatNum < 1 || seatNum > MAX_SEATS || !isSeatAvailableForRoute(partial.booking.currentLocation, partial.booking.destination, seatNum))
{
printf("Error: Invalid or unavailable seat number. Please select an available seat (1-%d).\n", MAX_SEATS);
clearInputBuffer();
continue;
}
break;
} while (1);
partial.booking.seats[i] = seatNum; // Store the booked seat number
bookSeatRoute(partial.booking.currentLocation, partial.booking.destination, seatNum); // Mark seat as booked
}
if (Transport_Choice == 1) {
if (partial.booking.returnTicket) {
partial.booking.price = (busPrices[currentChoice - 1][categoryChoice - 1]) * 2;
}
else {
partial.booking.price = busPrices[currentChoice - 1][categoryChoice - 1];
}
} else {
if (partial.booking.returnTicket) {
partial.booking.price = (ticketPrices[currentChoice - 1][categoryChoice - 1]) * 2;
}
else {
partial.booking.price = ticketPrices[currentChoice - 1][categoryChoice - 1];
}
}
clearInputBuffer();
do
{
char promoChoice[4];
char enteredCode[20];
printf("Do you want to add Promo Code(yes or no) : ");
if (fgets(promoChoice, sizeof(promoChoice), stdin) == NULL)
{
printf("Error: Failed to read input. Please try again.\n");
continue;
}
// Remove newline character that fgets adds
promoChoice[strcspn(promoChoice, "\n")] = '\0';
struct PromoCode promoCodes[] = {{"CLUBGAMMA", 20}, {"CHARUSAT", 15}, {"HECTOBERFEST", 10}};
int numCodes = sizeof(promoCodes) / sizeof(promoCodes[0]);
int promoApplied = 0;
if (strcmp(promoChoice, "yes") == 0)
{
printf("Enter promotional code: ");
scanf("%s", enteredCode);
for (int i = 0; i < numCodes; i++)
{
if (strcmp(enteredCode, promoCodes[i].code) == 0)
{
int discountAmount = partial.booking.price * (promoCodes[i].discount / 100.0);
partial.booking.price -= discountAmount;
printf("Promotional code applied! Discount: %d%%\n", promoCodes[i].discount);
printf("New price after discount: %d\n", partial.booking.price);
promoApplied = 1;
break;
}
}
if (!promoApplied)
{
printf("Invalid promotional code.\n");
continue;
}
break;
}
else if (strcmp(promoChoice, "no") == 0)
{
break;
}
else
{
printf("Invalid input. Please enter 'yes' or 'no'.\n");
}
} while (1);
// Display summary before finalizing booking
printBookingSummary(&partial, n); // Improved summary display
char confirm[10];
printf("\n");
do
{
printf("Do you want to confirm this booking? (yes/no): ");
if (fgets(confirm, sizeof(confirm), stdin) == NULL)
{
printf("Error: Failed to read input. Please try again.\n");
continue; // Retry prompt in case of error
}
// Remove newline character that fgets adds
confirm[strcspn(confirm, "\n")] = '\0';
for (int i = 0; confirm[i]; i++)
{
confirm[i] = tolower(confirm[i]);
}
if (strcmp(confirm, "yes") == 0)
{
// Mark the seats as booked
for (int j = 0; j < n; j++)
{
bookSeatRoute(partial.booking.currentLocation, partial.booking.destination, partial.booking.seats[j]);
}
int totalBookingPrice = partial.booking.price;
update_reedem_Points(partial.booking.name, partial.booking.price);
int currentPoints = getPoints(partial.booking.name);
printf("Your current Reedem points: %d\n", currentPoints);
int pointsToRedeem;
printf("Do you want to redeem Rddem points? (Enter points to redeem, 0 to skip): ");
scanf("%d", &pointsToRedeem);
clearInputBuffer();
if (pointsToRedeem > currentPoints) {
printf("Error: You cannot reedem more points than you have. Available points: %d\n", currentPoints);
pointsToRedeem = 0;
}
if (pointsToRedeem > 0) {
int redeemedPoints = RedeemPoints(partial.booking.name, pointsToRedeem);
if (redeemedPoints > 0) {
partial.booking.price -= redeemedPoints * 100;
printf("Reedem points redeemed successfully! New price: Rs. %d\n", partial.booking.price);
} else {
printf("Insufficient Reedem points or error reedeming points.\n");
}
}
FILE *file = fopen(FILENAME, "ab");
if (file == NULL)
{
printf("Error: Unable to open bookings file. Booking not saved.\n");
return;
}
if (fwrite(&partial.booking, sizeof(struct Booking), 1, file) != 1)
{
printf("Error: Failed to write booking data. Please try again.\n");
}
else
{
generateReferenceNumber(bookingReference, partial.booking.ticketID);
recordFeedback(partial.booking.ticketID, partial.booking.name);
printf("\nBooking added successfully!\n");
const int width = 90;
printf("\n");
// Receipt
printCentered("+----------------------------------------------+", 50);
printCentered("| Receipt |", 50);
printCentered("+----------------------------------------------+", 50);
printf(" | Ticket ID: %-33d |\n", partial.booking.ticketID);
printf(" | Name: %-38s |\n", partial.booking.name);
printf(" | Current Location: %-26s |\n", partial.booking.currentLocation);
printf(" | Destination: %-31s |\n", partial.booking.destination);
printf(" | Number of Travelers: %-23d |\n", n);
printf(" | Category: %-34s |\n", partial.booking.category);
printf("| Seats Booked: ");
for (int j = 0; j < n; j++)
{
printf("%d ", partial.booking.seats[j]);
}
printf("%lu |\n", (width - strlen("| Seats Booked: ") - (n * 2) - 1), ""); // Calculate space to keep in line
printf(" | Price: Rs. %-32d |\n", partial.booking.price);
printCentered("+----------------------------------------------+", 50);
}
fclose(file);
// Clear partial booking
partial.inProgress = false;
savePartialBooking(&partial);
remove(PROGRESS_FILENAME);
break;
}
else if (strcmp(confirm, "no") == 0)
{
for (int j = 0; j < n; j++)
{
freeSeatRoute(partial.booking.currentLocation, partial.booking.destination, partial.booking.seats[j]);
}
printf("Booking cancelled. You can make changes or start over.\n");
return;
}
else
{
printf("Invalid input. Please enter 'yes' or 'no'.\n");
}
} while (1);
}
}
int getPoints(const char* userName) {
struct User user;
FILE *file = fopen("reedem_points.dat", "rb");
if (file == NULL) {
file = fopen("reedem_points.dat", "wb");
if (file == NULL) {
printf("Error: Unable to create reedem points file.\n");
return 0;
}
fclose(file);
return 0;
}
while (fread(&user, sizeof(struct User), 1, file) == 1) {
if (strcmp(user.name, userName) == 0) {
fclose(file);
return user.points;
}
}
fclose(file);
return 0;
}
void update_reedem_Points(const char* userName, int bookingPrice) {
if (bookingPrice > 1800) {
struct User user;
FILE *file = fopen("reedem_points.dat", "rb+");
if (file == NULL) {
file = fopen("reedem_points.dat", "wb+");
if (file == NULL) {
printf("Error: Unable to create Reedem points file.\n");
return;
}
}
int userFound = 0;
while (fread(&user, sizeof(struct User), 1, file) == 1) {
if (strcmp(user.name, userName) == 0) {
user.points += 10;
fseek(file, -sizeof(struct User), SEEK_CUR);
fwrite(&user, sizeof(struct User), 1, file);
printf("Reedem points updated! New total: %d\n", user.points);
userFound = 1;
break;
}
}
if (!userFound) {
// If user not found, create a new entry
strncpy(user.name, userName, MAX_NAME_LENGTH);
user.points = 10; // Start with 10 points for the first booking
fwrite(&user, sizeof(struct User), 1, file); // Add new user record
printf("New user created. Reedem points: %d\n", user.points);
}
fclose(file);
} else {
printf("Booking price is not high enough to earn Reedem points.\n");
}
}
int RedeemPoints(const char* userName, int pointsToRedeem) {
struct User user;
FILE *file = fopen("reedem_points.dat", "rb+");
if (file == NULL) {
printf("Error: Unable to open reedem points file.\n");
return 0;
}
while (fread(&user, sizeof(struct User), 1, file) == 1) {
if (strcmp(user.name, userName) == 0) {
if (user.points >= pointsToRedeem) {
user.points -= pointsToRedeem;
fseek(file, -sizeof(struct User), SEEK_CUR);
fwrite(&user, sizeof(struct User), 1, file);
fclose(file);
return pointsToRedeem;
} else {
fclose(file);
return 0; // Not enough points
}
}
}
fclose(file);
return 0;
}
void displayPoints(const char* userName) {
struct User user;
FILE *file = fopen("reedem_points.dat", "rb");
if (file == NULL) {
printf("Error: Unable to open Reedem points file.\n");
return;
}
while (fread(&user, sizeof(struct User), 1, file) == 1) {
if (strcmp(user.name, userName) == 0) {
printf("Your current Reedem points: %d\n", user.points);
fclose(file);
return;
}
}
fclose(file);
printf("No Reedem points found for this user.\n");
}
void displayBookings()
{
int Transport_Choice;
while(1){
printf("\nSelect mode of transport:\n");
printf("1. Bus\n");
printf("2. Train\n");
printf("Enter your choice: ");
if (scanf("%d", &Transport_Choice) != 1 || (Transport_Choice < 1 || Transport_Choice > 2)) {
printf("Invalid choice. Please choose 1 for Bus or 2 for Train.\n");
clearInputBuffer();
continue;
}
clearInputBuffer();
printf("You selected: %s\n", Transport_Choice == 1 ? "Bus" : "Train");
break;
}
struct Booking booking;
FILE *file = fopen(FILENAME, "rb");
if (file == NULL)
{
printf("No bookings found or error opening file.\n");
return;
}
// Adjusted header to include "Booked Seat"
printf("\n +----------------------------------------------------------------------------------------------------------+\n");
printf(" | %-10s %-20s %-20s %-20s %-10s %-6.5s %10s|\n", "Ticket ID", "Name", "Current Location", "Destination", "Booked Seat", "Price", "Mode");
printf(" +----------------------------------------------------------------------------------------------------------+\n");