Introduction
Design Parking System problem is an easy difficulty question that has been asked in a few competitive coding platforms. The problem can be solved using various techniques in different languages. This article will discuss one of the most straightforward Java implementations of the Design Parking System.
Problem Statement
In this problem, we have to design a parking system for a parking lot. The parking system can contain three types of cars- big, medium, and small. We have to implement the class ParkingSystem with the following constraints:
- ParkingSystem(int big, int medium, int small): Constructor that takes the maximum limit of each car type that can be kept in the parking lot. Initialize the objects of the class with the incoming values.
- bool addCar(int carType): takes the type of car that needs to be added into the parking lot as an argument. The valid inputs for this function can be only one, two, and three. Any other value is not accepted. If a car can be added to the parking lot, return true. If no space is available in the parking lot, then return false.
Example and Explanation
INPUT
ParkingSystem[1,2,1]
addCar[1,2,2,3,1,2]
OUTPUT
[true, true, true, true, false, false]
In the input, we initialize the constructor with the values- [1, 2, 1]. This means that our parking lot can contain one big car, two medium cars, and one small car. We also pass the values [1, 2, 2, 3, 1, 2] to our addCar function. This means that we want to park two big cars, three medium cars and one small car in our parking lot.
The output obtained displays whether the car type can be parked or not. So, the cars that can be parked are:
- The first big car.
- The following two medium cars.
- The next small car.
The car that cannot be kept are:
- The second big car.
- The third medium car.