Parking Lot System
Problem Statement
Section titled “Problem Statement”Design a parking lot system that can manage multiple levels of parking, different vehicle types (motorcycle, car, truck), and handle parking/unparking operations. The system should efficiently allocate parking spots, calculate fees, and track parking availability.
Requirements
Section titled “Requirements”Functional Requirements
Section titled “Functional Requirements”- Support multiple vehicle types: Motorcycle, Car, Truck
- Handle multiple parking levels/floors
- Different spot sizes: Compact, Regular, Large
- Assign nearest available spot to vehicles
- Calculate parking fees based on duration
- Track spot availability in real-time
- Generate parking tickets with unique ID
- Support payment processing
- Handle entry and exit gates
Non-Functional Requirements
Section titled “Non-Functional Requirements”- Thread-safe for concurrent parking operations
- Fast spot lookup (O(1) or O(log n))
- Scalable to handle thousands of spots
- High availability
- Accurate fee calculation
Simplified Class Diagram
Section titled “Simplified Class Diagram”Simplified Overview
Section titled “Simplified Overview”Detailed Class Diagram
Section titled “Detailed Class Diagram”Key Design Patterns
Section titled “Key Design Patterns”- Singleton Pattern: ParkingLot ensures single instance
- Factory Pattern: Create different vehicle types
- Strategy Pattern: Different payment methods
- State Pattern: Parking spot status management
Design Pattern Diagrams
Section titled “Design Pattern Diagrams”1. Strategy Pattern - Fee Calculation & Spot Assignment
Section titled “1. Strategy Pattern - Fee Calculation & Spot Assignment”2. Factory Pattern - Vehicle Creation
Section titled “2. Factory Pattern - Vehicle Creation”3. State Pattern - Parking Spot Status
Section titled “3. State Pattern - Parking Spot Status”Code Snippets
Section titled “Code Snippets”Parking a Vehicle
Section titled “Parking a Vehicle”public class ParkingLot { public ParkingTicket parkVehicle(Vehicle vehicle) { synchronized(this) { ParkingSpot spot = findAvailableSpot(vehicle); if (spot == null) { throw new NoAvailableSpotException("No available spot for " + vehicle.getType()); }
spot.assignVehicle(vehicle); ParkingTicket ticket = new ParkingTicket(vehicle, spot); activeTickets.put(ticket.getTicketId(), ticket);
return ticket; } }
private ParkingSpot findAvailableSpot(Vehicle vehicle) { for (ParkingFloor floor : floors) { ParkingSpot spot = floor.findAvailableSpot(vehicle); if (spot != null) { return spot; } } return null; }}Spot Availability Check
Section titled “Spot Availability Check”public class ParkingSpot { public boolean canFitVehicle(Vehicle vehicle) { if (status != ParkingSpotStatus.AVAILABLE) { return false; }
switch(vehicle.getType()) { case MOTORCYCLE: return true; // Can fit in any spot case CAR: return type != SpotType.COMPACT; case TRUCK: return type == SpotType.LARGE; default: return false; } }}Fee Calculation
Section titled “Fee Calculation”public class ParkingTicket { public double calculateFee(ParkingRate rate) { if (exitTime == null) { exitTime = DateTime.now(); }
long durationMillis = exitTime.getMillis() - entryTime.getMillis(); double hours = Math.ceil(durationMillis / (1000.0 * 60 * 60));
this.fee = rate.calculateFee(vehicle, hours); return this.fee; }}Extension Points
Section titled “Extension Points”- Add reservation system for spots
- Implement dynamic pricing based on demand
- Add electric vehicle charging spots
- Implement valet parking service
- Add mobile app integration with QR code scanning
- Support monthly/annual parking passes
- Add parking spot navigation system