Parking Lot

Requirements

Functional Requirements

  1. Add/remove parking floors and slots.

  2. Park and unpark vehicles.

  3. Calculate parking fees.

  4. Support multiple vehicle types (bike, car, truck).

  5. Support different payment methods.

Non-Functional Requirements

  1. Scalability for multi-floor parking.

  2. Low latency for check-in and check-out.

  3. Extendability for additional features like reservations.


High-Level Design (HLD)

Key Components

  1. Parking Lot: Manages floors, slots, and overall operations.

  2. Parking Floor: Contains multiple parking slots.

  3. Parking Slot: Represents a unit where a vehicle can park.

  4. Vehicle: Represents different types of vehicles (bike, car, truck).

  5. Ticket: Tracks vehicle entry and exit.

  6. Payment: Handles fee calculation and processing.

HLD Diagram

ParkingLot
   |
   +-- ParkingFloor (1..n)
        |
        +-- ParkingSlot (1..n)
   |
   +-- Payment
   |
   +-- Ticket

Low-Level Design (LLD)

1. Classes and Responsibilities

ParkingLot

  • Manages multiple parking floors.

  • Tracks availability.

ParkingFloor

  • Contains multiple parking slots.

  • Identifies available slots.

ParkingSlot

  • Represents a single slot.

  • Tracks availability and vehicle type.

Vehicle

  • Contains details like type, registration number, and owner.

Ticket

  • Tracks entry time, exit time, and parking fees.

Payment

  • Handles fee calculation and payment processing.


Class Diagram

+-----------------+
| ParkingLot      |
+-----------------+
| - floors: List  |
| - tickets: List |
+-----------------+
| + addFloor()    |
| + parkVehicle() |
| + unparkVehicle()|
+-----------------+

+------------------+
| ParkingFloor     |
+------------------+
| - slots: List    |
+------------------+
| + findSlot()     |
| + addSlot()      |
+------------------+

+------------------+
| ParkingSlot      |
+------------------+
| - id: int        |
| - type: VehicleType |
| - isAvailable: boolean |
+------------------+
| + parkVehicle()  |
| + unparkVehicle()|
+------------------+

+-----------------+
| Vehicle         |
+-----------------+
| - type: VehicleType |
| - regNumber: String |
+-----------------+

+-----------------+
| Ticket          |
+-----------------+
| - entryTime: Date|
| - exitTime: Date |
| - slot: ParkingSlot |
+-----------------+

+-----------------+
| Payment         |
+-----------------+
| - calculateFee() |
+-----------------+

Implementation in Java (Spring Boot)

1. Enum for Vehicle Type

public enum VehicleType {
    BIKE, CAR, TRUCK
}

2. Parking Slot

public class ParkingSlot {
    private int id;
    private VehicleType type;
    private boolean isAvailable;

    public ParkingSlot(int id, VehicleType type) {
        this.id = id;
        this.type = type;
        this.isAvailable = true;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void parkVehicle() {
        if (!isAvailable) throw new IllegalStateException("Slot already occupied");
        isAvailable = false;
    }

    public void unparkVehicle() {
        if (isAvailable) throw new IllegalStateException("Slot is already empty");
        isAvailable = true;
    }
}

3. Parking Floor

import java.util.List;

public class ParkingFloor {
    private int floorNumber;
    private List<ParkingSlot> slots;

    public ParkingFloor(int floorNumber, List<ParkingSlot> slots) {
        this.floorNumber = floorNumber;
        this.slots = slots;
    }

    public ParkingSlot findAvailableSlot(VehicleType type) {
        return slots.stream()
                .filter(slot -> slot.isAvailable() && slot.getType().equals(type))
                .findFirst()
                .orElse(null);
    }
}

4. Ticket

import java.time.LocalDateTime;

public class Ticket {
    private String ticketId;
    private LocalDateTime entryTime;
    private LocalDateTime exitTime;
    private ParkingSlot slot;

    public Ticket(String ticketId, ParkingSlot slot) {
        this.ticketId = ticketId;
        this.slot = slot;
        this.entryTime = LocalDateTime.now();
    }

    public void closeTicket() {
        this.exitTime = LocalDateTime.now();
    }
}

5. Parking Lot

import java.util.ArrayList;
import java.util.List;

public class ParkingLot {
    private List<ParkingFloor> floors = new ArrayList<>();

    public void addFloor(ParkingFloor floor) {
        floors.add(floor);
    }

    public Ticket parkVehicle(Vehicle vehicle) {
        for (ParkingFloor floor : floors) {
            ParkingSlot slot = floor.findAvailableSlot(vehicle.getType());
            if (slot != null) {
                slot.parkVehicle();
                return new Ticket("TICKET-" + System.nanoTime(), slot);
            }
        }
        throw new RuntimeException("No available slots");
    }

    public void unparkVehicle(Ticket ticket) {
        ticket.getSlot().unparkVehicle();
        ticket.closeTicket();
    }
}

Flow Diagram

1. Vehicle arrives -> Request parking -> Search available slot
2. Assign slot -> Generate ticket -> Park vehicle
3. Vehicle departs -> Provide ticket -> Calculate fee
4. Free slot -> Close ticket -> Vehicle departs

Advantages

  1. Scalable: Supports multiple floors and vehicle types.

  2. Extensible: Easily add features like reservations.

  3. Maintainable: Clean segregation of responsibilities.

Future Enhancements

  1. Add reservation features.

  2. Integrate with payment gateways.

  3. Add a mobile-friendly UI for ease of use.

Last updated