Design Parking System

Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

1.ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space is given as part of the constructor.

2.bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1,2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.

Example:

Input
[[1, 1, 0], [1], [2], [3], [1]]
Output
[true, true, false, false]

Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

class ParkingSystem
{
public:
    int n1n2n3;
    ParkingSystem()
    {
    }
    ParkingSystem(int bigint mediumint small)
    {
        n1 = big;
        n2 = medium;
        n3 = small;
    }

    bool addCar(int carType)
    {
        if (carType == 1 && n1 > 0)
        {
            n1--;
            return true;
        }
        else if (carType == 1 && n1 == 0)
            return false;
        else if (carType == 2 && n2 > 0)
        {
            n2--;
            return true;
        }
        else if (carType == 2 && n2 == 0)
            return false;
        else if (carType == 3 && n3 > 0)
        {
            n3--;
            return true;
        }
        else
            return false;
    }
};

int main()
{
    ParkingSystem parkingSystem(110);
    cout << "[";
    if (parkingSystem.addCar(1))
        cout << "true, ";
    else
        cout << "false, ";
    if (parkingSystem.addCar(2))
        cout << "true, ";
    else
        cout << "false, ";
    if (parkingSystem.addCar(3))
        cout << "true, ";
    else
        cout << "false, ";
    if (parkingSystem.addCar(1))
        cout << "true";
    else
        cout << "false";
    cout << "]";
    return 0;
}


No comments:

Post a Comment