Skip to content
BytePatterns

Parking Lot Fee Calculator

EasyLow-Level Design#class-design#pricing-rules~15m

Problem

Design the pricing part of a parking lot as a small class. It is built with a rate table, mapping each vehicle kind to a price for the first hour and a price for every further started hour, and a daily cap per kind. fee(kind, minutes) returns what a stay costs: stays of 15 minutes or less are free, every full 24 hours costs exactly the daily cap, and the remaining part of a day is priced by the hourly rates but never above the cap. An unknown vehicle kind must raise a ValueError rather than be priced as something else.

Examples

Input:  rates = car (4, 2), motorbike (2, 1); caps = car 20, motorbike 8
        fee("car", 150)
Output: 8
Why:    first hour 4, then 90 more minutes start 2 more hours at 2 each
Input:  same lot, fee("car", 26 * 60)
Output: 26
Why:    one full day at the cap of 20, then 2 hours for 4 + 2
Input:  same lot, fee("motorbike", 10)
Output: 0
Why:    edge case, the stay is inside the 15-minute grace period

Hints

0 / 3

Stuck on the idea rather than the code? Designing a Parking Lot covers it.