└── main.c /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Macros 6 | #define MAX_BUSES 5 7 | #define MAX_SEATS 10 8 | #define NAME_LENGTH 30 9 | #define GENDER_LENGTH 10 10 | #define FOOD_LENGTH 50 11 | #define LOCATION_LENGTH 50 12 | #define USERNAME_LENGTH 20 13 | #define PASSWORD_LENGTH 20 14 | #define PAYMENT_METHOD_LENGTH 20 15 | 16 | // Enum to represent seat status 17 | enum SeatStatus { 18 | AVAILABLE, 19 | BOOKED 20 | }; 21 | 22 | // Structure to represent a reservation 23 | struct Reservation { 24 | char passengerName[NAME_LENGTH]; 25 | char gender[GENDER_LENGTH]; 26 | int seatNumber; 27 | int busNumber; 28 | char selectedFood[FOOD_LENGTH]; 29 | char timeOfDay[20]; // Added field for time of day 30 | char paymentMethod[PAYMENT_METHOD_LENGTH]; 31 | }; 32 | 33 | // Structure to represent bus information 34 | struct BusInfo { 35 | char startLocation[LOCATION_LENGTH]; 36 | char endLocation[LOCATION_LENGTH]; 37 | enum SeatStatus seatStatus[MAX_SEATS]; 38 | }; 39 | 40 | // Structure to represent user information 41 | struct UserInfo { 42 | char username[USERNAME_LENGTH]; 43 | char password[PASSWORD_LENGTH]; 44 | }; 45 | 46 | // Function prototypes 47 | void displayMenu(); 48 | void initializeBuses(struct BusInfo busInfo[MAX_BUSES]); 49 | void showAvailableSeats(struct BusInfo busInfo[MAX_BUSES]); 50 | void showAvailableFoods(); 51 | void signUp(struct UserInfo* userInfo); 52 | int login(struct UserInfo* userInfo); 53 | void makeReservation(struct BusInfo busInfo[MAX_BUSES], struct Reservation** reservations, int* reservationCount); 54 | void displayReservations(struct Reservation* reservations, int reservationCount, struct BusInfo busInfo[MAX_BUSES]); 55 | 56 | int main() { 57 | // Get user sign-up information 58 | struct UserInfo userInfo; 59 | signUp(&userInfo); 60 | 61 | // Arrays to represent reservations 62 | struct Reservation* reservations = NULL; 63 | int reservationCount = 0; 64 | 65 | // Bus information 66 | struct BusInfo busInfo[MAX_BUSES] = { 67 | {"Karur", "Trichy"}, 68 | {"Karur", "Chennai"}, 69 | {"Karur", "Coimbatore"}, 70 | {"Karur", "Vadapalani"}, 71 | {"Karur", "Goa"} 72 | }; 73 | 74 | // Initialize buses 75 | initializeBuses(busInfo); 76 | 77 | // Login 78 | if (!login(&userInfo)) { 79 | printf("Login failed. Exiting the program.\n"); 80 | return 1; 81 | } 82 | 83 | int choice; 84 | do { 85 | displayMenu(); 86 | printf("Enter your choice: "); 87 | scanf("%d", &choice); 88 | 89 | switch (choice) { 90 | case 1: 91 | showAvailableSeats(busInfo); 92 | break; 93 | case 2: 94 | makeReservation(busInfo, &reservations, &reservationCount); 95 | break; 96 | case 3: 97 | displayReservations(reservations, reservationCount, busInfo); 98 | break; 99 | case 4: 100 | printf("Exiting the program.\n"); 101 | break; 102 | default: 103 | printf("Invalid choice. Please try again.\n"); 104 | } 105 | } while (choice != 4); 106 | 107 | // Free dynamically allocated memory 108 | free(reservations); 109 | 110 | return 0; 111 | } 112 | 113 | void displayMenu() { 114 | printf("\n----- Bus Reservation System Menu -----\n"); 115 | printf("1. Show available seats\n"); 116 | printf("2. Make a reservation\n"); 117 | printf("3. Display reservations\n"); 118 | printf("4. Exit\n"); 119 | } 120 | 121 | void initializeBuses(struct BusInfo busInfo[MAX_BUSES]) { 122 | // Initialize seat status for each bus 123 | for (int i = 0; i < MAX_BUSES; ++i) { 124 | for (int j = 0; j < MAX_SEATS; ++j) { 125 | busInfo[i].seatStatus[j] = AVAILABLE; 126 | } 127 | } 128 | } 129 | 130 | void showAvailableSeats(struct BusInfo busInfo[MAX_BUSES]) { 131 | printf("\n----- Available Seats -----\n"); 132 | for (int i = 0; i < MAX_BUSES; ++i) { 133 | printf("Bus %d (%s to %s):\n", i + 1, busInfo[i].startLocation, busInfo[i].endLocation); 134 | for (int j = 0; j < MAX_SEATS; ++j) { 135 | printf("%d. %s\t", j + 1, busInfo[i].seatStatus[j] == AVAILABLE ? "Available" : "Booked"); 136 | } 137 | printf("\n"); 138 | } 139 | // Show available foods 140 | showAvailableFoods(); 141 | } 142 | 143 | void showAvailableFoods() { 144 | printf("\n----- Available Foods -----\n"); 145 | printf("Morning:\n"); 146 | printf("1. Sandwich\n"); 147 | printf("2. Cereal\n"); 148 | 149 | printf("\nAfternoon:\n"); 150 | printf("3. Pizza\n"); 151 | printf("4. Salad\n"); 152 | 153 | printf("\nNight:\n"); 154 | printf("5. Spaghetti\n"); 155 | printf("6. Soup\n"); 156 | // Add more food items as needed 157 | } 158 | 159 | void signUp(struct UserInfo* userInfo) { 160 | printf("\n----- Sign Up -----\n"); 161 | printf("Enter username: "); 162 | scanf("%s", userInfo->username); 163 | 164 | printf("Enter password: "); 165 | scanf("%s", userInfo->password); 166 | } 167 | 168 | int login(struct UserInfo* userInfo) { 169 | char username[USERNAME_LENGTH]; 170 | char password[PASSWORD_LENGTH]; 171 | 172 | printf("\n----- Login -----\n"); 173 | printf("Enter username: "); 174 | scanf("%s", username); 175 | 176 | printf("Enter password: "); 177 | scanf("%s", password); 178 | 179 | // Check if entered username and password match the sign-up information 180 | if (strcmp(username, userInfo->username) == 0 && strcmp(password, userInfo->password) == 0) { 181 | printf("Login successful.\n"); 182 | return 1; 183 | } else { 184 | printf("Login failed.\n"); 185 | return 0; 186 | } 187 | } 188 | 189 | void makeReservation(struct BusInfo busInfo[MAX_BUSES], struct Reservation** reservations, int* reservationCount) { 190 | // Allocate memory for the reservation 191 | *reservations = (struct Reservation*)realloc(*reservations, (*reservationCount + 1) * sizeof(struct Reservation)); 192 | 193 | if (*reservations == NULL) { 194 | printf("Memory allocation error.\n"); 195 | exit(EXIT_FAILURE); 196 | } 197 | 198 | // Get user input for reservation 199 | printf("\nEnter gender (Male/Female/Other): "); 200 | scanf("%s", (*reservations)[*reservationCount].gender); // Added line for gender 201 | printf("Enter passenger name: "); 202 | scanf("%s", (*reservations)[*reservationCount].passengerName); 203 | 204 | // Display available buses 205 | printf("\nAvailable Buses:\n"); 206 | for (int i = 0; i < MAX_BUSES; ++i) { 207 | printf("Bus %d (%s to %s):\n", i + 1, busInfo[i].startLocation, busInfo[i].endLocation); 208 | for (int j = 0; j < MAX_SEATS; ++j) { 209 | printf("%d. %s\t", j + 1, busInfo[i].seatStatus[j] == AVAILABLE ? "Available" : "Booked"); 210 | } 211 | printf("\n"); 212 | } 213 | 214 | // Get user input for bus number 215 | printf("Enter bus number: "); 216 | scanf("%d", &(*reservations)[*reservationCount].busNumber); 217 | 218 | // Check if the selected bus number is valid 219 | if ((*reservations)[*reservationCount].busNumber < 1 || (*reservations)[*reservationCount].busNumber > MAX_BUSES) { 220 | printf("Invalid bus number. Please try again.\n"); 221 | return; 222 | } 223 | 224 | // Display available seats for the selected bus 225 | printf("\nAvailable Seats for Bus %d (%s to %s): ", (*reservations)[*reservationCount].busNumber, busInfo[(*reservations)[*reservationCount].busNumber - 1].startLocation, busInfo[(*reservations)[*reservationCount].busNumber - 1].endLocation); 226 | for (int j = 0; j < MAX_SEATS; ++j) { 227 | printf("%d. %s\t", j + 1, busInfo[(*reservations)[*reservationCount].busNumber - 1].seatStatus[j] == AVAILABLE ? "Available" : "Booked"); 228 | } 229 | printf("\n"); 230 | 231 | // Get user input for seat number 232 | printf("Enter seat number: "); 233 | scanf("%d", &(*reservations)[*reservationCount].seatNumber); 234 | 235 | // Check if the selected seat number is valid 236 | if ((*reservations)[*reservationCount].seatNumber < 1 || (*reservations)[*reservationCount].seatNumber > MAX_SEATS || busInfo[(*reservations)[*reservationCount].busNumber - 1].seatStatus[(*reservations)[*reservationCount].seatNumber - 1] == BOOKED) { 237 | printf("Invalid seat number or seat already reserved. Please try again.\n"); 238 | return; 239 | } 240 | 241 | // Reserve the seat 242 | busInfo[(*reservations)[*reservationCount].busNumber - 1].seatStatus[(*reservations)[*reservationCount].seatNumber - 1] = BOOKED; 243 | 244 | // Get user input for selected food 245 | printf("Select food (enter the corresponding number): "); 246 | int foodChoice; 247 | scanf("%d", &foodChoice); 248 | switch (foodChoice) { 249 | case 1: 250 | strcpy((*reservations)[*reservationCount].selectedFood, "Sandwich"); 251 | strcpy((*reservations)[*reservationCount].timeOfDay, "Morning"); 252 | break; 253 | case 2: 254 | strcpy((*reservations)[*reservationCount].selectedFood, "Cereal"); 255 | strcpy((*reservations)[*reservationCount].timeOfDay, "Morning"); 256 | break; 257 | case 3: 258 | strcpy((*reservations)[*reservationCount].selectedFood, "Pizza"); 259 | strcpy((*reservations)[*reservationCount].timeOfDay, "Afternoon"); 260 | break; 261 | case 4: 262 | strcpy((*reservations)[*reservationCount].selectedFood, "Salad"); 263 | strcpy((*reservations)[*reservationCount].timeOfDay, "Afternoon"); 264 | break; 265 | case 5: 266 | strcpy((*reservations)[*reservationCount].selectedFood, "Spaghetti"); 267 | strcpy((*reservations)[*reservationCount].timeOfDay, "Night"); 268 | break; 269 | case 6: 270 | strcpy((*reservations)[*reservationCount].selectedFood, "Soup"); 271 | strcpy((*reservations)[*reservationCount].timeOfDay, "Night"); 272 | break; 273 | default: 274 | strcpy((*reservations)[*reservationCount].selectedFood, "Unknown"); 275 | strcpy((*reservations)[*reservationCount].timeOfDay, "Unknown"); 276 | break; 277 | } 278 | 279 | // Get user input for payment method 280 | printf("\nSelect payment method:\n"); 281 | printf("1. Online\n"); 282 | printf("2. Offline\n"); 283 | printf("Enter your choice: "); 284 | int paymentChoice; 285 | scanf("%d", &paymentChoice); 286 | 287 | if (paymentChoice == 1) { 288 | // For online payments, display additional options 289 | printf("Select online payment method (1. Paytm / 2. GPay / 3. PhonePe): "); 290 | int onlinePaymentChoice; 291 | scanf("%d", &onlinePaymentChoice); 292 | 293 | switch (onlinePaymentChoice) { 294 | case 1: 295 | printf("Payment successful via Paytm.\n"); 296 | break; 297 | case 2: 298 | printf("Payment successful via GPay.\n"); 299 | break; 300 | case 3: 301 | printf("Payment successful via PhonePe.\n"); 302 | break; 303 | default: 304 | printf("Invalid online payment method. Payment failed.\n"); 305 | break; 306 | } 307 | } else { 308 | printf("Payment successful.\n"); 309 | } 310 | 311 | printf("Reservation successful.\n"); 312 | 313 | (*reservationCount)++; 314 | } 315 | 316 | void displayReservations(struct Reservation* reservations, int reservationCount, struct BusInfo busInfo[MAX_BUSES]) { 317 | printf("\n----- Reservations -----\n"); 318 | printf("Passenger Name\tGender\tBus Number\tSeat Number\tSelected Food\tTime of Day\tPayment Method\n"); 319 | 320 | for (int i = 0; i < reservationCount; ++i) { 321 | printf("%s\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\t\t%s\n", 322 | reservations[i].passengerName, 323 | reservations[i].gender, 324 | reservations[i].busNumber, 325 | reservations[i].seatNumber, 326 | reservations[i].selectedFood, 327 | reservations[i].timeOfDay, 328 | reservations[i].paymentMethod); 329 | } 330 | } 331 | 332 | --------------------------------------------------------------------------------