educative.io

How should one determine the ParkingSpot Type without using ParkingSpotType Enum

Let’s say I have a vehicle of type “car”, now i need to select a “Compact” parking spot available. how do I link a car to a compact parking spot. I have to use enums mapping.
Also I did not understand how assingVehicle(Vehicle vechile) will be different in each parkingspot.
If they are all going to be same then better to put it in base class.


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Class Diagram for the Parking Lot - Grokking the Low Level Design Interview Using OOD Principles

Hi @Anshul_Arzare !!
To link a car to a compact parking spot using enums mapping, you can create an enum for the types of vehicles and another enum for the types of parking spots. Here’s an example in Java:

public enum VehicleType {
    CAR
    // Add more vehicle types if needed
}

public enum ParkingSpotType {
    COMPACT,
    // Add more parking spot types if needed
}

Now, you can associate the vehicle type and parking spot type using these enums. In your code, when you want to assign a car to a compact parking spot, you can do something like this:

Vehicle car = new Vehicle(VehicleType.CAR);
ParkingSpot compactSpot = findCompactSpot(); // Assume you have a method to find a compact parking spot
compactSpot.assignVehicle(car);

In this example, the Vehicle class has a constructor that takes a VehicleType as a parameter. The ParkingSpot class has a method assignVehicle() that takes a Vehicle object as an argument. By passing the car object and the compact parking spot object, you can link the car to the compact parking spot.

Regarding your question about the assignVehicle() method, if all parking spots behave the same way when assigning a vehicle, then it can indeed be placed in the base class. However, if different types of parking spots have different behaviors or restrictions when assigning vehicles, you may want to override the assignVehicle() method in each specific parking spot subclass to handle those differences.

The decision of whether to place the assignVehicle() method in the base class or override it in the subclasses depends on the specific requirements and behaviors of your parking spot system. If all parking spots behave identically, it can be placed in the base class. But if different parking spot types have distinct rules or requirements, it’s better to override the method in the respective subclasses to accommodate those differences.
I hope it helps. Happy Learning :blush: