Final challenge: extend the lot
After this: Add a weekend rate and an electric-car spot, predict new runs, and complete the line that decides whether a vehicle fits.
The owner of the lot is back with two new requests: a weekend rate, and spots with chargers for electric cars. This challenge has five steps, and each step is scored on its own. You will choose where the weekend rule goes, predict the weekend fees, choose where the charger rule goes, predict a new run of the lot, and write the line that decides whether a vehicle fits a spot. Code is shown in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Where the change goes
Where the change goes
New requirement: on weekends, every started hour costs double. Where should this change go?
Predict the weekend fees
WeekendFees is built on FeeCalculator and doubles its answer. A car stays from minute 0 to minute 61, and a bike from minute 10 to minute 55. What does this print?
class WeekendFees(FeeCalculator): def fee(self, ticket, exit_minute): return 2 * super().fee(ticket, exit_minute)
fees = WeekendFees()print(fees.fee(Ticket(1, Car("C1"), Spot("M1", Size.MEDIUM), 0), 61))print(fees.fee(Ticket(2, Bike("B1"), Spot("S1", Size.SMALL), 10), 55))class WeekendFees extends FeeCalculator { @Override int fee(Ticket t, int exitMinute) { return 2 * super.fee(t, exitMinute); }}
// in main:FeeCalculator fees = new WeekendFees();System.out.println(fees.fee(new Ticket(1, new Car("C1"), new Spot("M1", Size.MEDIUM), 0), 61));System.out.println(fees.fee(new Ticket(2, new Bike("B1"), new Spot("S1", Size.SMALL), 10), 55));Add an electric-car spot
New requirement: some medium spots have a charger, and an electric car must park on a spot with a charger. Which change fits the design best?
Predict a new run
A new, empty lot with the same six spots. Four cars arrive at minute 0, then a truck at minute 5. What does this print?
lot = ParkingLot( [ Level(1, [Spot("S1", Size.SMALL), Spot("S2", Size.SMALL), Spot("M1", Size.MEDIUM), Spot("M2", Size.MEDIUM)]), Level(2, [Spot("M3", Size.MEDIUM), Spot("L1", Size.LARGE)]), ], FeeCalculator(),)
for plate in ["C1", "C2", "C3", "C4"]: lot.park(Car(plate), 0)lot.park(Truck("T1"), 5)print("free", lot.free_count(Size.SMALL), lot.free_count(Size.MEDIUM), lot.free_count(Size.LARGE))ParkingLot lot = new ParkingLot( List.of( new Level(1, List.of(new Spot("S1", Size.SMALL), new Spot("S2", Size.SMALL), new Spot("M1", Size.MEDIUM), new Spot("M2", Size.MEDIUM))), new Level(2, List.of(new Spot("M3", Size.MEDIUM), new Spot("L1", Size.LARGE)))), new FeeCalculator());
for (String plate : List.of("C1", "C2", "C3", "C4")) { lot.park(new Car(plate), 0);}lot.park(new Truck("T1"), 5);System.out.println("free " + lot.freeCount(Size.SMALL) + " " + lot.freeCount(Size.MEDIUM) + " " + lot.freeCount(Size.LARGE));Write the fits line
Complete the fits method of Spot: a vehicle fits when the spot is free and the vehicle's size is at most the spot's size. With the right line, the whole run prints collected 110 and free 1 1 0.
class Spot: def __init__(self, spot_id, size): self.spot_id = spot_id self.size = size self.vehicle = None
def is_free(self): return self.vehicle is None
def fits(self, v): ____________________ # ← free, and big enough
def assign(self, v): self.vehicle = v
def release(self): self.vehicle = Noneclass Spot { final String id; final Size size; private Vehicle vehicle;
Spot(String id, Size size) { this.id = id; this.size = size; }
boolean isFree() { return vehicle == null; }
boolean fits(Vehicle v) { ____________________ // ← free, and big enough }
void assign(Vehicle v) { vehicle = v; }
void release() { vehicle = null; }}- Put a new fee rule in a new fee class handed to the lot, leaving ParkingLot unchanged.
- Predict fees under a new rule that builds on the old one.
- Put a new rule about which vehicle may use which spot into fits, the one place that decides it.
- Predict a new run of the lot, and write the fits line in Python or Java.