From f4bda0ca05bee1ac478b62065b8298faf653e814 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 30 May 2023 00:02:00 +0200 Subject: [PATCH] =?UTF-8?q?problems(cpp):=20add=20=E2=80=9C1603.=20Design?= =?UTF-8?q?=20Parking=20System=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/design-parking-system.cpp | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 problems/design-parking-system.cpp diff --git a/problems/design-parking-system.cpp b/problems/design-parking-system.cpp new file mode 100644 index 0000000..0f12713 --- /dev/null +++ b/problems/design-parking-system.cpp @@ -0,0 +1,46 @@ +#include + +class ParkingSystem { + int big; + int medium; + int small; + + int& get(int carType) + { + switch (carType) { + case 1: + return big; + case 2: + return medium; + case 3: + return small; + default: + assert(false); + } + } + +public: + ParkingSystem(int big, int medium, int small) + : big(big) + , medium(medium) + , small(small) + { + } + + bool addCar(int carType) + { + auto& space = get(carType); + if (space <= 0) { + return false; + } + + --space; + return true; + } +}; + +/** + * Your ParkingSystem object will be instantiated and called as such: + * ParkingSystem* obj = new ParkingSystem(big, medium, small); + * bool param_1 = obj->addCar(carType); + */