Design a multilevel parking lot system

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-09-22

Problem Statement

Design and implement the object-oriented model for a multi-level parking lot system. The system should handle multiple levels, different vehicle types, ticketing, and fee calculation. It should support real-world constraints like limited spots, efficient allocation, and concurrent vehicle entries/exits.


Functional Requirements (FR)


Parking Lot Setup


•A parking lot has multiple levels.
•Each level has a fixed number of parking spots.
•Spots can be of different types: Compact, Large, Motorbike, Handicapped.


Vehicle Management


•Vehicles can be of type: Car, Bike, Bus, Truck.
•A vehicle can park only in a compatible spot type.
•Support entry and exit of vehicles.


Ticketing System


•On entry, generate a ticket with details: vehicle number, spot, entry time.
•On exit, calculate parking fee based on duration and vehicle type.
•Mark the spot as free again.


Parking Spot Allocation


•Find the nearest available spot for a vehicle type.
•If parking lot is full, notify the user.


Admin Operations (Optional)


•Add/remove levels.
•Add/remove parking spots.
•View parking lot status (occupied vs free spots).


Non-Functional Requirements (NFR)


•Scalability → Should handle thousands of vehicles daily.
•Concurrency → Multiple vehicles can enter/exit simultaneously.
•Extensibility → Should support new spot types, pricing models, and payment methods.
•Performance → Spot lookup and allocation should be efficient (near O(1)).


Core Entities & Attributes


ParkingLot


•lotId
•levels[]


Level


•levelId
•spots[]


ParkingSpot


•spotId
•spotType (COMPACT, LARGE, BIKE, HANDICAPPED)
•isOccupied


Vehicle (Abstract)


•vehicleNumber
•vehicleType (CAR, BIKE, BUS, TRUCK)


Ticket


•ticketId
•vehicle
•spot
•entryTime
•exitTime (on release)
•fee


Payment (Optional)


•paymentId
•ticketId
•amount
•status (PENDING, SUCCESS, FAILED)


Core Components


ParkingSpotManager


•Manages available/occupied spots.
•Finds next free spot for a vehicle type.


TicketService


•Issues new tickets.
•Closes tickets and calculates fee.


PaymentService (Optional)


•Integrates with external payment gateways.


APIs / Operations


•parkVehicle(vehicle) → Allocates a spot & issues a ticket.
•exitVehicle(ticketId) → Frees the spot & calculates fee.
•getAvailableSpots(levelId, spotType) → Returns count of free spots.
•addLevel(levelConfig) → Adds a new parking level.
•addSpot(levelId, spotType) → Adds a new spot to a level.


LLD Design Goals


•Class Diagram → ParkingLot, Level, ParkingSpot, Vehicle (with subclasses), Ticket, ParkingSpotManager.
•Design Patterns:
•Singleton → For ParkingLot (one instance).
•Factory → For creating vehicles or spots.
•Strategy → For different pricing models.
•Observer → For notifying when parking lot is full.
•Concurrency Handling → Synchronization while allocating/freeing spots.


Solution

Comments