Assignment: Find closest available locker that fits the package.
OTP Generation: Secure 6-digit code for pickup.
Expiration: Auto-return packages not picked up in X days.
Sizes: Handle Small, Medium, Large, XL.
3. What are the constraints?
Optimization: Minimize distance for user.
Efficiency: Don't put a Small package in an XL locker if a Small locker is available (Best Fit).
Phase 2: Use Cases
UC1: Assign Locker (Order Placement)
Actor: System Flow:
User selects "Pick up from Locker" and provides Location.
System identifies Package Size.
System searches for nearest Locker Bank with available slot.
System reserves the slot.
System returns Locker Bank ID to User.
UC2: Deposit Package
Actor: Delivery Agent Flow:
Agent scans package at Locker Bank.
System opens the assigned locker door.
Agent places package and closes door.
System notifies User with OTP.
UC3: Pickup Package
Actor: Customer Flow:
Customer enters OTP at Locker Bank console.
System validates OTP.
System opens specific locker door.
System marks locker as "Free" upon door close.
Phase 3: Class Diagram
Step 1: Core Entities
LockerService: Facade for assignment.
LockerBank: Physical location containing lockers.
Locker: Individual slot.
Package: Item to be stored.
NotificationService: Sends OTP.
UML Diagram
Phase 4: Design Patterns
1. Strategy Pattern
Description: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Why used: The locker allocation logic can vary (Random, Best Fit, Nearest Location). Strategy allows us to plug in different algorithms (e.g., BestFitStrategy to minimize wasted space) without modifying the core LockerService.
2. Repository Pattern
Description: Abstraction of the data layer, mediating between the domain and data mapping layers.
Why used: To de-couple the business logic (assignLocker) from the underlying data access details (SQL, NoSQL, or In-Memory).
Phase 5: Code Key Methods
Java Implementation
Phase 6: Discussion
Concurrency
Q: Handling race conditions (Two users booking same locker)?
A: "Use Optimistic Locking (versioning) on the Locker row via DB. Or SELECT ... FOR UPDATE SKIP LOCKED which is standard for queue/allocation systems to grab the next available item without blocking others."
Extensions
Q: How to handle returns (expired packages)?
A: "Run a cron job nightly: SELECT * FROM Lockers WHERE expiry < NOW(). Mark status as RETURNING. Notify delivery agent to pick up. Once verified pickup, free the locker."
Q: What if locker door is broken?
A: "Locker needs a State (ACTIVE, BROKEN). Filter out BROKEN lockers during assignment."
SOLID Principles Checklist
S (Single Responsibility): Locker manages its state, Bank manages collection, Service manages allocation.
O (Open/Closed): New FittingStrategy (e.g., First Fit) can be added without changing Bank logic.
L (Liskov Substitution): Locker subtypes (e.g., RefrigeratedLocker) could be used.
I (Interface Segregation): Not heavily used.
D (Dependency Inversion): Service could depend on ILockerRepository instead of in-memory list.