-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathSearchPathService.java
More file actions
73 lines (64 loc) · 2.81 KB
/
Copy pathSearchPathService.java
File metadata and controls
73 lines (64 loc) · 2.81 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
package subway.service;
import org.jgrapht.GraphPath;
import subway.domain.LineStationRepository;
import subway.view.OutputView;
import java.util.Scanner;
import static subway.domain.MenuType.*;
import static subway.domain.StationRepository.findStationByName;
import static subway.view.OutputView.printResult;
public class SearchPathService extends InputService {
public boolean selectSearchPathMenu(Scanner scanner, String menu) {
if (SEARCH_SHORTEST_PATH.isKeyEquals(menu)) {
return searchShortestPath(scanner);
}
if (SEARCH_SHORTEST_TIME.isKeyEquals(menu)) {
return searchShortestTime(scanner);
}
if (BACK.isKeyEquals(menu)) {
return true;
}
return false;
}
private boolean searchShortestPath(Scanner scanner) {
try {
String startStationName = inputStartStationName(scanner);
String endStationName = inputEndStationName(scanner);
calculationShortestPath(startStationName, endStationName);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
private boolean searchShortestTime(Scanner scanner) {
try {
String startStationName = inputStartStationName(scanner);
String endStationName = inputEndStationName(scanner);
calculationShortestTime(startStationName, endStationName);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
private void calculationShortestPath(String startStationName, String endStationName) {
if (startStationName.equals(endStationName)) {
throw new IllegalArgumentException("[ERROR] 출발역과 도착역이 동일합니다.");
}
findStationByName(startStationName);
findStationByName(endStationName);
GraphPath shortestPath = LineStationRepository.findShortestPath(startStationName, endStationName);
GraphPath shortestTime = LineStationRepository.findShortestTime(startStationName, endStationName);
printResult(shortestPath, shortestTime);
}
private void calculationShortestTime(String startStationName, String endStationName) {
if (startStationName.equals(endStationName)) {
throw new IllegalArgumentException("[ERROR] 출발역과 도착역이 동일합니다.");
}
findStationByName(startStationName);
findStationByName(endStationName);
GraphPath shortestPath = LineStationRepository.findShortestPath(startStationName, endStationName);
GraphPath shortestTime = LineStationRepository.findShortestTime(startStationName, endStationName);
printResult(shortestPath, shortestTime);
}
}