DEV Community

Air France
Air France

Posted on

Create an Airport Reservation System in C++ with Full Code

To implement an airport reservation system in C++, we can design a basic console application that allows users to book a flight, check available seats, and manage reservations.

Here's a simple C++ program that simulates an airport reservation system:

C++ Code for Airport Reservation System

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Flight {
public:
    string flightNumber;
    string origin;
    string destination;
    int totalSeats;
    int bookedSeats;

    Flight(string flightNumber, string origin, string destination, int totalSeats)
        : flightNumber(flightNumber), origin(origin), destination(destination), totalSeats(totalSeats), bookedSeats(0) {}

    bool bookSeat() {
        if (bookedSeats < totalSeats) {
            bookedSeats++;
            return true;
        }
        return false;
    }

    int availableSeats() const {
        return totalSeats - bookedSeats;
    }
};

class ReservationSystem {
private:
    vector<Flight> flights;

public:
    void addFlight(string flightNumber, string origin, string destination, int totalSeats) {
        flights.push_back(Flight(flightNumber, origin, destination, totalSeats));
    }

    void displayFlights() const {
        cout << "Available Flights: \n";
        for (const auto& flight : flights) {
            cout << "Flight Number: " << flight.flightNumber << " | "
                 << "From: " << flight.origin << " | "
                 << "To: " << flight.destination << " | "
                 << "Available Seats: " << flight.availableSeats() << "/" << flight.totalSeats << endl;
        }
    }

    void bookTicket() {
        string flightNumber;
        cout << "Enter the flight number you want to book: ";
        cin >> flightNumber;

        bool flightFound = false;

        for (auto& flight : flights) {
            if (flight.flightNumber == flightNumber) {
                flightFound = true;
                if (flight.bookSeat()) {
                    cout << "Seat booked successfully on flight " << flight.flightNumber << "!\n";
                } else {
                    cout << "Sorry, no seats available on this flight.\n";
                }
                break;
            }
        }

        if (!flightFound) {
            cout << "Flight not found.\n";
        }
    }
};

int main() {
    ReservationSystem system;

    // Adding sample flights
    system.addFlight("AA123", "New York", "Los Angeles", 50);
    system.addFlight("BB456", "Chicago", "San Francisco", 60);
    system.addFlight("CC789", "Miami", "Houston", 30);

    int choice;
    do {
        cout << "\n*** Airport Reservation System ***\n";
        cout << "1. Display Available Flights\n";
        cout << "2. Book a Ticket\n";
        cout << "3. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
        case 1:
            system.displayFlights();
            break;
        case 2:
            system.bookTicket();
            break;
        case 3:
            cout << "Exiting the system.\n";
            break;
        default:
            cout << "Invalid choice. Please try again.\n";
        }

    } while (choice != 3);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

1. Flight Class:

Contains the flight details such as flight number, origin, destination, total seats, and booked seats.
The bookSeat function checks if there are available seats and books a seat if available.
The availableSeats function returns the number of available seats for that flight.

2. ReservationSystem Class:

Manages a list of flights (vector).
The addFlight method adds flights to the system.
The displayFlights method displays available flights along with the available seats.
The bookTicket method allows users to book tickets for a selected flight.

3. Main Program:

Provides a simple user interface where the user can display available flights, book a ticket, or exit the system.
The program repeatedly prompts the user until they choose to exit.

Example Interaction:

*** Airport Reservation System ***
1. Display Available Flights
2. Book a Ticket
3. Exit
Enter your choice: 1
Available Flights: 
Flight Number: AA123 | From: New York | To: Los Angeles | Available Seats: 50/50
Flight Number: BB456 | From: Chicago | To: San Francisco | Available Seats: 60/60
Flight Number: CC789 | From: Miami | To: Houston | Available Seats: 30/30


*** Airport Reservation System ***
1. Display Available Flights
2. Book a Ticket
3. Exit
Enter your choice: 2
Enter the flight number you want to book: AA123
Seat booked successfully on flight AA123!

*** Airport Reservation System ***
1. Display Available Flights
2. Book a Ticket
3. Exit
Enter your choice: 3
Exiting the system.
Enter fullscreen mode Exit fullscreen mode

This is a basic system. In a more advanced version, you could add user authentication, more detailed booking options (like dates and class), and save reservation data to a file or database. Air France: If more codes are required then contact us.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs