c++ – Leetcode 1603. Design Parking System


Description of the problem:

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:

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

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.

Below is my code, I would appreciate any suggestions on how to improve the code.

#include <iostream>

class ParkingSystem {
    
public:
    ParkingSystem(int big, int medium, int small) :
    m_big_max(big),
    m_medium_max(medium),
    m_small_max(small),
    m_big_curr(0),
    m_medium_curr(0),
    m_small_curr(0)
    {}
    
    bool addCar(int carType) {
        if (carType == 1) {
            // big car
            if (m_big_curr == m_big_max) {
                return false;
            } else {
                m_big_curr++;
            }
        } else if (carType == 2) {
            // medium car
            if (m_medium_curr == m_medium_max) {
                return false;
            } else {
                m_medium_curr++;
            }
        } else {
            // small car
            if (m_small_curr == m_small_max) {
                return false;
            } else {
                m_small_curr++;
            }
        }
        
        return true;
    }
    
private:
    int m_big_max;
    int m_medium_max;
    int m_small_max;
    
    int m_big_curr;
    int m_medium_curr;
    int m_small_curr;
    
};

int main() {
    
    ParkingSystem parking_system(1, 1, 0);
    
    std::cout << "parking_system.addCar(1) " << parking_system.addCar(1) << std::endl;
    std::cout << "parking_system.addCar(2) " << parking_system.addCar(2) << std::endl;
    std::cout << "parking_system.addCar(3) " << parking_system.addCar(3) << std::endl;
    std::cout << "parking_system.addCar(1) " << parking_system.addCar(1) << std::endl;

    
    return 0;
}