Requirements
- System should be able to park a car on a specific
floor.
- Spots are assigned to vehicles based on the size, spot availability.
- Fee for user is calculated based on parking duration.
Define Core Objects
- Parking Lot
- Vehicle
- Spot
- Floor
- Payment
- User
- Ticket
Analyze Relationships
- Floor has spots
- ParkingLot has floors
- Parking has a payment
- Car,Truck,Bike is a Vehicle
- Parking sport either has a vehicle or is vacant
- When a vehicle is parked ticket is generated recording the entry time.
- Users interact with the system based on their role.
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...
- We will create a parkingLot which will have attributes like Spot,floors etc.
- There will be Vehicle base class and classes like Car,Truck,Bike etc will inherit from base Vehicle class.
- There will be User class will special privilege. (Can be customer,Admin etc)
- There will be Ticket class
- There will be payment class which will generate receipt based on vehicle enrty,exit time listed on User Ticket.
Design Patterns
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
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.
from abc import ABC,abstractmethod
from enum import Enum
import threading
class VehcileType(Enum):
Car=1
Bike=2
Truck=3
class ParkingSpot:
def init(self,spotId,isOccupied):
self.spotId=spotId
self.isOccupied=isOccupied
class Vehicle:
def init(self,vehicleType,number,color,name):
self.vehicleType=vehicleType
self.number=number
self.color=color
self.name=name
class ParkingFloor:
level=-1
spots=[]
totalSpots=0
availableSpots=0
def __init__(self,level,totalSpots,availableSpots):
self.level=level
self.totalSpots=totalSpots
self.availableSpots=availableSpots
def add_spot(self,spot):
self.spots.append(spot)
def findAvailableSpots(self):
for spot in self.spots:
if spot.isOccupied==False:
return spot
class ParkingLot:
_instance=None
_lock=threading.Lock()
parkingFloor=[]
totalSpots=0
availableSpots=0
@classmethod
def get_instance(cls):
if not cls._instance:
with cls._lock:
if not cls._instance:
cls._instance=super().__new__(cls)
return cls._instance
def assignSpot(self,Vehicle,Floor): # assign spot to a vehicle on this floor
spot=Floor.findAvailableSpots()
if spot and spot.isOccupied==False:
spot.isOccupied=True
spot.Vehicle=Vehicle
print(f"Spot {spot.spotId} assigned with Vehicle {Vehicle.number}")
def releaseSpot(self,spot):
spot.isOccupied=False
p1=ParkingLot.get_instance()
v1=Vehicle(VehcileType.Car,1234,"RED","lola")
f1=ParkingFloor(1,10,10)
ps1=ParkingSpot(1,False)
f1.add_spot(ps1)
p1.assignSpot(v1,f1)
Adhere to SOLID Guidelines
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Consider Scalability and Flexibility
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
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
Critically examine your design for any flaws or areas for future improvement...