Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Parking Lot Management System with C# and Windows Forms

Tech Nov 18 11

A parking lot management system addresses the inefficiencies of traditional methods by automaitng vehicle entry and exit, monitoring space availabliity, and handling billing. This implementation uses C# with Windows Forms for the user interface, SQL Server for data storage, and ADO.NET for database operations, enhanced with GDI+ to visualization and multi-threading for concurrency.

Vehicle Entry and Exit Management

This module captures vehicle details, assigns available parking spots, and logs timestamps. A simulated license plate recognition process initiates the workflow.

// Method to process vehicle entry
private void ProcessVehicleArrival()
{
    string plateNumber = inputPlateField.Text.Trim();
    if (string.IsNullOrWhiteSpace(plateNumber))
    {
        ShowAlert("Plate number is required.", "Input Error");
        return;
    }

    int freeSpotId = LocateEmptySpot();
    if (freeSpotId == 0)
    {
        ShowAlert("No parking spaces available.", "Full Capacity");
        return;
    }

    var arrivalRecord = new ArrivalLog
    {
        PlateNumber = plateNumber,
        ArrivalTimestamp = DateTime.Now,
        AssignedSpotId = freeSpotId
    };

    using (var dataContext = new ParkingDataContext())
    {
        dataContext.ArrivalLogs.Add(arrivalRecord);
        var spot = dataContext.ParkingSpaces.Find(freeSpotId);
        spot.Occupied = true;
        dataContext.SaveChanges();
    }

    RefreshDisplay();
    ShowAlert($"Vehicle {plateNumber} parked at spot {freeSpotId}.", "Confirmed");
    inputPlateField.Clear();
}

// Find an unoccupied parking spot
private int LocateEmptySpot()
{
    using (var dataContext = new ParkingDataContext())
    {
        var emptySpot = dataContext.ParkingSpaces.FirstOrDefault(ps => !ps.Occupied);
        return emptySpot?.SpaceId ?? 0;
    }
}

Real-Time Parking Space Monitoring

A graphical layout displays spot occupancy, updated dynamically. Users can view availability and navigate to specific locations.

// Render the parking lot layout
private void RenderParkingGrid(Graphics graphics)
{
    int spaceWidth = 60;
    int spaceHeight = 30;
    int margin = 10;
    int rowCount = 10;
    int columnCount = 20;

    List<ParkingSpace> spaces;
    using (var dataContext = new ParkingDataContext())
    {
        spaces = dataContext.ParkingSpaces.ToList();
    }

    foreach (var space in spaces)
    {
        int rowIndex = (space.SpaceId - 1) / columnCount;
        int columnIndex = (space.SpaceId - 1) % columnCount;
        int xPos = columnIndex * (spaceWidth + margin) + margin;
        int yPos = rowIndex * (spaceHeight + margin) + margin;

        Brush fillColor = space.Occupied ? Brushes.Red : Brushes.Green;
        graphics.FillRectangle(fillColor, xPos, yPos, spaceWidth, spaceHeight);

        graphics.DrawString(space.SpaceId.ToString(), this.Font, Brushes.Black, xPos + 5, yPos + 5);
        graphics.DrawRectangle(Pens.Black, xPos, yPos, spaceWidth, spaceHeight);
    }
}

// Periodic update of parking status
private void UpdateTimer_Tick(object sender, EventArgs e)
{
    RefreshDisplay();
}

private void RefreshDisplay()
{
    layoutPanel.Invalidate();
    using (var dataContext = new ParkingDataContext())
    {
        int freeCount = dataContext.ParkingSpaces.Count(ps => !ps.Occupied);
        availableLabel.Text = $"Available spots: {freeCount}";
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.