How to Design a Parking Lot System: Architecture and Flow
To design a
parking lot system, define components like parking spots, vehicles, and ticketing. Use a parking manager to allocate spots and track availability, ensuring smooth entry and exit flow with real-time updates.Syntax
The parking lot system consists of these main parts:
- ParkingSpot: Represents a single parking space with attributes like spot number and type.
- Vehicle: Represents the vehicle entering or leaving the lot.
- ParkingLot: Manages all parking spots and tracks availability.
- Ticket: Issued when a vehicle enters, used to identify the spot and calculate fees.
- ParkingManager: Controls the flow of vehicles, assigns spots, and handles entry/exit.
This structure helps organize the system clearly and supports scalability.
python
class ParkingSpot: def __init__(self, spot_id, spot_type): self.spot_id = spot_id self.spot_type = spot_type self.is_free = True class Vehicle: def __init__(self, license_plate, vehicle_type): self.license_plate = license_plate self.vehicle_type = vehicle_type class Ticket: def __init__(self, ticket_id, spot, vehicle): self.ticket_id = ticket_id self.spot = spot self.vehicle = vehicle self.entry_time = None self.exit_time = None class ParkingLot: def __init__(self, spots): self.spots = spots # list of ParkingSpot class ParkingManager: def __init__(self, parking_lot): self.parking_lot = parking_lot self.active_tickets = {} def assign_spot(self, vehicle): pass def release_spot(self, ticket_id): pass
Example
This example shows a simple parking lot system that assigns the first free spot to a vehicle and releases it on exit.
python
import datetime class ParkingSpot: def __init__(self, spot_id, spot_type): self.spot_id = spot_id self.spot_type = spot_type self.is_free = True class Vehicle: def __init__(self, license_plate, vehicle_type): self.license_plate = license_plate self.vehicle_type = vehicle_type class Ticket: def __init__(self, ticket_id, spot, vehicle): self.ticket_id = ticket_id self.spot = spot self.vehicle = vehicle self.entry_time = datetime.datetime.now() self.exit_time = None class ParkingLot: def __init__(self, spots): self.spots = spots class ParkingManager: def __init__(self, parking_lot): self.parking_lot = parking_lot self.active_tickets = {} self.ticket_counter = 0 def assign_spot(self, vehicle): for spot in self.parking_lot.spots: if spot.is_free: spot.is_free = False self.ticket_counter += 1 ticket = Ticket(self.ticket_counter, spot, vehicle) self.active_tickets[self.ticket_counter] = ticket print(f"Assigned spot {spot.spot_id} to vehicle {vehicle.license_plate}") return ticket print("No free spots available") return None def release_spot(self, ticket_id): ticket = self.active_tickets.get(ticket_id) if ticket: ticket.exit_time = datetime.datetime.now() ticket.spot.is_free = True duration = (ticket.exit_time - ticket.entry_time).seconds // 60 print(f"Vehicle {ticket.vehicle.license_plate} exited from spot {ticket.spot.spot_id} after {duration} minutes") del self.active_tickets[ticket_id] else: print("Invalid ticket ID") # Setup parking lot with 3 spots spots = [ParkingSpot(1, 'compact'), ParkingSpot(2, 'large'), ParkingSpot(3, 'compact')] parking_lot = ParkingLot(spots) manager = ParkingManager(parking_lot) # Vehicle enters vehicle1 = Vehicle('ABC123', 'car') ticket1 = manager.assign_spot(vehicle1) # Vehicle exits manager.release_spot(ticket1.ticket_id)
Output
Assigned spot 1 to vehicle ABC123
Vehicle ABC123 exited from spot 1 after 0 minutes
Common Pitfalls
Common mistakes when designing a parking lot system include:
- Not handling different vehicle sizes and spot types, causing allocation errors.
- Failing to update spot availability in real-time, leading to double bookings.
- Ignoring concurrency issues when multiple vehicles enter or exit simultaneously.
- Not designing for scalability, making it hard to add more spots or levels later.
Always plan for thread-safe updates and clear spot categorization.
python
class ParkingManager: def assign_spot(self, vehicle): # Wrong: Does not check if spot matches vehicle type for spot in self.parking_lot.spots: if spot.is_free: spot.is_free = False # ... return def assign_spot_correct(self, vehicle): # Right: Checks spot type matches vehicle type for spot in self.parking_lot.spots: if spot.is_free and spot.spot_type == vehicle.vehicle_type: spot.is_free = False # ... return
Quick Reference
- ParkingSpot: Represents each parking space with type and availability.
- Vehicle: Has license and type to match spots.
- Ticket: Tracks vehicle entry and exit times.
- ParkingManager: Assigns and releases spots, manages tickets.
- Concurrency: Use locks or atomic operations to avoid conflicts.
- Scalability: Design for multiple levels and spot categories.
Key Takeaways
Design clear classes for spots, vehicles, tickets, and management to organize the system.
Always check spot availability and type before assigning to avoid errors.
Update spot status immediately on vehicle entry and exit to prevent double bookings.
Plan for concurrency and scalability from the start to handle real-world usage.
Use tickets to track parking duration and support billing or reporting.