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

  • UrlShortenerService: “đầu não” xử lý 2 use case: createShortUrl()resolveAndRedirect()
  • ShortCodeGenerator (interface): tạo code (có thể đổi chiến lược)
  • UrlRepository (interface): lưu/đọc mapping code -> longUrl
  • InMemoryUrlRepository (implementation): demo cho bài (dễ test)
  • UrlMapping: object dữ liệu (code, longUrl, createdAt, optional expireAt)
  • Exceptions:
    • InvalidUrlException: URL đầu vào sai
    • NotFoundException: code không tồn tại
    • AlreadyExistsException: custom alias bị trùng (nếu có)
    • StorageException: lỗi lưu/đọc

Quan hệ:

  • UrlShortenerService phụ thuộc vào abstraction UrlRepositoryShortCodeGenerator (SOLID: DIP).
  • UrlRepository lưu và trả UrlMapping.
  • ShortCodeGenerator tạo code, service kiểm tra collision bằng repository.


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

Use case 1: Create short URL (write path)

  1. Client gọi UrlShortenerService.createShortUrl(longUrl)
  2. Service validate URL (http/https + host)
  3. Service gọi codeGenerator.generateCode()
  4. Service kiểm tra collision bằng repository.exists(code) → nếu trùng thì retry
  5. Service tạo UrlMapping(code, longUrl, now)repository.save(mapping)
  6. Trả mapping → client ghép thành https://sho.rt/{code}

Use case 2: Redirect / get long URL (read path)

  1. Client truy cập GET /{code}
  2. Controller/handler gọi UrlShortenerService.getLongUrl(code)
  3. Service gọi repository.findByCode(code)
  4. Có → trả longUrl → handler trả HTTP 301/302 redirect
  5. Không có → ném NotFoundException → handler trả 404

Error handling & exception management (đúng ý feedback)

Expected errors

  • URL rỗng/sai định dạng → InvalidUrlException → trả 400
  • code rỗng → InvalidCodeException → trả 400
  • code không tồn tại → NotFoundException → trả 404
  • collision quá nhiều / lỗi lưu DB → StorageException → trả 500 (hoặc 503)

Unexpected errors

  • Repository throw lỗi hệ thống → wrap thành StorageException để không “rò rỉ” chi tiết.