Learn practical skills, build real-world projects, and advance your career

Object Oriented Programming

Table Of Contents

Use Case 1: Parking lot implementation using Classes and Objects

Use Case 1: Implementation of Parking lot using Classes and Objects

Implementing functionality for parking lot where users can:

-- Declare number of parking spots.

-- Divide them based on bikes, cars etc.

-- Park a vehicle if spot available.

-- Exit a vehicle.

# Abstract Class --> ParkingLot

class ParkingLot:
    
    def addTotalAvailableSlots(self, totalSlots):
        """This method will be used to add total number of slots"""
        pass
    
    def divideSlotsbyVehicleType(self, totalSlots, vehicleType, numOfDesiredSlots):
        """This method will be used to add total slots between vehicle types"""
        pass
    
    def park(self, vehicleType, vehicleNum, parkingStartTime):
        """This method will be used to park the vehicle"""
        pass
    
    def exitParking(self, vehicleNum):
        """This method will exit the slot (and return the fare)"""
        pass
# Model

class ParkingLotModel:
    TOTAL_SLOTS = None
    
    # Parking Lot is represented as a sorted dictionary, where
    # keys represent the slot and the values represent the number, vehicle type
    parkingLot = {}
    
    def __init__(self, totalSlots):
        self.TOTAL_SLOTS = totalSlots
        print("self.TOTAL_SLOTS: ",self.TOTAL_SLOTS)
        
    def parkVehicle(self, slotRange, vehicleNumber, startTime):
        bookedSlots = self.parkingLot.keys()
        for slotNumber in range(slotRange[0], slotRange[1]):
            if slotNumber not in bookedSlots:
                self.parkingLot[slotNumber] = (vehicleNumber, startTime)
                print("Parking Lot: ", self.parkingLot)
                return "vehicle number: "+str(vehicleNumber)+" parked at "+str(slotNumber)
        
    def checkAvailability(self, slotRange):
        bookedSlots = self.parkingLot.keys()
        print("bookedSlots: ",bookedSlots)
        for slot in range(slotRange[0], slotRange[1]):
            if slot not in bookedSlots:
                return True
            return False