-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathSearchService.java
More file actions
79 lines (64 loc) · 3.18 KB
/
Copy pathSearchService.java
File metadata and controls
79 lines (64 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package subway;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import subway.domain.station.Station;
import subway.domain.station.StationRepository;
import subway.exception.ErrorCode;
import subway.exception.StationException;
import subway.view.InputView;
import subway.view.screen.SearchView;
import java.util.ArrayList;
import java.util.List;
public class SearchService {
private static final List<DijkstraShortestPath> dijkstraShortestPaths = new ArrayList<>();
private static final int DISTANCE_INDEX = 0;
private static final int TIME_INDEX = 1;
private final InputView inputView;
private final SearchView searchView;
public SearchService(InputView inputView, SearchView searchView) {
this.inputView = inputView;
this.searchView = searchView;
}
public static void addDijkstraShortestPath(DijkstraShortestPath dijkstraShortestPath) {
dijkstraShortestPaths.add(dijkstraShortestPath);
}
public void findShortestDistance() {
searchView.printAdd();
String firstStationName = inputView.inputNextLine();
Station firstStation = StationRepository.findByName(firstStationName);
searchView.printSecondAdd();
String secondStationName = inputView.inputNextLine();
Station secondStation = StationRepository.findByName(secondStationName);
checkSameName(firstStationName, secondStationName);
searchView.printAfterAdd();
printDistance(firstStation, secondStation);
}
public void findShortestTime() {
searchView.printAdd();
String firstStationName = inputView.inputNextLine();
Station firstStation = StationRepository.findByName(firstStationName);
searchView.printSecondAdd();
String secondStationName = inputView.inputNextLine();
Station secondStation = StationRepository.findByName(secondStationName);
checkSameName(firstStationName, secondStationName);
searchView.printAfterAdd();
printTime(firstStation, secondStation);
}
private void printTime(Station firstStation, Station secondStation) {
DijkstraShortestPath dijkstraShortestPathDistance = dijkstraShortestPaths.get(TIME_INDEX);
double pathWeight = dijkstraShortestPathDistance.getPathWeight(firstStation.getName(), secondStation.getName());
GraphPath path = dijkstraShortestPathDistance.getPath(firstStation.getName(), secondStation.getName());
searchView.printTimeList(path, pathWeight);
}
private void printDistance(Station firstStation, Station secondStation) {
DijkstraShortestPath dijkstraShortestPathDistance = dijkstraShortestPaths.get(DISTANCE_INDEX);
double pathWeight = dijkstraShortestPathDistance.getPathWeight(firstStation.getName(), secondStation.getName());
GraphPath path = dijkstraShortestPathDistance.getPath(firstStation.getName(), secondStation.getName());
searchView.printDistanceList(path, pathWeight);
}
private void checkSameName(String firstStationName, String secondStationName) {
if (firstStationName.equals(secondStationName)) {
throw new StationException(ErrorCode.STATION_SAME_NAME);
}
}
}