Object Oriented Design Solution | Interview Friendly | Clean Code
You can just write simple lines to get accepted in this question, but my solution is interview friendly based on the OOP concepts and Deisgn Patterns to make it
- You can just write simple lines to get `accepted` in this question, but my solution is interview friendly based on the **OOP concepts** and **Deisgn Patterns** to make it scalable and clean. \n- My approach:- \n\t- Create interface **ISlot**, and create 3 classes **BigSlot, MediumSlot, SmallSlot** inherit from ISlot\n\t- Have hash sets to store the available slots and busy slots\n\t- Use **factory design pattern** to create the objects and push them to the available slots hash sets\n\t- In **addCar(int carType)** function, check if the size of the available slots > 0, So remove a slot from the available and then add it to the the busy slots and return true.  \n\n`Any comments are welcomed :)`\n\n```cpp\nclass ISlot {\n    virtual int getId() = 0;  \n};\n    \nclass BigSlot : public ISlot {\nprivate:\n    int slotId;\npublic: \n    BigSlot(int id) {\n        slotId = id;\n    }\n    \n    int getId() {\n        return slotId;\n    }\n};\n\nclass MediumSlot : public ISlot {\nprivate:\n    int slotId;\npublic: \n    MediumSlot(int id) {\n        slotId = id;\n    }\n    \n    int getId() {\n        return slotId;\n    }\n};\n\nclass SmallSlot : public ISlot {\nprivate:\n    int slotId;\npublic: \n    SmallSlot(int id) {\n        slotId = id;\n    }\n    \n    int getId() {\n        return slotId;\n    }\n};\n\nclass Factory {\nprivate: \n    ISlot *iSlot;\npublic: \n    Factory() {\n        \n    }\n    \n    ISlot* work(int carType, int id) {\n        if(carType == 1) {\n            iSlot = new BigSlot(id);\n        } else if(carType == 2) {\n            iSlot = new MediumSlot(id);\n        } else if(carType == 3) {\n            iSlot = new SmallSlot(id);\n        }\n        return iSlot;\n    }  \n};\n\nclass ParkingSystem {\nprivate: \n    unordered_set<ISlot*> bigSlots, mediumSlots, smallSlots;\n    unordered_set<ISlot*> busyBigSlots, busyMediumSlots, busySmallSlots;\n    \npublic:\n    \n    ParkingSystem(int big, int medium, int small) {\n        initializeSpots(big, medium, small);\n    }\n    \n    void initializeSpots(int big, int medium, int small) {\n        int id = 0;\n        Factory *factoryObj = new Factory();\n        for(int i = 0; i < big; i++) {\n            ISlot *curSlot = factoryObj->work(1, id++);\n            bigSlots.insert(curSlot);\n        }\n        for(int i = 0; i < medium; i++) {\n            ISlot *curSlot = factoryObj->work(1, id++);\n            mediumSlots.insert(curSlot);\n        }\n        for(int i = 0; i < small; i++) {\n            ISlot *curSlot = factoryObj->work(1, id++);\n            smallSlots.insert(curSlot);\n        }\n    }\n    \n    bool addCar(int carType) {\n        if(carType == 1) {\n            if(bigSlots.size() > 0) {\n                ISlot *curSlot = *bigSlots.begin();\n                bigSlots.erase(curSlot);\n                busyBigSlots.insert(curSlot);\n                return true;\n            }\n        } else if(carType == 2) {\n            if(mediumSlots.size() > 0) {\n                ISlot *curSlot = *mediumSlots.begin();\n                mediumSlots.erase(curSlot);\n                busyMediumSlots.insert(curSlot);\n                return true;\n            }\n        } else if(carType == 3) {\n            if(smallSlots.size() > 0) {\n                ISlot *curSlot = *smallSlots.begin();\n                smallSlots.erase(curSlot);\n                busySmallSlots.insert(curSlot);\n                return true;\n            }\n        }\n        return false;\n    }\n};\n```
['C', 'C++']