Design Parking System

To solve this coding challenge, we need to design a class
ParkingSystem
that initializes a parking lot with three types of parking spaces (big, medium, and small). Each of these spaces has a fixed capacity. The class also needs a method
addCar
which checks if there's an available parking slot for a car of a given type and parks the car if possible, then returns a boolean indicating success or failure.

Explanation

  1. Class Initialization :
    • The class should accept three integers representing the initial numbers of big, medium, and small parking slots available.
    • We will store these values in variables within the class so that they can be accessed and modified by the methods.
  2. Method
    addCar
    :
    • This method will receive an integer (
      carType
      ) which can be 1 for big, 2 for medium, or 3 for small cars.
    • The method will check if there is an available slot for the given car type.
      • If a slot is available (
        number of slots > 0
        ), decrement the slot count for that type and return
        true
        .
      • If no slot is available (
        number of slots == 0
        ), return
        false
        .
    To implement this in pseudocode:
  3. Initialization :
    • Define a class
      ParkingSystem
      .
    • Create an initializer method to set up the parking slots.
  4. Method to Add a Car :
    • Define a method to check and update parking slot availability based on the car type.

Detailed Steps in Pseudocode

Initialization:
                                            
# Define the ParkingSystem class
class ParkingSystem:
    # Initialize the class with big, medium, and small slots
    method initialize(big_slots, medium_slots, small_slots):
        # Set the instance variables to the provided slot counts
        set big_slot_count to big_slots
        set medium_slot_count to medium_slots
        set small_slot_count to small_slots

                                        
Method to Add a Car:
                                            
    # Define the addCar method that checks for available slots and parks the car if possible
    method addCar(carType):
        # If the car is a big car (type 1)
        if carType is 1:
            # Check if any big slots are available
            if big_slot_count is greater than 0:
                # Decrement the number of available big slots
                decrement big_slot_count by 1
                # Return true indicating the car was parked
                return true
            else:
                # Return false indicating no available slots
                return false
        
        # If the car is a medium car (type 2)
        if carType is 2:
            # Check if any medium slots are available
            if medium_slot_count is greater than 0:
                # Decrement the number of available medium slots
                decrement medium_slot_count by 1
                # Return true indicating the car was parked
                return true
            else:
                # Return false indicating no available slots
                return false
        
        # If the car is a small car (type 3)
        if carType is 3:
            # Check if any small slots are available
            if small_slot_count is greater than 0:
                # Decrement the number of available small slots
                decrement small_slot_count by 1
                # Return true indicating the car was parked
                return true
            else:
                # Return false indicating no available slots
                return false

                                        
Usage Example in Pseudocode:
                                            
# Create an instance of the ParkingSystem with 1 big slot, 1 medium slot, and 0 small slots
parkingSystem = new ParkingSystem(1, 1, 0)

# Try to add a big car and check if it was successful
assert parkingSystem.addCar(1) equals true # returns true, there is 1 available big slot

# Try to add a medium car and check if it was successful
assert parkingSystem.addCar(2) equals true # returns true, there is 1 available medium slot

# Try to add a small car and check if it was successful
assert parkingSystem.addCar(3) equals false # returns false, there are no available small slots

# Try to add another big car and check if it was successful
assert parkingSystem.addCar(1) equals false # returns false, there are no remaining big slots

                                        
This pseudocode breaks down the operation of the
ParkingSystem
class and its
addCar
method, illustrating the mechanics involved in managing parking spaces.