From 7c2dba39d49af89970f3230d35a6c9c12d9514af Mon Sep 17 00:00:00 2001 From: heondong9265 Date: Fri, 31 May 2024 20:41:13 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A7=80=ED=95=98=EC=B2=A0=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20#05=20feat=20:=20=EC=A7=80=ED=95=98=EC=B2=A0=20?= =?UTF-8?q?=EC=97=AD=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/SubwayService.java | 11 +++++++++++ src/test/java/subway/SubwayServiceTest.java | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/subway/SubwayService.java b/src/main/java/subway/SubwayService.java index 60761a175..6e1c2ad56 100644 --- a/src/main/java/subway/SubwayService.java +++ b/src/main/java/subway/SubwayService.java @@ -26,6 +26,17 @@ public void addLine(String name, String[] stationNames) { LineRepository.addLine(line); } + public void deleteStation(String name) { + if (LineRepository.lines().stream() + .flatMap(line -> line.getStations().stream()) + .anyMatch(station -> station.getName().equals(name))) { + throw new IllegalArgumentException("[ERROR] 노선에 등록된 역은 삭제할 수 없습니다."); + } + if (!StationRepository.deleteStation(name)) { + throw new IllegalArgumentException("[ERROR] 존재하지 않는 역입니다."); + } + } + public List getStations() { return StationRepository.stations(); } diff --git a/src/test/java/subway/SubwayServiceTest.java b/src/test/java/subway/SubwayServiceTest.java index 09dfa4a2c..d3c98929c 100644 --- a/src/test/java/subway/SubwayServiceTest.java +++ b/src/test/java/subway/SubwayServiceTest.java @@ -24,12 +24,21 @@ void setUp() { class AddStationTest { @Test - @DisplayName("지하철 역을 등록하고 삭제할 수 있다.") + @DisplayName("지하철 역을 등록할 수 있다.") void addStation() { subwayService.addStation("잠실역"); assertTrue(StationRepository.isStationExist("잠실역")); } + @Test + @DisplayName("역을 삭제할 수 있다.") + void deleteStation() { + subwayService.addStation("잠실역"); + assertTrue(StationRepository.isStationExist("잠실역")); + subwayService.deleteStation("잠실역"); + assertFalse(StationRepository.isStationExist("잠실역")); + } + @Test @DisplayName("중복된 지하철 역 이름이 등록될 수 없다.") void duplicationStation() {