0% found this document useful (0 votes)
24 views4 pages

Zohol 1

The document defines classes for seats, coaches, and a railway reservation system to model booking and canceling tickets on different coaches. It includes methods for displaying menus, booking and canceling tickets, and checking coach status.

Uploaded by

SUSEENDRAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Zohol 1

The document defines classes for seats, coaches, and a railway reservation system to model booking and canceling tickets on different coaches. It includes methods for displaying menus, booking and canceling tickets, and checking coach status.

Uploaded by

SUSEENDRAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <vector>
#include <limits>
#include <algorithm>

using namespace std;

class Seat {
private:
bool booked;

public:
Seat() : booked(false) {}

bool isBooked() const {


return booked;
}

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);
}

int getNumSeats() const {


return seats.size();
}

int getNumAvailableSeats() const {


int count = 0;
for (const auto& seat : seats) {
if (!seat.isBooked()) {
count++;
}
}
return count;
}

Seat& getSeat(int seatNumber) {


return seats[seatNumber];
}

bool bookSeat(int seatNumber) {


if (seatNumber >= 0 && seatNumber < seats.size() && !seats[seatNumber].isBooked()) {
seats[seatNumber].book();
return true;
} else if (seatNumber >= 0 && seatNumber < seats.size() && seats[seatNumber].isBooked()) {
cout << "Seat " << seatNumber + 1 << " is already booked. Please choose another seat." << endl;
return false;
}
return false;
}
void cancelSeatBooking(int seatNumber) {
if (seatNumber >= 0 && seatNumber < seats.size()) {
seats[seatNumber].cancelBooking();
}
}
};

class RailwayReservationSystem {
private:
Coach acCoach;
Coach nonAcCoach;
Coach seater;

public:
RailwayReservationSystem() : acCoach("AC", 60), nonAcCoach("Non-AC", 60), seater("Seater", 60) {}

void displayMenu() const {


cout << "Welcome to Railway Reservation System" << endl;
cout << "1. Book Ticket" << endl;
cout << "2. Cancel Ticket" << endl;
cout << "3. Check Coach Status" << endl;
cout << "4. Exit" << endl;
}

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;
}

cout << "Available Seat Numbers: ";


for (int seat : availableSeats) {
cout << seat << " ";
}
cout << endl;

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;
}

bool success = false;


switch (coachType) {
case 1:
success = acCoach.bookSeat(seatNumber - 1); // Adjust seat number to 0-based index
break;
case 2:
success = nonAcCoach.bookSeat(seatNumber - 1);
break;
case 3:
success = seater.bookSeat(seatNumber - 1);
break;
}

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;
}

cout << "Enter Seat Number: ";


cin >> seatNumber;
if (seatNumber < 1 || seatNumber > 60) {
cout << "Invalid Seat Number! Please enter a seat number between 1 and 60." << 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;
}

cout << "Ticket canceled successfully!" << endl;


}

void checkCoachStatus() const {


cout << "Coach Status:" << endl;
cout << "AC Coach:" << endl;
cout << "Total seats: " << acCoach.getNumSeats() << endl;
cout << "Available seats: " << acCoach.getNumAvailableSeats() << endl;

cout << "Non-AC Coach:" << endl;


cout << "Total seats: " << nonAcCoach.getNumSeats() << endl;
cout << "Available seats: " << nonAcCoach.getNumAvailableSeats() << endl;

cout << "Seater Coach:" << endl;


cout << "Total seats: " << seater.getNumSeats() << endl;
cout << "Available seats: " << seater.getNumAvailableSeats() << endl;
}

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;
}

You might also like