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.
Functional Requirements
-
Support parking for motorcycles, cars, and trucks.
-
Maintain three spot types (compact, regular, oversized) with appropriate capacity.
-
Assign spots based on vehicle size.
-
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
-
A
Vehicle(e.g.,Motorcycle,Car,Truck) arrives at the entry gate. -
The
ParkingLotsystem records essential vehicle details such aslicenseNumber,vehicleTypeandarrivalTime, and identifies an appropriate availableParkingSpot(e.g.,CompactSpot,RegularSpot, orOversizedSpot) based on the enteredvehicleType. -
If a suitable spot exists, the
ParkingLotsystem assigns theParkingSpotto theVehicle, generates aTicketand returns itsticketIdto the user.

Unpark Vehicle
-
The
Vehiclearrives at the exit gate and provides theticketIdto theParkingLotsystem. -
The
ParkingLotsystem retrieves the correspondingTicketand calculates the parking fee based on the duration of the stay. -
After the
Paymentis completed, theParkingLotsystem marks the associatedParkingSpotas available, updates theTicket, and allows theVehicleto exit.

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.

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.

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.

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.
