Cse Ass
Cse Ass
Programming Language.
Objectives:
• To Develop a Ticket Booking System.
• To Implement Seat Allocation Mechanism.
• To Integrate Class and Fare Management.
• To Enable Modifications and Cancellations.
Full Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Passenger {
string name;
int seatNumber;
string seatClass;
int price;
public:
Passenger(string n, int seat, string cls, int cost)
: name(n), seatNumber(seat), seatClass(cls), price(cost) {}
1
public:
void bookSeat(string name, string cls) {
int start = 0, end = 0, price = 0;
if (cls == "Business") {
start = 0;
end = BUSINESS_SEATS;
price = BUSINESS_PRICE;
} else if (cls == "Economy") {
start = BUSINESS_SEATS;
end = TOTAL_SEATS;
price = ECONOMY_PRICE;
} else {
cout << "Invalid class type. Choose Business or Economy.\n";
return;
}
for (int i = start; i < end; ++i) {
if (!seats[i]) {
seats[i] = true;
bookings.push_back(Passenger(name, i + 1, cls, price));
cout << "Seat booked for " << name
<< " in " << cls << " class. Seat Number: " << i + 1
<< ", Price: BDT" << price << endl;
return;
}}
cout << cls << " class is full!\n";
}
void cancelBooking(string name) {
for (size_t i = 0; i < bookings.size(); ++i) {
if (bookings[i].getName() == name) {
seats[bookings[i].getSeatNumber() - 1] = false;
bookings.erase(bookings.begin() + i);
cout << "Booking canceled for " << name << endl;
return;
}}
cout << "No booking found for " << name << endl;
}
void showAllBookings() {
cout << "\n--- Current Bookings ---\n";
for (const auto& p : bookings) {
p.display();
}}
void showAvailableSeats() {
cout << "\nAvailable Business Seats: ";
2
for (int i = 0; i < BUSINESS_SEATS; ++i) {
if (!seats[i]) cout << (i + 1) << " ";
}
cout << "\nAvailable Economy Seats: ";
for (int i = BUSINESS_SEATS; i < TOTAL_SEATS; ++i) {
if (!seats[i]) cout << (i + 1) << " ";
}
cout << endl;
}};
int main() {
Airline airline;
int choice;
string name, cls;
cout<<"--------------MTE Roll(7-12) Airlines Agency--------------"<<endl<<"Flight from
Rajshahi To Dhaka"<<endl;
do {
cout << "\n1. Book Seat\n2. Cancel Booking\n3. Show All Bookings\n4. Show Available
Seats\n0. Exit\nEnter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
cout << "Enter passenger name: ";
getline(cin, name);
cout << "Enter class (Business/Economy): ";
getline(cin, cls);
airline.bookSeat(name, cls);
break;
case 2:
cout << "Enter name to cancel booking: ";
getline(cin, name);
airline.cancelBooking(name);
break;
case 3:
airline.showAllBookings();
break;
case 4:
airline.showAvailableSeats();
break;
}}
while (choice != 0);
return 0;}
3
Output:
4
Figure: Airlines Ticket Booking System Application of System
Explanation:
• Header and Namespaces:
Includes required libraries for input/output, dynamic arrays, and strings. Prepares the
environment to use cin, cout, vector, string without prefixes.
• Constants Section:
Defines constants for seat configuration and pricing. Used in booking logic to assign seat
limits and fares.
5
• Passenger Class:
Represents each booked passenger. Initializes passenger with name, seat, class, and price
as constructor. getName(), getSeatNumber(), getSeatClass() was used as getter methods.
display() was used to display passenger details.
• Airline Class:
Manages all operations of the airline system (booking, cancellation, etc.). Stores all
passenger bookings. Tracks seat availability (false = available, true = booked).
• Airline::bookSeat():
Books a seat for a passenger. Determines seat range and price based on class. Finds the
first available seat. Marks the seat as booked. Adds passenger to bookings list. Displays
booking confirmation or full class message.
• Airline::cancelBooking():
Cancels a booking by passenger name. Searches bookings for matching name. Frees the
seat and removes the passenger. Displays cancellation success or not found message.
• Airline::showAllBookings():
Lists all current passenger bookings. Loops through bookings and calls each passenger's
display() method.
• Airline::showAvailableSeats():
Displays unbooked seat numbers in each class. Loops through seats[] and prints available
seat numbers.
• main() Function:
Displays menu. Handles user input and choices using a loop and switch-case.
1: Book seat → calls bookSeat()
2: Cancel booking → calls cancelBooking()
3: Show all bookings → calls showAllBookings()
4: Show available seats → calls showAvailableSeats()
0: Exit program
• User Interface Header:
Displays the program title ”MTE Roll(7-12) Airlines Agency” and flight route (Flight from
Rajshahi To Dhaka) at the start.
Discussion:
This assignment presents a C++ program simulating an airline ticket booking and seat allocation
system. It manages two travel classes—Business and Economy—using predefined constants for
seat limits and pricing. The Passenger class stores individual booking information, while the
Airline class handles booking operations and seat availability through a vector and a boolean array.
The user interface is menu-driven, allowing users to book a seat by entering their name and
choosing a class, with the system automatically assigning the next available seat. It also provides
6
options to cancel a booking by name, view all current bookings, and display available seats for
each class. The program ensures basic validation (e.g., class type) and updates the booking list
accordingly. Overall, it demonstrates the use of object-oriented programming to handle real-life
scenarios like ticket management in a structured and efficient way.
Conclusion:
In conclusion, this assignment effectively demonstrates a basic airline reservation system using
C++. It applies object-oriented programming concepts through the use of Passenger and Airline
classes. The system allows booking, cancellation, and seat availability tracking in both Business
and Economy classes. It uses arrays and vectors for data storage and manipulation. The menu-
driven approach makes it user-friendly and interactive. Seat allocation is handled automatically,
minimizing user input errors. The program ensures data consistency by updating seat status with
every operation. Overall, it provides a solid foundation for more advanced booking systems.