Skip to content

Commit

Permalink
지하철 등록 techeer-sv#1 feat : 새로운 역 등록 기능 구현 및 예외 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
heondong9265 committed May 31, 2024
1 parent a420b17 commit 8307c3a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/subway/SubwayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/subway/domain/Station.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/subway/domain/StationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public static List<Station> 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));
}
}
40 changes: 40 additions & 0 deletions src/test/java/subway/SubwayServiceTest.java
Original file line number Diff line number Diff line change
@@ -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("짱"));
}
}
}

0 comments on commit 8307c3a

Please sign in to comment.