Monday, March 24, 2025

Simple C++ program for an air ticketing system

 Here's a simple C++ program for an air ticketing system:



#include <iostream>

#include <string>


using namespace std;


class Flight {

public:

    string flightNumber;

    string departureCity;

    string arrivalCity;

    string departureTime;

    string arrivalTime;

    int availableSeats;

    double ticketPrice;


    Flight(string flightNumber, string departureCity, string arrivalCity, string departureTime, string arrivalTime, int availableSeats, double ticketPrice) {

        this->flightNumber = flightNumber;

        this->departureCity = departureCity;

        this->arrivalCity = arrivalCity;

        this->departureTime = departureTime;

        this->arrivalTime = arrivalTime;

        this->availableSeats = availableSeats;

        this->ticketPrice = ticketPrice;

    }


    void displayFlightDetails() {

        cout << "Flight Number: " << flightNumber << endl;

        cout << "Departure City: " << departureCity << endl;

        cout << "Arrival City: " << arrivalCity << endl;

        cout << "Departure Time: " << departureTime << endl;

        cout << "Arrival Time: " << arrivalTime << endl;

        cout << "Available Seats: " << availableSeats << endl;

        cout << "Ticket Price: " << ticketPrice << endl;

    }

};


class Passenger {

public:

    string name;

    string email;

    string phoneNumber;


    Passenger(string name, string email, string phoneNumber) {

        this->name = name;

        this->email = email;

        this->phoneNumber = phoneNumber;

    }


    void displayPassengerDetails() {

        cout << "Name: " << name << endl;

        cout << "Email: " << email << endl;

        cout << "Phone Number: " << phoneNumber << endl;

    }

};


class Booking {

public:

    Flight flight;

    Passenger passenger;

    int numberOfSeats;


    Booking(Flight flight, Passenger passenger, int numberOfSeats) {

        this->flight = flight;

        this->passenger = passenger;

        this->numberOfSeats = numberOfSeats;

    }


    void displayBookingDetails() {

        cout << "Flight Details:" << endl;

        flight.displayFlightDetails();

        cout << "\nPassenger Details:" << endl;

        passenger.displayPassengerDetails();

        cout << "\nNumber of Seats: " << numberOfSeats << endl;

        cout << "Total Fare: " << flight.ticketPrice * numberOfSeats << endl;

    }

};


int main() {

    int choice;

    string flightNumber;

    string departureCity;

    string arrivalCity;

    string departureTime;

    string arrivalTime;

    int availableSeats;

    double ticketPrice;

    string name;

    string email;

    string phoneNumber;

    int numberOfSeats;


    Flight flight1("F001", "New York", "Los Angeles", "10:00", "13:00", 200, 500.0);

    Flight flight2("F002", "Chicago", "Houston", "14:00", "17:00", 250, 400.0);


    do {

        cout << "Air Ticketing System" << endl;

        cout << "1. Book Flight" << endl;

        cout << "2. Display Flight Details" << endl;

        cout << "3. Exit" << endl;

        cout << "Enter your choice: ";

        cin >> choice;


        switch (choice) {

            case 1:

                cout << "Select Flight:" << endl;

                cout << "1. Flight F001" << endl;

                cout << "2. Flight F002" << endl;

                int flightChoice;

                cin >> flightChoice;


                if (flightChoice == 1) {

                    flightNumber = flight1.flightNumber;

                    departureCity = flight1.departureCity;

                    arrivalCity = flight1.arrivalCity;

                    departureTime = flight1.departureTime;

                    arrivalTime = flight1.arrivalTime;

                    availableSeats = flight1.availableSeats;

                    ticketPrice = flight1.ticketPrice;

                } else if (flightChoice == 2) {

                    flightNumber = flight2.flightNumber;

                    departureCity = flight2.departureCity;

                    arrivalCity = flight2.arrivalCity;

                    departureTime = flight2.departureTime;

                    arrivalTime = flight2.arrivalTime;

                    availableSeats = flight2.availableSeats;

                    ticketPrice = flight2.ticketPrice;

                }


                cout << "Enter Passenger Name: ";

                cin.ignore();

                getline(cin, name);

                cout << "Enter Passenger Email: ";

                getline(cin, email);

                cout << "Enter Passenger Phone Number: ";

                getline(cin, phoneNumber);

                cout << "Enter Number of Seats: ";

                cin >> numberOfSeats;


                if (numberOfSeats <= availableSeats) {

                    Passenger passenger(name, email, phoneNumber);

                    Flight flight(flightNumber, departureCity, arrivalCity, departureTime, arrivalTime, availableSeats, ticketPrice);

                    Booking booking(flight, passenger, numberOfSeats);

                    booking.displayBookingDetails();

                } else {

                    cout << "Sorry, not enough seats available." << endl;

                }

                break;

            case 2:

                cout << "Select Flight:" << endl;

                cout << "1. Flight F001" << endl;

                cout << "2. Flight F002" << endl;

                int displayFlightChoice;

                cin >> displayFlightChoice;


                if (displayFlightChoice == 1) {

                    flight1.displayFlightDetails();

                } else if (displayFlightChoice == 2) {

                    flight2.displayFlightDetails();

                }

                break;

            case 3:

                cout << "Exiting program..." << endl;

                break;

            default:

                cout << "Invalid choice. Please try again." << endl;

        }

    } while (choice != 3);


    return 0;

}



This program creates a simple air ticketing system with the following features:


- Allows users to book flights

- Displays flight details

- Allows users to exit the program


Note that this is a basic implementation and can be extended to include more features like storing flight and passenger data in a database, handling payments, and generating tickets.

No comments:

Post a Comment

Featured posts

What is the future of the new generation?

 What is the future of the new generation? The future of the new generation is exciting and uncertain. With rapid advancements in technology...

Popular posts