forked from techeer-sv/Infinite_Challenge_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd4ecdd
commit 89e8bf0
Showing
5 changed files
with
136 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package subway; | ||
|
||
public class SubwayInitializer { | ||
private final SubwayService subwayService; | ||
|
||
public SubwayInitializer(SubwayService subwayService) { | ||
this.subwayService = subwayService; | ||
} | ||
|
||
public void initialize() { | ||
initializeStations(); | ||
initializeLines(); | ||
} | ||
|
||
private void initializeStations() { | ||
subwayService.addStation("교대역"); | ||
subwayService.addStation("강남역"); | ||
subwayService.addStation("역삼역"); | ||
subwayService.addStation("남부터미널역"); | ||
subwayService.addStation("양재역"); | ||
subwayService.addStation("양재시민의숲역"); | ||
subwayService.addStation("매봉역"); | ||
} | ||
|
||
private void initializeLines() { | ||
subwayService.addLine("2호선", new String[]{"교대역", "강남역", "역삼역"}); | ||
subwayService.addLine("3호선", new String[]{"교대역", "남부터미널역", "양재역", "매봉역"}); | ||
subwayService.addLine("신분당선", new String[]{"강남역", "양재역", "양재시민의숲역"}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
package subway; | ||
|
||
import subway.domain.Line; | ||
import subway.domain.LineRepository; | ||
import subway.domain.Station; | ||
import subway.domain.StationRepository; | ||
|
||
public class SubwayService { | ||
|
||
public void addStation(String name) { | ||
StationRepository.addStation(new Station(name)); | ||
} | ||
|
||
public void addLine(String name, String[] stationNames) { | ||
Line line = new Line(name); | ||
for (String stationName : stationNames) { | ||
Station station = StationRepository.stations().stream() | ||
.filter(s -> s.getName().equals(stationName)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 존재하지 않는 역입니다.")); | ||
line.addStation(station); | ||
} | ||
LineRepository.addLine(line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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.Line; | ||
import subway.domain.LineRepository; | ||
import subway.domain.Station; | ||
import subway.domain.StationRepository; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class SubwayInitializerTest { | ||
|
||
@Nested | ||
@DisplayName("SubwayInitializer") | ||
class SubwayService_Init { | ||
SubwayInitializer subwayInitializer; | ||
SubwayService subwayService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
subwayInitializer = new SubwayInitializer(subwayService); | ||
} | ||
|
||
@Test | ||
@DisplayName("지하철 초기화") | ||
void initSubwayLine() { | ||
subwayInitializer.initialize(); | ||
|
||
// 지하철 역 초기화 | ||
assertStationsInitialized(StationRepository.stations()); | ||
|
||
// 지하철 노선 초기화 | ||
assertLinesInitialized(LineRepository.lines()); | ||
} | ||
private void assertStationsInitialized(List<Station> stations) { | ||
assertEquals(7, stations.size()); | ||
assertAll("stations", | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("교대역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("강남역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("역삼역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("남부터미널역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("양재역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("양재시민의숲역"))), | ||
() -> assertTrue(stations.stream().anyMatch(station -> station.getName().equals("매봉역"))) | ||
); | ||
} | ||
|
||
private void assertLinesInitialized(List<Line> lines) { | ||
assertEquals(3, lines.size()); | ||
|
||
assertLine(lines, "2호선", new String[]{"교대역", "강남역", "역삼역"}); | ||
assertLine(lines, "3호선", new String[]{"교대역", "남부터미널역", "양재역", "매봉역"}); | ||
assertLine(lines, "신분당선", new String[]{"강남역", "양재역", "양재시민의숲역"}); | ||
} | ||
|
||
private void assertLine(List<Line> lines, String lineName, String[] stationNames) { | ||
Line line = lines.stream().filter(l -> l.getName().equals(lineName)).findFirst().orElse(null); | ||
|
||
assertNotNull(line, lineName); | ||
assertEquals(stationNames.length, line.getStations().size(), lineName); | ||
|
||
for (int i = 0; i < stationNames.length; i++) { | ||
assertEquals(stationNames[i], line.getStations().get(i).getName(), lineName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,5 @@ | ||
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 java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class SubwayServiceTest { | ||
|
||
@Nested | ||
@DisplayName("SubwayService Initialization") | ||
class SubwayService_Init { | ||
SubwayService subwayService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
subwayService = new SubwayService(); | ||
} | ||
|
||
@Test | ||
@DisplayName("지하철 노선도 초기화") | ||
void initSubwayLine() { | ||
subwayService.initSubwayLine(); | ||
|
||
assertAll( | ||
() -> assertEquals(Arrays.asList("교대역", "강남역", "역삼역"), subwayService.getStations("2호선")), | ||
() -> assertEquals(Arrays.asList("교대역", "남부터미널역", "양재역", "매봉역"), subwayService.getStations("3호선")), | ||
() -> assertEquals(Arrays.asList("강남역", "양재역", "양재시민의숲역"), subwayService.getStations("신분당선")) | ||
); | ||
} | ||
} | ||
} |