-
Notifications
You must be signed in to change notification settings - Fork 282
[지하철 경로조회] 코드리뷰 #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[지하철 경로조회] 코드리뷰 #117
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| ### 기능 요구사항 | ||
|
|
||
| [ ✅ ] 주어진 지하철 정보를 초기화한다. | ||
|
|
||
| [ ✅ ] 아래와 같이 입력을 받는다. | ||
| ``` | ||
| ## 메인 화면 | ||
| 1. 경로 조회 | ||
| Q. 종료 | ||
|
|
||
| ## 원하는 기능을 선택하세요. | ||
| 1 | ||
|
|
||
| ## 경로 기준 | ||
| 1. 최단 거리 | ||
| 2. 최소 시간 | ||
| B. 돌아가기 | ||
|
|
||
| ## 원하는 기능을 선택하세요. | ||
| 1 | ||
|
|
||
| ## 출발역을 입력하세요. | ||
| 교대역 | ||
|
|
||
| ## 도착역을 입력하세요. | ||
| 양재역 | ||
| ``` | ||
| - 입력 받을 때, 경로 조회 시 출발역과 도착역이 같으면 에러를 출력한다. | ||
| - 정상적으로 입력하지 않았다면, 에러를 출력한다. | ||
|
|
||
| [X] 경로 조회 시 출발역과 도착역이 연결되어 있지 않으면 에러를 출력한다. | ||
| - 모든 역이 서로 연결되어있기때문에 위 내용 구현X | ||
|
|
||
| [ ✅ ] 총 거리를 구한다. | ||
|
|
||
| [ ✅ ] 총 소요시간을 구한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,30 @@ | ||
| package subway; | ||
|
|
||
| import subway.controller.GraphController; | ||
| import subway.controller.LineController; | ||
| import subway.controller.StationController; | ||
| import subway.controller.SubwayController; | ||
| import subway.view.InputView; | ||
| import subway.view.OutputView; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| import static subway.repository.LineRepository.lines; | ||
| import static subway.repository.StationRepository.stations; | ||
|
|
||
| public class Application { | ||
|
|
||
| private static final LineController lineController = new LineController(); | ||
| private static final StationController stationController = new StationController(); | ||
| private static final SubwayController subwayController = new SubwayController(); | ||
| private static final GraphController graphController = new GraphController(); | ||
| public static void main(String[] args) { | ||
| lineController.initLine(); | ||
| stationController.initStation(); | ||
| subwayController.initSubway(); | ||
| graphController.initGraph(); | ||
| final Scanner scanner = new Scanner(System.in); | ||
| // TODO: 프로그램 구현 | ||
| InputView.init(scanner); | ||
| subwayController.start(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package subway.controller; | ||
|
|
||
| import subway.service.GraphService; | ||
|
|
||
| public class GraphController { | ||
| private final GraphService graphService; | ||
|
|
||
| public GraphController(){ | ||
| graphService = new GraphService(); | ||
| } | ||
|
|
||
| public void initGraph(){ | ||
| graphService.initGraph(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package subway.controller; | ||
|
|
||
| import subway.service.LineService; | ||
|
|
||
| public class LineController { | ||
|
|
||
| private final LineService lineService; | ||
| public LineController(){ | ||
| lineService = new LineService(); | ||
| } | ||
|
|
||
| public void initLine(){ | ||
| lineService.initLine(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package subway.controller; | ||
|
|
||
| import subway.service.StationService; | ||
|
|
||
| public class StationController { | ||
| private final StationService stationService; | ||
|
|
||
| public StationController(){ | ||
| stationService = new StationService(); | ||
| } | ||
|
|
||
| public void initStation(){ | ||
| stationService.initStation(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 계층 구조를 잘 지키셨군요! 👍 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package subway.controller; | ||
|
|
||
| import subway.service.GraphService; | ||
| import subway.service.SubwayService; | ||
| import subway.util.message.InputMessage; | ||
| import subway.util.message.Menu; | ||
| import subway.view.InputView; | ||
| import subway.view.OutputView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class SubwayController { | ||
| private final SubwayService subwayService; | ||
| private final GraphService graphService; | ||
| private final List<String> main = new ArrayList<>(List.of(Menu.SELECT_ROUTINE.getKey(), Menu.EXIT.getKey())); | ||
| private final List<String> routine = new ArrayList<>(List.of(Menu.SHORTEST_DISTANCE.getKey(), Menu.SHORTEST_TIME.getKey(), Menu.BACK.getKey())); | ||
|
|
||
| private int totalDistance; | ||
| private int totalTime; | ||
| private List<String> path; | ||
| public SubwayController(){ | ||
| subwayService = new SubwayService(); | ||
| graphService = new GraphService(); | ||
| } | ||
|
|
||
| public void initSubway(){ | ||
| subwayService.initSubway(); | ||
| } | ||
|
|
||
| public void start(){ | ||
| while(questionIsproceed()){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 낙타표기법에 의해 |
||
| try{ | ||
| questionRoutine(); | ||
| } catch(IllegalArgumentException e){ | ||
| OutputView.printMessage(e.getMessage()); | ||
| questionRoutine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean questionIsproceed(){ | ||
| OutputView.printMain(); | ||
| String input = InputView.inputKey(main); | ||
| return confirmProceed(input); | ||
| } | ||
|
|
||
| private boolean confirmProceed(final String input){ | ||
| if(input.equals(Menu.EXIT.getKey())){ | ||
| return false; | ||
| } | ||
| return input.equals(Menu.SELECT_ROUTINE.getKey()); | ||
| } | ||
|
|
||
| private void questionRoutine(){ | ||
| OutputView.printRoutine(); | ||
| String input = InputView.inputKey(routine); | ||
| questionMainNext(input); | ||
| } | ||
|
|
||
| private void questionMainNext(final String input){ | ||
| if(input.equals(Menu.BACK.getKey())){ | ||
| questionRoutine(); | ||
| } | ||
| List<Integer> totalInfo = new ArrayList<>(); | ||
| if(input.equals(Menu.SHORTEST_DISTANCE.getKey())){ | ||
| totalInfo = getShortestDistance(); | ||
| } | ||
| if(input.equals(Menu.SHORTEST_TIME.getKey())){ | ||
| totalInfo = getShortestTime(); | ||
| } | ||
| savetotalInfo(totalInfo); | ||
| OutputView.printResult(path, totalDistance, totalTime); | ||
| } | ||
|
|
||
| private List<Integer> getShortestDistance(){ | ||
| String startStationInfo = InputView.inputStation(InputMessage.GET_START_STATION.getValue()); | ||
| String endStationInfo = InputView.inputStation(InputMessage.GET_END_STARTION.getValue()); | ||
| path = graphService.getShortestDistancePath(startStationInfo, endStationInfo); | ||
| return graphService.getTotalShortestDistanceAndShortestTime(path); | ||
| } | ||
|
|
||
| private List<Integer> getShortestTime(){ | ||
| String startStationInfo = InputView.inputStation(InputMessage.GET_START_STATION.getValue()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| String endStationInfo = InputView.inputStation(InputMessage.GET_END_STARTION.getValue()); | ||
| path = graphService.getShortestTimePath(startStationInfo, endStationInfo); | ||
| return graphService.getTotalShortestDistanceAndShortestTime(path); | ||
| } | ||
|
|
||
| private void savetotalInfo(final List<Integer> totalInfo){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 낙타표기법으로 |
||
| totalDistance = totalInfo.get(0); | ||
| totalTime = totalInfo.get(1); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package subway.domain; | ||
|
|
||
| public class ConnectStationNode { | ||
| private Station station; | ||
| private int distance; | ||
| private int time; | ||
|
|
||
| private ConnectStationNode(Station station, int distance, int time){ | ||
| this.station = station; | ||
| this.distance = distance; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public static ConnectStationNode create(Station station, int distance, int time){ | ||
| return new ConnectStationNode(station, distance, time); | ||
| } | ||
|
|
||
| public Station getStation(){ | ||
| return station; | ||
| } | ||
|
|
||
| public int getDistance(){ | ||
| return distance; | ||
| } | ||
|
|
||
| public int getTime(){ | ||
| return time; | ||
| } | ||
|
|
||
| public String toString(){ | ||
| return "연결된 역 정보 = " + station.getName() + "," + getDistance() + "," + getTime(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package subway.domain; | ||
|
|
||
| import org.jgrapht.graph.DefaultWeightedEdge; | ||
| import org.jgrapht.graph.WeightedMultigraph; | ||
| import subway.util.constants.StationName; | ||
|
|
||
| public class Graph { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,33 @@ | ||
| package subway.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Line { | ||
| private String name; | ||
| private List<Station> stations = new ArrayList<>(); | ||
|
|
||
| public Line(String name) { | ||
| private Line(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public static Line create(String name){ | ||
| return new Line(name); | ||
| } | ||
|
|
||
| public void addStationsInLine(final Station station){ | ||
| stations.add(station); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| public boolean isEqualName(String name){ | ||
| return name.equals(this.name); | ||
| } | ||
|
|
||
| public String toString(){ | ||
| return name; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,36 @@ | ||
| package subway.domain; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Station { | ||
| private String name; | ||
| private List<ConnectStationNode> connectStationNodes = new ArrayList<>(); | ||
|
|
||
| public Station(String name) { | ||
| private Station(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public static Station create(String name){ | ||
| return new Station(name); | ||
| } | ||
|
|
||
| public void addConnectStations(final ConnectStationNode connectStationNode){ | ||
| connectStationNodes.add(connectStationNode); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| public boolean isEqualName(String name){ | ||
| return name.equals(this.name); | ||
| } | ||
|
|
||
| public List<ConnectStationNode> getConnectStationNodes() { | ||
| return Collections.unmodifiableList(connectStationNodes); | ||
| } | ||
|
|
||
| public String toString(){ | ||
| return name; | ||
| } | ||
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 application에서는 controller만 받게끔 만들어 봐야 겠어요!!