Part 4 of 6 · 4 min read
The main flow
Park, exit, and what each call changes.
The core is shown here in pieces. It was compiled and run with Java 21.
The vehicle and the fit rule:
enum VehicleType { BIKE, CAR, TRUCK }
enum SpotType { SMALL, MEDIUM, LARGE }
record Vehicle(String plate, VehicleType type) {
Vehicle { Objects.requireNonNull(plate); Objects.requireNonNull(type); }
boolean fits(SpotType s) {
return switch (type) {
case BIKE -> true;
case CAR -> s != SpotType.SMALL;
case TRUCK -> s == SpotType.LARGE;
};
}
}
The spot guards its own state:
class Spot {
private final String id; private final int floor; private final SpotType type;
private Vehicle parked;
// constructor and getters omitted
boolean isFree() { return parked == null; }
void park(Vehicle v) {
if (!isFree() || !v.fits(type))
throw new IllegalStateException("cannot park " + v.plate() + " in " + id);
parked = v;
}
void free() { parked = null; }
}
The fee strategy, in whole rupees (made up rates: bike 10, car 40, truck 100 per hour):
interface FeeStrategy { long fee(VehicleType t, Duration stay); }
class HourlyFee implements FeeStrategy {
private final Map<VehicleType, Long> rate;
HourlyFee(Map<VehicleType, Long> rate) { this.rate = Map.copyOf(rate); }
public long fee(VehicleType t, Duration stay) {
long hours = Math.max(1, (stay.toMinutes() + 59) / 60); // started hours
return hours * rate.get(t);
}
}
The lot, with both public methods synchronized so that two gates cannot take the same spot (module 3 of Level 5):
record Ticket(String id, Vehicle vehicle, Spot spot, Instant entry) {}
class LotFullException extends Exception {
LotFullException(VehicleType t) { super("no free spot for " + t); }
}
class ParkingLot {
private final List<Spot> spots; private final Clock clock; private final FeeStrategy fees;
private final Map<String, Ticket> open = new HashMap<>();
private final Set<String> inside = new HashSet<>();
private int next = 1;
synchronized Ticket park(Vehicle v) throws LotFullException {
if (inside.contains(v.plate()))
throw new IllegalStateException(v.plate() + " is already parked");
Spot best = null;
for (Spot s : spots) {
if (!s.isFree() || !v.fits(s.type())) continue;
if (best == null || s.type().ordinal() < best.type().ordinal()) best = s;
}
if (best == null) throw new LotFullException(v.type());
best.park(v); inside.add(v.plate());
Ticket t = new Ticket("T" + next++, v, best, clock.now());
open.put(t.id(), t);
return t;
}
synchronized long exit(String ticketId) {
Ticket t = open.get(ticketId);
if (t == null) throw new NoSuchElementException("unknown ticket " + ticketId);
long fee = fees.fee(t.vehicle().type(), Duration.between(t.entry(), clock.now()));
open.remove(ticketId); t.spot().free(); inside.remove(t.vehicle().plate());
return fee;
}
}
A full lot is a checked exception, since the gate can respond, for example by showing "lot full" (Level 5, module 1). A duplicate plate or an unknown ticket is a caller error and is unchecked. The state changes come after every check, so a failed call changes nothing.
A run with four spots: small 1-S1, medium 1-M1, large 2-L1 and medium 2-M1, and a fixed clock at 09:00 (made up).
| Step | Result |
|---|---|
| Bike B1, car C1, truck T1 arrive | spots 1-S1, 1-M1, 2-L1 |
| Car C2 arrives | 2-M1, the last medium spot |
| Car C3 arrives | refused: "no free spot for CAR" |
| C1 tries to park again | refused: "C1 is already parked" |
| 130 minutes pass; car C1 and bike B1 exit | fees 120 and 30 |
| Free medium and small spots | 1 and 1 |
| Ticket "T99" exits | "unknown ticket T99" |
| C1's ticket exits a second time | "unknown ticket T2" |
| Car C3 arrives again | 1-M1, the freed medium spot |
The fee for C1 is 3 started hours at 40, which is 120. For B1 it is 3 at 10, which is 30. Because the clock is injected, the test advanced time by 130 minutes in no time.
Check yourself:
1. Why is LotFullException checked and the duplicate plate unchecked?
2. What fee does a truck pay for 61 minutes at 100 per hour?
3. Why are park and exit synchronized?