Parking Lot
Requirements
Functional Requirements
Add/remove parking floors and slots.
Park and unpark vehicles.
Calculate parking fees.
Support multiple vehicle types (bike, car, truck).
Support different payment methods.
Non-Functional Requirements
Scalability for multi-floor parking.
Low latency for check-in and check-out.
Extendability for additional features like reservations.
High-Level Design (HLD)
Key Components
Parking Lot: Manages floors, slots, and overall operations.
Parking Floor: Contains multiple parking slots.
Parking Slot: Represents a unit where a vehicle can park.
Vehicle: Represents different types of vehicles (bike, car, truck).
Ticket: Tracks vehicle entry and exit.
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
Scalable: Supports multiple floors and vehicle types.
Extensible: Easily add features like reservations.
Maintainable: Clean segregation of responsibilities.
Future Enhancements
Add reservation features.
Integrate with payment gateways.
Add a mobile-friendly UI for ease of use.
Last updated