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.
지하철 등록 techeer-sv#6 refactor : 의존성 주입 및 패키지 구조 수정
- Loading branch information
1 parent
7c2dba3
commit 3cb1a0b
Showing
9 changed files
with
153 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
4 changes: 3 additions & 1 deletion
4
src/main/java/subway/SubwayInitializer.java → ...java/subway/config/SubwayInitializer.java
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
14 changes: 8 additions & 6 deletions
14
...in/java/subway/domain/LineRepository.java → ...way/domain/repository/LineRepository.java
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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