Requirements
Functional Requirements:
- Assign parking spots based on vehicle size, allowing motorcycles to park in small spots, cars in medium spots, and trucks in large spots, with the option to park in larger spots if needed.
- Issue a ParkingTicket upon vehicle entry, which records the vehicle information, assigned parking spot, and entry timestamp.
- Compute the parking fee upon vehicle exit based on the duration of stay and a configurable pricing strategy.
- Handle scenarios where the parking lot is full by rejecting entry and displaying a lot-full indicator to the driver.
- Support multiple floors in the parking lot, with each floor containing a mix of small, medium, and large parking spots.
- If a parking spot of the corresponding size is not available, the next bigger spot if available should be allocated.
Non-Functional Requirements:
- Extensibility
- We should be able to add parking spots of different sizes for eg: a sedan size that's in between a car and a truck or an XL size parking spot for a bus
- We should be able to add different pricing strategies. Some parking lots would want to charge based on the spot size and others might choose to charge based on the vehicle size
- We should be able to do the above without modifying existing code
- Thread safety
- When two cars try to enter at the same time, they should be assigned different spots if available and if not, one of them should be shown parking full
- Update of parking spot status should be done in thread safe manner. No two threads should be accessing the same parking spot.
- Each gate should be assigned a separate thread and since it's possible that multiple threads might end up reserving the same parking spot, we need to ensure that the entry in table corresponding to that parking spot should be updated in thread safe manner
Core Objects & Relationships
Based on the requirements and use cases, identify the main objects of the system and analyze how they interact and relate to each other...
Following are the entity classes
- ParkingLot
- ParkingSpot
- Vehicle
- ParkingTicket
Following are the utility classes
- FeesCalculator
- GateUtility
- FeeStrategy
ParkingLot
- ParkingLot class contains a list of ParkingSpots and list of active parking tickets
- Responsibility - Contains the overall list of all parkingspots. Will be used by the utility classes to decide which spots are empty. This class by itself does not contain any intelligence. It's only used to store the list of all parking spot objects
- Each parking lot contains an id that is globally unique
ParkingFloor
- Uniquely identifies the floor in the parking lot
ParkingSpot
- ParkingSpot contains the info about the size of the parking spot , which parkingfloor, unique id of the parking spot ( local to that parking lot ). It also contains a flag that indicates whether it's occupied or not
- Responsibility - Contains all the details about the parking spot and its current state ( available or not). It does not possess any other intelligence
Vehicle
- It is an interface that can be implemented by any type of vehice - 2 wheeler, car , truck and so on. It contains a registration id
- Responsibility - This class contains information about how to identify the vehicle. the type of the object will indicate its size.It does not possess any other intelligence
ParkingTicket
- It contains the info about the car ( registration id ), parking spot id, entry time.
- The FeeCalculator utility will calculate based on the parking ticket's entry time and the fee strategy
- Once a parkingTicket is created, it will be added to the list of active tickets in ParkingSlot
Utility Classes
GateUtility
- This utility is used to allow or reject a car at the gate. It will lookup the ParkingLot list of spots to find out availability and decide
- This utility will also generate a parkingTicket and add it to the ParkingLot's list of tickets and also give it to the caller
FeeCalculator
- FeeCalculator will take a parkingTicket and calculate the fees based on the exit time and the fee strategy. Fee Strategy is an interface that is implemented by multiple classes for eg: SmallSpotFeeStrategy, LargeSpotFeeStrategy and so on. The corresponding strategy is loaded by FeeCalculator based on the ParkingSpot which can be retrieved from the parkingTicket
APIs & Class Members
For each class, define the attributes (data) it will hold and the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation. Write your code in the code editor below.
Deep Dive
Explain design tradeoffs you considered. Check and explain whether your design adheres to SOLID principles. Explain how your design can handle changes in scale and whether it would be easy to extend with new functionalities. Identify areas for future improvement...