Bus Reservation System Project using C++

The Bus Reservation System is built using C++ and features a menu-driven interface for users to perform various tasks. It incorporates essential functionalities such as adding new bus details, making reservations, viewing reservations, checking available buses, and gracefully exiting the system.

Project Overview

Welcome aboard the digital journey of the Bus Reservation System! In this article, we’ll explore the intricacies of a simple, yet efficient bus reservation program written in C++. This system allows users to manage and reserve bus seats, providing a seamless experience for both passengers and administrators.

Exploring the Functionalities with Outputs

1. Adding New Bus

Users can add new buses to the system by providing details such as bus number, driver’s name, arrival, and departure times, as well as source and destination. The system initializes the seat matrix for the new bus.

bus reservation system project in c++

2. Making Reservations

Passengers can make reservations by specifying the bus number and selecting an available seat. The system checks the seat’s availability and records the passenger’s name. Here you can check that which seat is available.

OutPut for making reservation in bus reservation project

3. Viewing Reservations

Administrators can view reservations for a specific bus by entering its number. The system displays the bus details, seat matrix, and the names of passengers who have made reservations.

view reservation in bus reservation project

4. Checking Available Buses

Users can check the available buses along with their details such as bus number, driver name, arrival, and departure times, source, and destination.

checking available buses in bus reservation system c++ project

4. Exiting the System

A graceful exit option allows users to conclude their interaction with the system.

See basic concepts of Object Oriented Programming

The Code Structure

The project is structured into three main components:

buss.h

This header file contains the class definition for the bus class, which encapsulates the properties and methods related to bus operations. It includes attributes like bus number, driver name, arrival and departure times, source and destination, and an array to represent bus seat availability.


// bus.h
#pragma once
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class Bus {
    string bus_number;
    string bus_driver;
    string bus_arrival;
    string bus_depart;
    string b_from;
    string b_to;
    string bus_seat[8][4];

public:
    void addNewBus();
    void makeReservation(Bus buses[], int busIndex);
    void clearSeats();
    void viewBusDetails();
    void showAvailableBuses(Bus buses[]);
    void showSeatAvailability(int busIndex);
    void displayLine(char ch);
};

buss.cpp

The implementation file (bus. cpp) contains the logic for member functions of the bus class. These functions handle tasks such as adding new bus details, making reservations, viewing reservations, and displaying available buses.

// bus.cpp
#include "buss.h"

void Bus::addNewBus() {
    cout << "Enter Bus Number: ";
    cin >> bus_number;

    cout << "\nEnter Driver's Name: ";
    cin >> bus_driver;

    cout << "\nArrival Time: ";
    cin >> bus_arrival;

    cout << "\nDeparture Time: ";
    cin >> bus_depart;

    cout << "\nFrom: ";
    cin >> b_from;

    cout << "\nTo: ";
    cin >> b_to;

    clearSeats();
    cout << "\nNew Bus Added Successfully.\n";
}

void Bus::makeReservation(Bus buses[], int busIndex) {
    showSeatAvailability(busIndex);

    cout << "Enter Seat Number for Reservation: ";
    int seatNumber;
    cin >> seatNumber;

    if (seatNumber < 1 || seatNumber > 32) {
        cout << "Invalid seat number. Please enter a number between 1 and 32.\n";
        return;
    }

    int row = (seatNumber - 1) / 4;
    int col = (seatNumber - 1) % 4;

    if (buses[busIndex].bus_seat[row][col] == "Empty") {
        cout << "Enter Passenger's Name: ";
        cin >> buses[busIndex].bus_seat[row][col];
        cout << "Reservation Successful!\n";
    }
    else {
        cout << "The seat is already reserved. Please choose another seat.\n";
    }
}

void Bus::clearSeats() {
    for (int x = 0; x < 8; x++) {
        for (int z = 0; z < 4; z++) {
            bus_seat[x][z] = "Empty";
        }
    }
}

void Bus::viewBusDetails() {
    displayLine('*');
    cout << "Bus Number: \t" << bus_number
        << "\nDriver: \t" << bus_driver << "\t\tArrival Time: \t"
        << bus_arrival << "\tDeparture Time:" << bus_depart
        << "\nFrom: \t\t" << b_from << "\t\tTo: \t\t" << b_to << "\n";
    displayLine('*');

    showSeatAvailability(0);  // Display seat availability for this bus
}

void Bus::showAvailableBuses(Bus buses[]) {
    displayLine('*');
    cout << "Available Buses:\n";
    for (int i = 0; i < 5; i++) {
        if (!buses[i].bus_number.empty()) {
            cout << "Bus Number: " << buses[i].bus_number << "\tDriver: " << buses[i].bus_driver
                << "\tFrom: " << buses[i].b_from << "\tTo: " << buses[i].b_to << "\n";
        }
    }
    displayLine('_');
}

void Bus::showSeatAvailability(int busIndex) {
    cout << "Seat Availability:\n";
    int seatNumber = 1;

    for (int x = 0; x < 8; x++) {
        for (int z = 0; z < 4; z++) {
            cout << setw(5) << left << seatNumber << ".";
            cout << setw(10) << left << bus_seat[x][z];
            seatNumber++;
        }
        cout << "\n";
    }

    cout << "\nThere are " << (32 - seatNumber + 1) << " Seats Available in Bus Number: " << bus_number << "\n";
}

void Bus::displayLine(char ch) {
    for (int x = 80; x > 0; x--) {
        cout << ch;
    }
    cout << '\n';
}

main.cpp

The main driver of the program (main. cpp) orchestrates the interaction with users. It creates an array of bus objects and presents a menu for users to choose from various options like adding a new bus, making reservations, viewing reservations, checking available buses, and exiting the program.

// main.cpp
#include "buss.h"

int main() {
    const int MAX_BUSES = 5;
    Bus buses[MAX_BUSES];

    int choice;

    while (true) {
        cout << "\n\n";
        cout << "\t\t***SIMPLE BUS RESERVATION SYSTEM***";
        cout << "\n\n";
        cout << "\t\t\t1. Add New Bus\n\t\t\t"
            << "2. Make Reservation\n\t\t\t"
            << "3. View Reservation\n\t\t\t"
            << "4. Show Available Buses\n\t\t\t"
            << "5. Exit";
        cout << "\n\t\t\tEnter your Choice: ";

        cin >> choice;

        switch (choice) {
        case 1:
            buses[0].addNewBus();
            break;

        case 2:
            int busIndex;
            cout << "Enter Bus Index: ";
            cin >> busIndex;
            if (busIndex >= 0 && busIndex < MAX_BUSES) {
                buses[0].makeReservation(buses, busIndex);
            }
            else {
                cout << "Invalid Bus Index.\n";
            }
            break;

        case 3:
            buses[0].viewBusDetails();
            break;

        case 4:
            buses[0].showAvailableBuses(buses);
            break;

        case 5:
            cout << "Exiting the program. Thank you!\n";
            return 0;

        default:
            cout << "Invalid choice. Please try again.\n";
        }
    }

    return 0;
}

Enhancements and Future Improvements

The Bus Reservation System serves as a foundation that can be enhanced in various ways. Some potential improvements include incorporating linked lists for managing buses, providing additional features like cancellation of reservations, and implementing graphical representations for a more user-friendly experience.

NOTE…

If you are using Microsoft Visual Studio code or community then simply clone our Github Repository and start your project, Here is the link to Github Repository.

Conclusion

In a nutshell, the Bus Reservation System in C++ offers a practical solution for managing bus reservations. By exploring the code structure and functionalities, users can gain insights into how such systems are implemented. Whether you’re a coding enthusiast or a student learning C++, this project provides hands-on experience in building a functional reservation system.

You may also like:

FAQ


1. Is there a limit to the number of buses that can be added to the system?

The current implementation supports up to 5 buses. If you wish to extend this limit, you can modify the code to accommodate more buses based on your requirements.

2. What happens if I try to reserve an already reserved seat?

If you attempt to reserve a seat that is already booked, the system will prompt you to choose another seat. You cannot reserve a seat that is already occupied by another passenger.

3. Can I modify the code for my specific needs or features?

Absolutely! The provided code is for educational purposes. You are encouraged to modify and expand it based on your preferences, requirements, or additional features you may want to include in your bus reservation system.