-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusBookingSystem.cpp
588 lines (516 loc) · 17.5 KB
/
BusBookingSystem.cpp
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
// main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <iomanip> // for std::setw
#include <algorithm> // for std::find_if
#include <limits> // for clearing std::cin
/**
* @file main.cpp
* @brief Console-based Bus Booking System
*
* This application allows users to manage bus reservations, including installing new buses,
* reserving seats, canceling reservations, viewing bus details, and searching buses by route.
*/
/**
* @brief ANSI escape codes to clear the terminal screen.
*
* This function clears the terminal screen using ANSI escape codes, which are supported by
* many modern terminals.
*/
void clearScreen() {
std::cout << "\033[2J\033[H";
}
/**
* @brief Utility function to draw a horizontal line of repeated characters.
*
* @param ch The character to repeat.
* @param length The number of times to repeat the character. Default is 80.
*/
void printLine(char ch, int length = 80) {
for(int i = 0; i < length; ++i) std::cout << ch;
std::cout << std::endl;
}
/**
* @struct Seat
* @brief Represents a single seat in the bus.
*
* Each seat has a passenger name (or "Empty" if unreserved) and a fare price.
*/
struct Seat {
std::string passengerName; /**< Name of the passenger. "Empty" if seat is vacant. */
double fare; /**< Fare price for the seat. */
/**
* @brief Default constructor initializes seat as empty with zero fare.
*/
Seat() : passengerName("Empty"), fare(0.0) {}
};
/**
* @class Bus
* @brief Represents a bus with its details and seat information.
*/
class Bus {
private:
std::string busNumber; /**< Unique identifier for the bus. */
std::string driverName; /**< Name of the bus driver. */
std::string arrivalTime; /**< Arrival time of the bus. */
std::string departureTime; /**< Departure time of the bus. */
std::string from; /**< Origin location. */
std::string to; /**< Destination location. */
/**
* @brief 2D vector representing seats arranged in rows and columns.
*
* 8 rows with 4 seats each (total 32 seats).
*/
std::vector<std::vector<Seat>> seats;
public:
/**
* @brief Default constructor initializes seats with default fares.
*/
Bus();
/**
* @brief Install a new bus by reading user input.
*
* Allows the user to input bus details. If the user enters "0" or leaves a prompt empty,
* the installation is canceled.
*/
void install();
/**
* @brief Reserve a seat on the bus.
*
* Allows the user to reserve a seat by specifying the bus number and seat number.
* Ensures that the seat is available before booking.
*/
void allotment();
/**
* @brief Cancel a seat reservation.
*
* Allows the user to cancel a previously reserved seat by specifying the bus number
* and seat number.
*/
void cancelSeat();
/**
* @brief Display detailed information about the bus, including seat map.
*/
void show() const;
/**
* @brief Display a concise summary of the bus information.
*/
void printBasicInfo() const;
/**
* @brief Check if the bus matches the given route.
*
* @param origin Origin location to match.
* @param dest Destination location to match.
* @return true If the bus matches the route.
* @return false Otherwise.
*/
bool matchesRoute(const std::string& origin, const std::string& dest) const;
/**
* @brief Get the bus number.
*
* @return std::string The bus number.
*/
std::string getBusNumber() const;
/**
* @brief Friend function to allow access to private members for seat reservation and cancellation.
*
* @param seatNumber The seat number to access.
* @return Seat& Reference to the specified seat.
*/
Seat& getSeat(int seatNumber);
};
/**
* @brief Global container storing all buses.
*/
std::vector<Bus> busList;
/****************************************
* Bus Class Method Definitions *
****************************************/
Bus::Bus()
: seats(8, std::vector<Seat>(4)) // 8 rows, each row has 4 Seat objects
{
// Initialize default seat fares (e.g., 300.0 for all seats)
for(auto &row : seats) {
for(auto &seat : row) {
seat.fare = 300.0;
}
}
}
void Bus::install() {
std::cout << "Enter bus number (or 0 to cancel): ";
std::getline(std::cin, busNumber);
if(busNumber == "0" || busNumber.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
return;
}
std::cout << "Enter driver's name (or 0 to cancel): ";
std::getline(std::cin, driverName);
if(driverName == "0" || driverName.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
return;
}
std::cout << "Enter arrival time (or 0 to cancel): ";
std::getline(std::cin, arrivalTime);
if(arrivalTime == "0" || arrivalTime.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
driverName.clear();
return;
}
std::cout << "Enter departure time (or 0 to cancel): ";
std::getline(std::cin, departureTime);
if(departureTime == "0" || departureTime.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
driverName.clear();
arrivalTime.clear();
return;
}
std::cout << "Enter origin (From) (or 0 to cancel): ";
std::getline(std::cin, from);
if(from == "0" || from.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
driverName.clear();
arrivalTime.clear();
departureTime.clear();
return;
}
std::cout << "Enter destination (To) (or 0 to cancel): ";
std::getline(std::cin, to);
if(to == "0" || to.empty()) {
std::cout << "Installation cancelled.\n";
busNumber.clear();
driverName.clear();
arrivalTime.clear();
departureTime.clear();
from.clear();
return;
}
std::cout << "\nBus installed successfully!\n";
}
void Bus::allotment() {
std::cout << "Enter bus number to reserve seat (or 0 to cancel): ";
std::string number;
std::getline(std::cin, number);
if(number == "0" || number.empty()) {
std::cout << "Operation cancelled.\n";
return;
}
// Find the bus in the global busList
auto it = std::find_if(busList.begin(), busList.end(),
[&](const Bus &b){ return b.getBusNumber() == number; });
if(it == busList.end()) {
std::cout << "Bus not found. Please try again.\n";
return;
}
// Ask for seat number
std::cout << "Enter seat number (1-32) (or 0 to cancel): ";
int seatNumber;
if(!(std::cin >> seatNumber)) {
std::cout << "Invalid input. Operation cancelled.\n";
// Clear the error flag and ignore invalid input
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
// Clear buffer
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(seatNumber == 0) {
std::cout << "Operation cancelled.\n";
return;
}
if(seatNumber < 1 || seatNumber > 32) {
std::cout << "Invalid seat number. Please enter a number between 1 and 32.\n";
return;
}
// Convert seatNumber to row and column
int row = (seatNumber - 1) / 4;
int col = (seatNumber - 1) % 4;
// Check if seat is already booked
if(it->seats[row][col].passengerName != "Empty") {
std::cout << "That seat is already reserved by " << it->seats[row][col].passengerName << "!\n";
return;
}
// If empty, ask for passenger name
std::cout << "Enter passenger's name (or 0 to cancel): ";
std::string passenger;
std::getline(std::cin, passenger);
if(passenger == "0" || passenger.empty()) {
std::cout << "Operation cancelled.\n";
return;
}
// Book seat
it->seats[row][col].passengerName = passenger;
double cost = it->seats[row][col].fare;
std::cout << "Seat " << seatNumber << " reserved successfully for "
<< passenger << ".\n"
<< "Fare: Rs. " << std::fixed << std::setprecision(2) << cost << "\n";
}
void Bus::cancelSeat() {
std::cout << "Enter bus number to cancel a seat (or 0 to cancel): ";
std::string number;
std::getline(std::cin, number);
if(number == "0" || number.empty()) {
std::cout << "Operation cancelled.\n";
return;
}
// Find the bus in the global busList
auto it = std::find_if(busList.begin(), busList.end(),
[&](const Bus &b){ return b.getBusNumber() == number; });
if(it == busList.end()) {
std::cout << "Bus not found.\n";
return;
}
std::cout << "Enter seat number to cancel (1-32) (or 0 to cancel): ";
int seatNumber;
if(!(std::cin >> seatNumber)) {
std::cout << "Invalid input. Operation cancelled.\n";
// Clear the error flag and ignore invalid input
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(seatNumber == 0) {
std::cout << "Operation cancelled.\n";
return;
}
if(seatNumber < 1 || seatNumber > 32) {
std::cout << "Invalid seat number. Please enter a number between 1 and 32.\n";
return;
}
int row = (seatNumber - 1) / 4;
int col = (seatNumber - 1) % 4;
if(it->seats[row][col].passengerName == "Empty") {
std::cout << "This seat is already empty.\n";
return;
}
// Confirm cancellation
std::cout << "Are you sure you want to cancel the reservation for seat "
<< seatNumber << " (Passenger: " << it->seats[row][col].passengerName << ")? (y/n): ";
char confirm;
std::cin >> confirm;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(tolower(confirm) != 'y') {
std::cout << "Cancellation aborted.\n";
return;
}
// Cancel the booking
it->seats[row][col].passengerName = "Empty";
std::cout << "Reservation for seat " << seatNumber << " has been cancelled.\n";
}
void Bus::show() const {
printLine('*');
std::cout << "Bus Number : " << busNumber << "\n"
<< "Driver : " << driverName << "\n"
<< "Arrival Time : " << arrivalTime << "\n"
<< "Departure Time: " << departureTime << "\n"
<< "From : " << from << "\n"
<< "To : " << to << "\n";
printLine('*');
int seatIndex = 1;
int emptyCount = 0;
// Print seats in a grid
for(int i = 0; i < 8; ++i) {
std::cout << "\nRow " << (i+1) << ":\n";
for(int j = 0; j < 4; ++j) {
std::cout << " Seat " << std::setw(2) << seatIndex << ": ";
if(seats[i][j].passengerName == "Empty") {
std::cout << "Empty (Rs. " << std::fixed << std::setprecision(2) << seats[i][j].fare << ")";
emptyCount++;
} else {
std::cout << seats[i][j].passengerName
<< " (Rs. " << std::fixed << std::setprecision(2) << seats[i][j].fare << ")";
}
std::cout << "\n";
seatIndex++;
}
}
std::cout << "\nTotal empty seats: " << emptyCount << "\n\n";
}
void Bus::printBasicInfo() const {
std::cout << "Bus Number : " << busNumber << "\n"
<< "Driver : " << driverName << "\n"
<< "Arrival Time : " << arrivalTime << "\n"
<< "Departure Time: " << departureTime << "\n"
<< "Route : " << from << " -> " << to << "\n";
}
bool Bus::matchesRoute(const std::string& origin, const std::string& dest) const {
return (from == origin && to == dest);
}
std::string Bus::getBusNumber() const {
return busNumber;
}
Seat& Bus::getSeat(int seatNumber) {
int row = (seatNumber - 1) / 4;
int col = (seatNumber - 1) % 4;
return seats[row][col];
}
/****************************************
* Other Global Functions *
****************************************/
/**
* @brief Display all buses available in the system.
*
* Iterates through the global bus list and prints basic information for each bus.
*/
void showAllBuses() {
if(busList.empty()) {
std::cout << "No buses available.\n";
return;
}
for(const auto &b : busList) {
// If busNumber is empty, skip printing (shouldn't be in the list)
if(b.getBusNumber().empty()) continue;
printLine('*');
b.printBasicInfo();
printLine('*');
}
}
/**
* @brief Search for buses based on origin and destination.
*
* Prompts the user to enter origin and destination, then displays matching buses.
*/
void searchBusesByRoute() {
if(busList.empty()) {
std::cout << "No buses available.\n";
return;
}
std::cout << "Enter origin (From): ";
std::string origin;
std::getline(std::cin, origin);
// Check cancel
if(origin == "0" || origin.empty()) {
std::cout << "Search cancelled.\n";
return;
}
std::cout << "Enter destination (To): ";
std::string destination;
std::getline(std::cin, destination);
if(destination == "0" || destination.empty()) {
std::cout << "Search cancelled.\n";
return;
}
bool foundAny = false;
for(const auto &b : busList) {
if(b.matchesRoute(origin, destination)) {
foundAny = true;
printLine('=');
b.printBasicInfo();
printLine('=');
}
}
if(!foundAny) {
std::cout << "No matching buses found for route "
<< origin << " -> " << destination << ".\n";
}
}
/****************************************
* Main Function *
****************************************/
/**
* @brief Entry point of the Bus Booking System.
*
* Presents a menu-driven interface for users to interact with the system.
* Continuously loops until the user chooses to exit.
*
* @return int Exit status.
*/
int main() {
clearScreen();
while(true) {
std::cout << "\n\t\t===== Bus Booking System =====\n"
<< "\t\t1. Install New Bus\n"
<< "\t\t2. Reserve a Seat\n"
<< "\t\t3. Show Bus Details\n"
<< "\t\t4. Show All Buses Available\n"
<< "\t\t5. Cancel (Remove) a Seat\n"
<< "\t\t6. Search Buses by Route\n"
<< "\t\t7. Exit\n\n"
<< "\t\tEnter your choice:-> ";
int choice;
if(!(std::cin >> choice)) {
std::cout << "Invalid input. Please enter a number between 1 and 7.\n";
// Clear the error flag and ignore invalid input
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
// Clear newline
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch(choice) {
case 1: {
Bus newBus;
newBus.install();
// Only add if busNumber is valid
if(!newBus.getBusNumber().empty()) {
busList.push_back(newBus);
}
break;
}
case 2: {
if(busList.empty()) {
std::cout << "No buses installed. Please install a bus first.\n";
} else {
// Handle seat allotment using the Bus class
Bus dummy; // Temporary object to call the method
dummy.allotment();
}
break;
}
case 3: {
if(busList.empty()) {
std::cout << "No buses installed yet.\n";
break;
}
std::cout << "Enter bus number to show details (or 0 to cancel): ";
std::string number;
std::getline(std::cin, number);
if(number == "0" || number.empty()) {
std::cout << "Operation cancelled.\n";
break;
}
auto it = std::find_if(busList.begin(), busList.end(),
[&](const Bus &b){ return b.getBusNumber() == number; });
if(it == busList.end()) {
std::cout << "Bus not found.\n";
} else {
it->show();
}
break;
}
case 4: {
showAllBuses();
break;
}
case 5: {
if(busList.empty()) {
std::cout << "No buses installed yet.\n";
} else {
// Handle seat cancellation using the Bus class
Bus dummy; // Temporary object to call the method
dummy.cancelSeat();
}
break;
}
case 6: {
searchBusesByRoute();
break;
}
case 7: {
std::cout << "Exiting... Have a nice day!\n";
return 0;
}
default: {
std::cout << "Invalid choice. Please enter a number between 1 and 7.\n";
break;
}
}
}
return 0;
}