Skip to content

Commit

Permalink
지하철 등록 techeer-sv#6 refactor : 의존성 주입 및 패키지 구조 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
heondong9265 committed May 31, 2024
1 parent 7c2dba3 commit 3cb1a0b
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 69 deletions.
43 changes: 0 additions & 43 deletions src/main/java/subway/SubwayService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package subway;
package subway.config;

import subway.service.SubwayService;

public class SubwayInitializer {
private final SubwayService subwayService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package subway.domain;
package subway.domain.repository;

import subway.domain.Line;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class LineRepository {
private static final List<Line> lines = new ArrayList<>();
private final List<Line> lines = new ArrayList<>();

public static List<Line> lines() {
public List<Line> lines() {
return Collections.unmodifiableList(lines);
}

public static void addLine(Line line) {
public void addLine(Line line) {
lines.add(line);
}

public static boolean deleteLineByName(String name) {
public boolean deleteLineByName(String name) {
return lines.removeIf(line -> Objects.equals(line.getName(), name));
}

public static void clear() {
public void clear() {
lines.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package subway.domain;
package subway.domain.repository;

import subway.domain.Station;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -8,26 +10,26 @@
public class StationRepository {
private static final List<Station> stations = new ArrayList<>();

public static List<Station> stations() {
public List<Station> stations() {
return Collections.unmodifiableList(stations);
}

public static void addStation(Station station) {
public 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) {
public boolean deleteStation(String name) {
return stations.removeIf(station -> Objects.equals(station.getName(), name));
}

public static boolean isStationExist(String name) {
public boolean isStationExist(String name) {
return stations.stream().anyMatch(station -> station.getName().equals(name));
}

public static void clear() {
public void clear() {
stations.clear();
}
}
35 changes: 35 additions & 0 deletions src/main/java/subway/service/LineService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package subway.service;

import subway.domain.Line;
import subway.domain.repository.LineRepository;
import subway.domain.Station;
import subway.domain.repository.StationRepository;

import java.util.List;

public class LineService {

private final LineRepository lineRepository;
private final StationRepository stationRepository;

public LineService(LineRepository lineRepository, StationRepository stationRepository) {
this.lineRepository = lineRepository;
this.stationRepository = stationRepository;
}

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);
}

public List<Line> getLines() {
return lineRepository.lines();
}
}
30 changes: 30 additions & 0 deletions src/main/java/subway/service/StationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package subway.service;

import subway.domain.Station;
import subway.domain.repository.StationRepository;

import java.util.List;

public class StationService {

private final StationRepository stationRepository;

public StationService(StationRepository stationRepository) {
this.stationRepository = stationRepository;
}

public void addStation(String name) {
Station station = new Station(name);
stationRepository.addStation(station);
}

public void deleteStation(String name) {
if (!stationRepository.deleteStation(name)) {
throw new IllegalArgumentException("[ERROR] 존재하지 않는 역입니다.");
}
}

public List<Station> getStations() {
return stationRepository.stations();
}
}
37 changes: 37 additions & 0 deletions src/main/java/subway/service/SubwayService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package subway.service;

import subway.domain.Station;

import java.util.List;

public class SubwayService {

private final StationService stationService;
private final LineService lineService;

public SubwayService(StationService stationService, LineService lineService) {
this.stationService = stationService;
this.lineService = lineService;
}

public void addStation(String name) {
stationService.addStation(name);
}

public void addLine(String name, String[] stationNames) {
lineService.addLine(name, stationNames);
}

public void deleteStation(String name) {
if (lineService.getLines().stream()
.flatMap(line -> line.getStations().stream())
.anyMatch(station -> station.getName().equals(name))) {
throw new IllegalArgumentException("[ERROR] 노선에 등록된 역은 삭제할 수 없습니다.");
}
stationService.deleteStation(name);
}

public List<Station> getStations() {
return stationService.getStations();
}
}
20 changes: 15 additions & 5 deletions src/test/java/subway/SubwayInitializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import subway.config.SubwayInitializer;
import subway.domain.Line;
import subway.domain.LineRepository;
import subway.domain.repository.LineRepository;
import subway.domain.Station;
import subway.domain.StationRepository;
import subway.domain.repository.StationRepository;
import subway.service.LineService;
import subway.service.StationService;
import subway.service.SubwayService;

import java.util.List;

Expand All @@ -20,10 +24,16 @@ class SubwayInitializerTest {
class SubwayService_Init {
SubwayInitializer subwayInitializer;
SubwayService subwayService;
StationRepository stationRepository;
LineRepository lineRepository;

@BeforeEach
void setUp() {
subwayService = new SubwayService();
stationRepository = new StationRepository();
lineRepository = new LineRepository();
StationService stationService = new StationService(stationRepository);
LineService lineService = new LineService(lineRepository, stationRepository);
subwayService = new SubwayService(stationService, lineService);
subwayInitializer = new SubwayInitializer(subwayService);
}

Expand All @@ -33,10 +43,10 @@ void initSubwayLine() {
subwayInitializer.initialize();

// 지하철 역 초기화
assertStationsInitialized(StationRepository.stations());
assertStationsInitialized(stationRepository.stations());

// 지하철 노선 초기화
assertLinesInitialized(LineRepository.lines());
assertLinesInitialized(lineRepository.lines());
}
private void assertStationsInitialized(List<Station> stations) {
assertEquals(7, stations.size());
Expand Down
25 changes: 17 additions & 8 deletions src/test/java/subway/SubwayServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@
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 subway.domain.repository.LineRepository;
import subway.domain.repository.StationRepository;
import subway.service.LineService;
import subway.service.StationService;
import subway.service.SubwayService;

import static org.junit.jupiter.api.Assertions.*;

public class SubwayServiceTest {
SubwayService subwayService;
StationRepository stationRepository;
LineRepository lineRepository;

@BeforeEach
void setUp() {
subwayService = new SubwayService();
StationRepository.clear();
LineRepository.clear();
stationRepository = new StationRepository();
lineRepository = new LineRepository();
StationService stationService = new StationService(stationRepository);
LineService lineService = new LineService(lineRepository, stationRepository);
subwayService = new SubwayService(stationService, lineService);
stationRepository.clear();
lineRepository.clear();
}

@Nested
Expand All @@ -27,16 +36,16 @@ class AddStationTest {
@DisplayName("지하철 역을 등록할 수 있다.")
void addStation() {
subwayService.addStation("잠실역");
assertTrue(StationRepository.isStationExist("잠실역"));
assertTrue(stationRepository.isStationExist("잠실역"));
}

@Test
@DisplayName("역을 삭제할 수 있다.")
void deleteStation() {
subwayService.addStation("잠실역");
assertTrue(StationRepository.isStationExist("잠실역"));
assertTrue(stationRepository.isStationExist("잠실역"));
subwayService.deleteStation("잠실역");
assertFalse(StationRepository.isStationExist("잠실역"));
assertFalse(stationRepository.isStationExist("잠실역"));
}

@Test
Expand Down

0 comments on commit 3cb1a0b

Please sign in to comment.