Create Parking Management System in C

Faraz

By Faraz -

Learn to create a Parking Management System in C Programming with a beautiful UI. Follow our step-by-step guide for an easy-to-understand tutorial.


create-parking-management-system-in-c.webp

Introduction

Creating a Parking Management System in C programming is a great way to enhance your coding skills. This project will help you understand the fundamentals of C programming, including user interface design, file handling, and data management. In this guide, we’ll walk you through the entire process, from setting up your environment to running your final program. Let’s get started!

Step-by-Step Guide

1. Set Up Your Development Environment

Before you start coding, ensure you have a C compiler installed on your computer. For Windows users, you can use Code::Blocks or Dev-C++. For Linux and Mac users, you can use gcc, which is included in most distributions.

2. Create Your Project Files

Start by creating a new project in your C development environment. Name your project ParkingManagementSystem. Inside your project folder, create a file named main.c. This will be the main file where you’ll write your code.

3. Define Your Data Structures

In main.c, begin by defining the data structures you’ll use. For a parking management system, you might need structures for Car and ParkingSlot. Here’s a simple example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CARS 100

typedef struct {
    char licensePlate[15];
    char ownerName[30];
    int parkingSlot;
} Car;

typedef struct {
    int slotNumber;
    int isOccupied;
} ParkingSlot;

Car cars[MAX_CARS];
ParkingSlot slots[MAX_CARS];
int carCount = 0;

4. Implement the User Interface

Next, create a user-friendly menu interface. This will allow users to interact with your program easily. Here’s a basic example:

void clearScreen() {
    #ifdef _WIN32
        system("cls");
    #else
        system("clear");
    #endif
}

void displayMenu() {
    clearScreen();
    printf("\033[1;36m"); // Cyan color
    printf("****************************************\n");
    printf("*     PARKING MANAGEMENT SYSTEM         *\n");
    printf("****************************************\n");
    printf("* 1. Add Car                            *\n");
    printf("* 2. Remove Car                         *\n");
    printf("* 3. View Cars                          *\n");
    printf("* 4. Exit                               *\n");
    printf("****************************************\n");
    printf("\033[0m"); // Reset color
    printf("Choose an option: ");
}

5. Add Car Functionality

Create a function to add a car to the system. This will involve inputting details and assigning a parking slot:

void addCar() {
    if (carCount >= MAX_CARS) {
        printf("\n\033[1;31mParking full!\033[0m\n");
        return;
    }

    Car newCar;
    printf("\nEnter license plate: ");
    scanf("%s", newCar.licensePlate);
    printf("Enter owner name: ");
    scanf("%s", newCar.ownerName);

    for (int i = 0; i < MAX_CARS; i++) {
        if (slots[i].isOccupied == 0) {
            newCar.parkingSlot = i;
            slots[i].isOccupied = 1;
            cars[carCount++] = newCar;
            printf("\n\033[1;32mCar added to slot %d.\033[0m\n", i);
            return;
        }
    }
}

6. Remove Car Functionality

Implement a function to remove a car from the system:

void removeCar() {
    char licensePlate[15];
    printf("\nEnter license plate of car to remove: ");
    scanf("%s", licensePlate);

    for (int i = 0; i < carCount; i++) {
        if (strcmp(cars[i].licensePlate, licensePlate) == 0) {
            slots[cars[i].parkingSlot].isOccupied = 0;
            for (int j = i; j < carCount - 1; j++) {
                cars[j] = cars[j + 1];
            }
            carCount--;
            printf("\n\033[1;32mCar removed.\033[0m\n");
            return;
        }
    }
    printf("\n\033[1;31mCar not found.\033[0m\n");
}

7. View Cars Functionality

Create a function to display all parked cars:

void viewCars() {
    printf("\n\033[1;34m=== PARKED CARS ===\033[0m\n");
    if (carCount == 0) {
        printf("\n\033[1;33mNo cars parked.\033[0m\n");
        printf("\nPress Enter to return to the menu...");
        getchar(); // To consume newline character
        getchar(); // To wait for user input
        return;
    }

    for (int i = 0; i < carCount; i++) {
        printf("License Plate: \033[1;33m%s\033[0m, Owner: \033[1;33m%s\033[0m, Slot: \033[1;33m%d\033[0m\n",
               cars[i].licensePlate, cars[i].ownerName, cars[i].parkingSlot);
    }
    printf("\n\033[1;34m====================\033[0m\n");
    printf("\nPress Enter to return to the menu...");
    getchar(); // To consume newline character
    getchar(); // To wait for user input

    clearScreen(); // Clear screen after user presses Enter
}

8. Main Function

Combine all your functions in the main function:

int main() {
    int choice;

    for (int i = 0; i < MAX_CARS; i++) {
        slots[i].slotNumber = i;
        slots[i].isOccupied = 0;
    }

    while (1) {
        displayMenu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addCar();
                break;
            case 2:
                removeCar();
                break;
            case 3:
                viewCars();
                break;
            case 4:
                printf("\n\033[1;31mExiting...\033[0m\n");
                exit(0);
                break;
            default:
                printf("\n\033[1;31mInvalid option. Try again.\033[0m\n");
                printf("Press Enter to continue...");
                getchar(); // To consume newline character
                getchar(); // To wait for user input
        }
    }

    return 0;
}

Compiling and Running the Program

To compile your program, open your command prompt or terminal and navigate to your project directory. Use the following command:

gcc main.c -o ParkingManagementSystem

To run the compiled program, type:

ParkingManagementSystem

Conclusion

Congratulations! You've successfully created a Parking Management System in C programming. This project not only helps you practice your coding skills but also gives you a practical tool that can be expanded with more features in the future. Keep experimenting and adding more functionalities to make your program even better.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺