Requirements


  • A parking lot consists of many spaces on many floors.
  • A spot can be identified uniquely.
  • A space may not be rented to more than one car at a given time.
  • A driver may rent a space for the size of their car.
  • A driver may rent a space for a calculated fee based on how long the space is rented for.
  • A driver needs to be told if a spot is available for a given size vehicle.


Assumptions:

  • Let's assume the parking lot has 3 levels.
  • Each level has 3 spaces.
  • The parking lot has 3 spaces for small cars, medium cars, and large cars, respectively.
  • A space is rented for $10/hr regardless of size.
  • No 2 cars may rent the same parking lot at the same time.
  • Let's assume drivers leave their space after their allotted duration is up.
  • One driver can rent a space at a time.




Define Core Objects

Lot - object to represent a particular lot in the parking lot. Has an id, a rate, a size, whether the lot if available to lease, when the lot is leased, and who leased it.

ParkingLot - responsible for managing the lots. It knows how many lots exist on each level. It can answer whether a lot is available for a size and duration. It can mark a lot as leased for a size and driver. It can mark a lot as returned.

ParkingLotFactory - responsible for creating lots and the parking lot.




Analyze Relationships

A ParkingLot has many Lots.





Establish Hierarchy

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...






Design Patterns

Factory pattern to create the Lots and ParkingLot instances.





Define Class Members (write code)

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.


public class Car { ... } // Example






Adhere to SOLID Guidelines

Single Responsibility Principle:

  • A Lot is responsible for knowing its size, rate, who leased it, when it was leased, and whether it is available for lease. It is also responsible for calculating its fee.
  • A ParkingLot is responsible for managing a collection of Lots. It can mark a lot as leased, can determine available lots, and can return a lot to available.

Open closed principle:

  • A lot can accept new attributes without modification to other classes.
  • A ParkingLot can hold more lots without modification.





Consider Scalability and Flexibility

The design could extend to include a LotFactory if lot rates or lot sizes were to change in the future. A ParkingLot could hold a LotFactory to create lots instead of the ParkingLotFactory. If more than one lot may be rented at the same time, concurrency would need to be implemented. Instead of an array of Lots, suggest to use a synchronised list instead.





Create/Explain your diagram(s)

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...






Future improvements

  • Allow for concurrent lot leases