Zohol 1
Zohol 1
#include <vector>
#include <limits>
#include <algorithm>
class Seat {
private:
bool booked;
public:
Seat() : booked(false) {}
void book() {
booked = true;
}
void cancelBooking() {
booked = false;
}
};
class Coach {
private:
string type;
vector<Seat> seats;
public:
Coach(const string& t, int numSeats) : type(t) {
seats.resize(numSeats);
}
class RailwayReservationSystem {
private:
Coach acCoach;
Coach nonAcCoach;
Coach seater;
public:
RailwayReservationSystem() : acCoach("AC", 60), nonAcCoach("Non-AC", 60), seater("Seater", 60) {}
void bookTicket() {
int coachType;
cout << "Enter Coach Type (1 for AC, 2 for Non-AC, 3 for Seater): ";
cin >> coachType;
if (coachType < 1 || coachType > 3) {
cout << "Invalid Coach Type! Please enter a valid option." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
int totalSeats;
vector<int> availableSeats;
switch (coachType) {
case 1:
totalSeats = acCoach.getNumSeats();
for (int i = 0; i < totalSeats; ++i) {
if (!acCoach.getSeat(i).isBooked()) {
availableSeats.push_back(i + 1);
}
}
break;
case 2:
totalSeats = nonAcCoach.getNumSeats();
for (int i = 0; i < totalSeats; ++i) {
if (!nonAcCoach.getSeat(i).isBooked()) {
availableSeats.push_back(i + 1);
}
}
break;
case 3:
totalSeats = seater.getNumSeats();
for (int i = 0; i < totalSeats; ++i) {
if (!seater.getSeat(i).isBooked()) {
availableSeats.push_back(i + 1);
}
}
break;
}
if (availableSeats.empty()) {
cout << "No available seats in the selected coach." << endl;
return;
}
int seatNumber;
cout << "Enter Seat Number: ";
cin >> seatNumber;
if (find(availableSeats.begin(), availableSeats.end(), seatNumber) == availableSeats.end()) {
cout << "Invalid Seat Number! Please choose one of the available seat numbers." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
if (success) {
cout << "Ticket booked successfully!" << endl;
}
}
void cancelTicket() {
int coachType, seatNumber;
cout << "Enter Coach Type (1 for AC, 2 for Non-AC, 3 for Seater): ";
cin >> coachType;
if (coachType < 1 || coachType > 3) {
cout << "Invalid Coach Type! Please enter a valid option." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
switch (coachType) {
case 1:
acCoach.cancelSeatBooking(seatNumber - 1); // Adjust seat number to 0-based index
break;
case 2:
nonAcCoach.cancelSeatBooking(seatNumber - 1);
break;
case 3:
seater.cancelSeatBooking(seatNumber - 1);
break;
}
void run() {
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
bookTicket();
break;
case 2:
cancelTicket();
break;
case 3:
checkCoachStatus();
break;
case 4:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice! Please enter a number between 1 and 4." << endl;
}
} while (choice != 4);
}
};
int main() {
RailwayReservationSystem system;
system.run();
return 0;
}