From b60df0dd926723c3eaad3438520c2487c97c4c1f Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Tue, 17 Mar 2026 08:03:45 +0800 Subject: [PATCH] Refactor DesignParkingSystem1603: move to proper package structure and add tests - Delete old ParkingSystem1603.java from src/ directory - Move implementation to src/main/java/com/leetcode/easy/DesignParkingSystem1603.java - Use HashMap to track available parking spaces for each car type - Car types: 1 (big), 2 (medium), 3 (small) - addCar() returns true if space available, false otherwise - Add JUnit test for LeetCode example 1 with assertTrue/assertFalse --- src/ParkingSystem1603.java | 30 ------------------- .../easy/DesignParkingSystem1603.java | 25 ++++++++++++++++ .../easy/DesignParkingSystem1603Test.java | 23 ++++++++++++++ 3 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 src/ParkingSystem1603.java create mode 100644 src/main/java/com/leetcode/easy/DesignParkingSystem1603.java create mode 100644 src/test/java/com/leetcode/easy/DesignParkingSystem1603Test.java 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); + } +}