diff --git a/src/main/java/subway/SubwayService.java b/src/main/java/subway/SubwayService.java index d46168c2e..e329cc54c 100644 --- a/src/main/java/subway/SubwayService.java +++ b/src/main/java/subway/SubwayService.java @@ -8,7 +8,8 @@ public class SubwayService { public void addStation(String name) { - StationRepository.addStation(new Station(name)); + Station station = new Station(name); + StationRepository.addStation(station); } public void addLine(String name, String[] stationNames) { diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index bdb142590..f7cd2e94f 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -1,9 +1,12 @@ package subway.domain; public class Station { - private String name; + private final String name; public Station(String name) { + if(name == null || name.length() < 2) { + throw new IllegalArgumentException("[ERROR] 역 이름은 2글자 이상이어야 합니다."); + } this.name = name; } diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b7245c0f3..4b364200c 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -13,10 +13,17 @@ public static List stations() { } public static void addStation(Station station) { + if(stations.stream().anyMatch(s -> s.getName().equals(station.getName()))){ + throw new IllegalArgumentException("[ERROR] 중복된 역 이름입니다."); + } stations.add(station); } public static boolean deleteStation(String name) { return stations.removeIf(station -> Objects.equals(station.getName(), name)); } + + public static boolean isStationExist(String name) { + return stations.stream().anyMatch(station -> station.getName().equals(name)); + } } diff --git a/src/test/java/subway/SubwayServiceTest.java b/src/test/java/subway/SubwayServiceTest.java index 70ed1c37e..9fad47685 100644 --- a/src/test/java/subway/SubwayServiceTest.java +++ b/src/test/java/subway/SubwayServiceTest.java @@ -1,5 +1,45 @@ package subway; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import subway.domain.LineRepository; +import subway.domain.StationRepository; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class SubwayServiceTest { + SubwayService subwayService; + + @BeforeEach + void setUp() { + subwayService = new SubwayService(); + } + + @Nested + @DisplayName("역 등록 테스트") + class AddStationTest { + + @Test + @DisplayName("새로운 역을 등록") + void addStation() { + subwayService.addStation("잠실역"); + assertTrue(StationRepository.isStationExist("잠실역")); + } + + @Test + @DisplayName("중복된 역 검증") + void duplicationStation() { + subwayService.addStation("잠실역"); + assertThrows(IllegalArgumentException.class, () -> subwayService.addStation("잠실역")); + } + @Test + @DisplayName("역 이름은 2글자 이상이어야 한다.") + void invalidStationName() { + assertThrows(IllegalArgumentException.class, () -> subwayService.addStation("짱")); + } + } }