Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
Based on the requirements and use cases, identify the main objects of the system...
Determine how these objects will interact with each other to fulfill the use cases...
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
from abc import ABC, abstractmethod
from enum import Enum
class VehicleSize(Enum):
MOTORCYCLE = 0
COMPACT = 1
LARGE = 2
def can_fit_in(self, spot_size: VehicleSize) -> bool:
return self.value <= spot_size.value
class Vehicle(ABC):
def init(self, license_plate: str, model: str, brand: str):
self.license_plate = license_plate
self.model = model
self.brand = brand
@property
@abstractmethod
def size(self) -> VehicleSize:
pass
class MotorcycleVehicle(Vehicle):
@property
def size(self):
return VehicleSize.MOTORCYCLE
class CompactVehicle(Vehicle):
@property
def size(self):
return VehicleSize.COMPACT
class LargeVehicle(Vehicle):
@property
def size(self):
return VehicleSize.LARGE
class Driver:
def init(self, vehicle: Vehicle, license: str, is_handicapped: bool):
super().init(is_handicapped)
self.vehicle = vehicle
self.is_handicapped = is_handicapped
class ParkingSpot(ABC):
spot_size: VehicleSize
def init(self, spot_id: int):
self.spot_id = spot_id
self.is_occupied = False
self.driver: Driver = None
self._is_handicap_only = False
pass
@property
@abstractmethod
def spot_size(self) -> VehicleSize:
pass
def can_accommodate(self, driver: Driver) -> bool:
if self._is_handicap_only and not driver.is_handicapped:
return False
return driver.vehicle.spot_size.can_fit_in(self.spot_size)
def assign_spot(self, driver: Driver):
if self.is_occupied:
return False
if not self.can_accommodate(driver):
return False
self.driver = driver
self.is_occupied = True
return True
class MotorcycleSpot(ParkingSpot):
def init(self, spot_id: int):
super().init(spot_id)
@property
def spot_size(self):
return VehicleSize.MOTORCYCLE
class CompactSpot(ParkingSpot):
def init(self, spot_id: int):
super().init(spot_id)
@property
def spot_size(self):
return VehicleSize.COMPACT
class LargeSpot(ParkingSpot):
def init(self, spot_id: int):
super().init(spot_id)
@property
def spot_size(self):
return VehicleSize.LARGE
class HandicapSpot(LargeSpot):
def init(self, spot_id: int):
super().init(spot_id)
self._is_handicap_only = True
class ParkingLevel:
def init(self, level_number: int, spot_counts: dict[ParkingSpot, int]):
self._spots = dict[VehicleSize, list[ParkingSpot]]
self.level_number = level_number
for parking_spot, cnt in spot_counts.items():
self_spots[spot_size] = [parking_spot(i) for i in range(1, count+1)]
def find_available_spot(self, driver: Driver) -> Optional[ParkingSpot]:
for spot_size, spots in self._spots.items():
for spot in spots:
if spot.is_occupied or not spot.can_accomodate(driver):
continue
return spot
def available_spots(self) -> dict[VehicleSize, int]:
return {
spot_size, sum(1 for spot in spots if not spot.is_occupied)
for spot_size, spots in _spots.items()
}
class ParkingLot:
def init(self, spots_per_level: list[dict[VehicleSize, int]]):
self.levels: dict[int, ParkingLevel] = {}
for i, spot_counts in enumerate(spots_per_level):
self.levels[i] = ParkingLevel(i, spot_counts)
def park_vehicle(self, driver: Driver):
for level_num, level in self.levels.items():
if (spot := level.find_available_spot(driver)):
is_assigned = spot.assign_spot(driver)
if is_assigned:
return ParkingTicket(driver, spot)
class TicketStatus(Enum):
ACTIVE = 'active'
PAID = 'paid'
EXPIRED = 'expired'
class ParkingTicket:
def init(self, driver: Driver, spot: ParkingSpot):
self.ticket_id = self._generate_ticket_id()
self.license_plate = driver.vehicle.license_plate
self.parking_spot = spot
self.entry_time = datetime.Now()
self.exit_time = None
self.duration = None
self.status = TicketStatus.ACTIVE
self.amount_due = None
self.payment_id = None
def _generate_ticket_id(self):
// some UUID generator - maybe SHA256
pass
def _calculate_duration(self):
if self.exit_time is None:
self.exit_time = datetime.Now()
self.duration = self.exit_time - self.entry_time
return self.duration
def calculate_fee(self, rate_calculator: RateCalculator):
if self.amount_due > 0:
return self.amount_due
if self.duration == 0:
self._calculate_duration()
self.amount_due = RateCalculator(spot=self.parking_spot, duration=self.duration)
return amount_due
def mark_as_paid(self, payment_id: int):
self.status = TicketStatus.PAID
self.payment_id = payment_id
self.parking_spot.is_occupied = False
self.parking_spot.driver = None
def is_valid(self):
return self.status == TicketStatus.ACTIVE
class RateCalculator:
fee_strategies = {
HandicapSpot: HandicapFeeStrategy(),
MotorcycleSpot: MotorcycleFeeStrategy(),
CompactSpot: CompactFeeStrategy(),
LargeSpot: LargeFeeStrategy(),
}
@classmethod
def calculate_fee(cls, spot: ParkingSpot, duration: timedelta):
return cls.fee_strategiestype(spot)
class TicketSpotMultiplier(Enum):
// Hourly Rates
MOTORCYCLE = 2.0
COMPACT = 2.5
LARGE = 3.0
@classmethod
def get_multiplier(cls, size: VehicleSize):
return cls[size.name].value
class ParkingFeeStrategy(ABC):
@abstractclass
def calculate_fee(self, duration: timedelta) -> float:
pass
class CompactFeeStrategy(ParkingFeeStrategy):
spot_size = VehicleSize.COMPACT
def calculate_fee(self, duration: timedelta) -> float:
return TicketSpotMultipler.get_multipler(CompactFeeStrategy.spot_size) * duration
class MotorcycleFeeStrategy(ParkingFeeStrategy):
spot_size = VehicleSize.MOTORCYCLE
def calculate_fee(self, duration: timedelta) -> float:
return TicketSpotMultipler.get_multipler(MotorcycleFeeStrategy.spot_size) * duration
class LargeFeeStrategy(ParkingFeeStrategy):
spot_size = VehicleSize.LARGE
def calculate_fee(self, duration: timedelta) -> float:
return TicketSpotMultipler.get_multipler(LargeFeeStrategy.spot_size) * duration
class HandicapFeeStrategy(LargeFeeStrategy):
spot_size = VehicleSize.LARGE
def calculate_fee(self, duration: timedelta) -> float:
return TicketSpotMultipler.get_multipler(HandicapFeeStrategy.spot_size) * duration / 2
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...