diff --git a/src/ParkingSystem1603.java b/src/ParkingSystem1603.java deleted file mode 100644 index 9f02518..0000000 --- a/src/ParkingSystem1603.java +++ /dev/null @@ -1,30 +0,0 @@ -import java.util.HashMap; -import java.util.Map; - -public class ParkingSystem1603 { - Map slots; - - public ParkingSystem1603(int big, int medium, int small) { - Map slots = new HashMap<>(); - slots.put(1, big); - slots.put(2, medium); - slots.put(3, small); - this.slots = slots; - } - - public boolean addCar(int carType) { - int remainSlots = this.slots.get(carType); - if (remainSlots > 0) { - this.slots.put(carType, remainSlots - 1); - return true; - } else { - return false; - } - - } - - public static void main(String[] args) { - ParkingSystem1603 parkingSystem = new ParkingSystem1603(1, 1, 0); - System.out.println(parkingSystem.addCar(1)); // return true because there is 1 available slot for a big car - } -} diff --git a/src/main/java/com/leetcode/easy/DesignParkingSystem1603.java b/src/main/java/com/leetcode/easy/DesignParkingSystem1603.java new file mode 100644 index 0000000..4650995 --- /dev/null +++ b/src/main/java/com/leetcode/easy/DesignParkingSystem1603.java @@ -0,0 +1,25 @@ +// Tags: Design, Simulation, Counting +package com.leetcode.easy; + +import java.util.HashMap; +import java.util.Map; + +public class DesignParkingSystem1603 { + private final Map spaces = new HashMap<>(); + + public DesignParkingSystem1603(int big, int medium, int small) { + this.spaces.put(1, big); + this.spaces.put(2, medium); + this.spaces.put(3, small); + } + + public boolean addCar(int carType) { + int remaining = this.spaces.get(carType); + if (remaining == 0) { + return false; + } + this.spaces.put(carType, remaining - 1); + return true; + + } +} diff --git a/src/test/java/com/leetcode/easy/DesignParkingSystem1603Test.java b/src/test/java/com/leetcode/easy/DesignParkingSystem1603Test.java new file mode 100644 index 0000000..d95243c --- /dev/null +++ b/src/test/java/com/leetcode/easy/DesignParkingSystem1603Test.java @@ -0,0 +1,23 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class DesignParkingSystem1603Test { + + @Test + void testAddCar_Example1() { + DesignParkingSystem1603 parkingSystem = new DesignParkingSystem1603(1, 1, 0); + + boolean result1 = parkingSystem.addCar(1); // big car - true + boolean result2 = parkingSystem.addCar(2); // medium car - true + boolean result3 = parkingSystem.addCar(3); // small car - false (no slots) + boolean result4 = parkingSystem.addCar(1); // big car - false (already occupied) + + assertTrue(result1); + assertTrue(result2); + assertFalse(result3); + assertFalse(result4); + } +}