- 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
- UrlShortenerService: “đầu não” xử lý 2 use case:
createShortUrl()và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 saiNotFoundException: code không tồn tạiAlreadyExistsException: custom alias bị trùng (nếu có)StorageException: lỗi lưu/đọc
Quan hệ:
UrlShortenerServicephụ thuộc vào abstractionUrlRepositoryvàShortCodeGenerator(SOLID: DIP).UrlRepositorylưu và trảUrlMapping.ShortCodeGeneratortạ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)
- Client gọi
UrlShortenerService.createShortUrl(longUrl) - Service validate URL (http/https + host)
- Service gọi
codeGenerator.generateCode() - Service kiểm tra collision bằng
repository.exists(code)→ nếu trùng thì retry - Service tạo
UrlMapping(code, longUrl, now)vàrepository.save(mapping) - Trả mapping → client ghép thành
https://sho.rt/{code}
Use case 2: Redirect / get long URL (read path)
- Client truy cập
GET /{code} - Controller/handler gọi
UrlShortenerService.getLongUrl(code) - Service gọi
repository.findByCode(code) - Có → trả
longUrl→ handler trả HTTP 301/302 redirect - 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.