1. Requirements
  2. Functional Requirements:
  3. Assign first-fitting spot - On arrival, find the earliest free spot that can fit the vehicle size across floors.
  4. Issue ticket on entry - Generate unique ticket with vehicle, spot, and entry time.
  5. Compute fee on exit - Calculate duration and apply active PricingStrategy.
  6. 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ái occupied
  • 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()exit()

Quan hệ:

  • ParkingLot quản lý nhiều ParkingSpot
  • ParkingLot tạo Ticket khi xe vào, và dùng PricingStrategy khi xe ra
  • Ticket “gắn” một Vehicle với một ParkingSpot tại entryTime



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)
    • ParkingLot chỉ lo logic vào/ra + quản lý spot/ticket.
    • PricingStrategy chỉ lo tính phí.
    • Ticket, ParkingSpot, Vehicle giữ 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).
  • L (Liskov Substitution)
    • Car extends Vehicle: có thể dùng Car ở mọi nơi nhận Vehicle.
  • I (Interface Segregation)
    • PricingStrategy nhỏ, đúng mục đích.
  • D (Dependency Inversion)
    • ParkingLot phụ thuộc vào abstraction PricingStrategy, 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 / PriorityQueue theo 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.
  • 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.