Object Oriented Design: Parking Lot System

December 18, 2025

Build a comprehensive solution for efficiently managing a parking lot. The system should automate key processes such as vehicle entry, ticket generation, intelligent spot allocation, real-time occupancy tracking, and seamless vehicle exit, ensuring a smooth and reliable parking experience.

parking-lot


Functional Requirements

  1. Support parking for motorcycles, cars, and trucks.

  2. Maintain three spot types (compact, regular, oversized) with appropriate capacity.

  3. Assign spots based on vehicle size.

  4. Calculate fees using vehicle size and parking duration, with time-of-day rate variations.



Sequence Flow

At its core, a parking lot system revolves around two main operations: parking and unparking a vehicle. Let's discuss the step by step flow of each use case to identify the core entities involved:

Park Vehicle

  1. A Vehicle (e.g., Motorcycle, Car, Truck) arrives at the entry gate.

  2. The ParkingLot system records essential vehicle details such as licenseNumber, vehicleType and arrivalTime, and identifies an appropriate available ParkingSpot (e.g., CompactSpot, RegularSpot, or OversizedSpot) based on the entered vehicleType.

  3. If a suitable spot exists, the ParkingLot system assigns the ParkingSpot to the Vehicle, generates a Ticket and returns its ticketId to the user.

park-vehicle-mermaid-diagram

Unpark Vehicle

  1. The Vehicle arrives at the exit gate and provides the ticketId to the ParkingLot system.

  2. The ParkingLot system retrieves the corresponding Ticket and calculates the parking fee based on the duration of the stay.

  3. After the Payment is completed, the ParkingLot system marks the associated ParkingSpot as available, updates the Ticket, and allows the Vehicle to exit.

unpark-vehicle-mermaid-diagram


Object Model Design

Let's identify the core classes involved in the parking lot system and outline their responsibilities, focusing on how each class encapsulates specific data and behavior to support the overall system functionality.

Vehicle

The Vehicle class represents a vehicle entering the parking lot. It serves primarily as a data holder, capturing essential details such as licenseNumber and vehicleType with minimal behavior.

To model parking constraints like "Which vehicle can park in which type of spot?", we can define specific subclasses:

  • Motorcycle: The smallest vehicle type, which can be accomodated in compact spots.
  • Car: A medium-sized vehicle that can fit in either compact or regular spots.
  • Truck: The largest vehicle type, which requires oversized spots.

This design leverages inheritance to represent the "is-a" relationship between vehicle types. It avoids large conditional logic and makes the system more extensible when introducing new vehicle types.

vehicle-class-diagram

ParkingSpot

The ParkingSpot class represents an individual parking space within the parking lot and is responsible for managing its occupancy state. It will maintain state such as spotId, spotType, and isAvailable, along with a reference to the assigned Vehicle.

Each parking spot supports basic operations to manage its occupancy, including assigning a Vehicle when occupied and removing the Vehicle when it becomes available.

void assignVehicle(Vehicle v) {
    this.vehicle = v;
    this.isOccupied = true;
}

void removeVehicle() {
    this.vehicle = null;
    this.isOccupied = false;
}

By encapsulating these updates within controlled methods, the system ensures the state changes occur atomically and the spot’s state remains consistent and up to date.

We can use inheritance to model different types of parking spots as specialized variants of a base ParkingSpot class.

  • CompactSpot: Suitable for smaller vehicles such as motorcycles and cars.
  • RegularSpot: Designed for standard vehicles like cars.
  • OversizedSpot: Intended for larger vehicles like trucks.

parking-spot-class-diagram

Ticket

The Ticket class represents a parking session and acts as a record of a vehicle's entry and exit within the parking lot. It captures essential details such as associated Vehicle, assigned ParkingSpot, entryTime, and exitTime, along with the total parking amount.

It primarily acts as a data holder with minimal behavior, such as updating the exitTime and amount. Core business logic such as fee calculation and releasing the associated parking spot can be delegated to separate components, ensuring proper separation of concerns.

ticket-class-diagram

ParkingLot

The ParkingLot class acts as the central orchestrator (facade) of the system, coordinating all core operations such as findAvailableSpot(VehicleType type), calculatePrice(Ticket ticket), or processPayment(double amount) involved in parking and unparking vehicles.

Instead of handling all responsibilities directly, it can delegate responsibilities to specialized components.

// ✅ Good design
ParkingLot  SpotManager: find spot  
ParkingLot  Ticket: create ticket 
ParkingLot  PriceCalculator: calculate fee  
ParkingLot  PaymentProcessor: process payment  
ParkingLot  ParkingSpot: free spot   

This design promotes a clear separation of concerns by ensuring that each component is responsible for a specific piece of functionality.

During unparking, the system uses the ticketId to retrieve all related parking details. To make this efficient, the ParkingLot can hold a Map<String, Ticket>, allowing direct and constant-time access to the corresponding Ticket without needing to search through all records.

parking-lot-class-diagram

SpotManager

PriceCalculator

PaymentProcessor