- Requirements
- Functional Requirements:
- Assign first-fitting spot - On arrival, find the earliest free spot that can fit the vehicle size across floors.
- Issue ticket on entry - Generate unique ticket with vehicle, spot, and entry time.
- Compute fee on exit - Calculate duration and apply active PricingStrategy.
- Lot full handling - If no suitable spot exists, indicate 'Lot Full'.
Core Objects & Relationships
Các đối tượng chính:
- Vehicle (xe): có
plate+size - ParkingSpot (chỗ đỗ): có
floor,number,capacity, trạng tháioccupied - Ticket (vé):
id,vehicle,spot,entryTime - PricingStrategy (chiến lược giá): interface tính phí dựa trên xe + thời gian
- ParkingLot (bãi xe): chứa danh sách spot theo tầng; xử lý
enter()vàexit()
Quan hệ:
ParkingLotquản lý nhiềuParkingSpotParkingLottạoTicketkhi xe vào, và dùngPricingStrategykhi xe raTicket“gắn” mộtVehiclevới mộtParkingSpottạientryTime
APIs & Class Members
For each class, define the attributes (data) it will hold and the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation. Write your code in the code editor below.
Deep Dive
Tradeoffs mình chọn
- First-fitting spot: duyệt từ tầng 1 → tầng N, spot 1 → spot M.
- Ưu: đúng yêu cầu “earliest”, dễ hiểu.
- Nhược: nếu cực nhiều spot, tìm spot có thể chậm (O(F*S)).
- Ticket ID dùng UUID:
- Ưu: unique dễ, không cần counter trung tâm.
- Nhược: dài; nếu muốn “vé ngắn” có thể dùng sequence + encode.
- PricingStrategy tách riêng:
- Ưu: đổi giá/đổi luật tính tiền không phải sửa ParkingLot.
SOLID
- S (Single Responsibility)
ParkingLotchỉ lo logic vào/ra + quản lý spot/ticket.PricingStrategychỉ lo tính phí.Ticket,ParkingSpot,Vehiclegiữ dữ liệu + hành vi liên quan trực tiếp.
- O (Open/Closed)
- Thêm cách tính phí mới: tạo class mới implements
PricingStrategy(không sửa ParkingLot).
- Thêm cách tính phí mới: tạo class mới implements
- L (Liskov Substitution)
Car extends Vehicle: có thể dùngCarở mọi nơi nhậnVehicle.
- I (Interface Segregation)
PricingStrategynhỏ, đúng mục đích.
- D (Dependency Inversion)
ParkingLotphụ thuộc vào abstractionPricingStrategy, không phụ thuộc class tính phí cụ thể.
Scale & mở rộng
- Nếu nhiều spot (rất lớn), tối ưu tìm spot bằng:
- Lưu available spots theo size trong các cấu trúc như
TreeSet / PriorityQueuetheo thứ tự (floor, number). - Khi occupy/release thì update set → tìm spot gần như O(log N) thay vì quét hết.
- Lưu available spots theo size trong các cấu trúc như
- Nếu nhiều cổng vào/ra (đa luồng):
- Cần đồng bộ (synchronization / locks) để tránh 2 xe lấy cùng 1 spot.
- Tách
SpotManagerđể quản lý concurrency tốt hơn.
- Dễ thêm tính năng:
- Reserved spot, VIP, handicap: thêm rule trong chọn spot hoặc thêm strategy chọn spot.
- Expired ticket, lost ticket handling.
- Analytics: lưu lịch sử vào/ra.