-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathEdge.java
More file actions
169 lines (150 loc) · 6.24 KB
/
Copy pathEdge.java
File metadata and controls
169 lines (150 loc) · 6.24 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package subway.controller;
import subway.domain.Line;
import subway.domain.LineRepository;
import subway.domain.StationRepository;
import subway.domain.exception.IncludeInLineException;
import subway.domain.exception.MinimumStationNumberException;
import subway.domain.exception.NonExistentNameException;
import subway.domain.exception.NotIncludeInLineException;
import subway.domain.exception.OrderRangeException;
import subway.domain.exception.OrderTypeException;
import subway.view.InputView;
import subway.view.OutputView;
public class Edge {
private static final String STATION_MESSAGE = "역";
private static final String LINE_MESSAGE = "노선";
private static final String EDGE_MESSAGE = "구간";
private static final String NAME_MESSAGE = "이름";
private static final int ORDER_INDEX_CONSTANT = 1;
private static final int MINIMUM_DELETE_CONDITION_STAION_NUMBER = 3;
private Edge() {
}
public static void add(InputView inputView) {
String lineName = scanAddEdgeLineName(inputView);
String stationName = scanAddEdgeStationName(inputView, lineName);
int order = scanOrder(inputView, lineName);
for (int i = 0; i < LineRepository.lines().size(); i++) {
Line line = LineRepository.lines().get(i);
if (line.getName().equals(lineName)) {
line.addStationByName(stationName, order);
}
}
OutputView.printEdgeAddActionFinishMessage(EDGE_MESSAGE);
}
public static void delete(InputView inputView) {
String lineName = scanDeleteEdgeLineName(inputView);
String stationName = scanDeleteEdgeStationName(inputView, lineName);
for (int i = 0; i < LineRepository.lines().size(); i++) {
Line line = LineRepository.lines().get(i);
if (line.getName().equals(lineName)) {
line.deleteStationByName(stationName);
}
}
OutputView.printEdgeDeleteActionFinishMessage(EDGE_MESSAGE);
}
private static void validateLineState(String lineName) {
LineRepository.validateExistentLineName(lineName, LINE_MESSAGE);
validateLineRange(lineName);
}
private static void validateLineRange(String lineName) {
boolean isValidRange = false;
for (int i = 0; i < LineRepository.lines().size(); i++) {
Line line = LineRepository.lines().get(i);
if (line.getName().equals(lineName)) {
isValidRange = line.validateRange(MINIMUM_DELETE_CONDITION_STAION_NUMBER);
}
}
if (!isValidRange) {
throw new MinimumStationNumberException();
}
}
private static void validateStationState(String stationName, String lineName) {
if (StationRepository.validateUniqueName(stationName)) {
throw new NonExistentNameException(STATION_MESSAGE);
}
if (!validateLineIncludeStation(lineName, stationName)) {
throw new NotIncludeInLineException();
}
}
private static String scanAddEdgeLineName(InputView inputView) {
OutputView.printInputMessage(LINE_MESSAGE);
String lineName = inputView.getInput();
LineRepository.validateExistentLineName(lineName, LINE_MESSAGE);
return lineName;
}
private static String scanAddEdgeStationName(InputView inputView, String lineName) {
String stationNameMessage = STATION_MESSAGE + NAME_MESSAGE;
OutputView.printInputMessage(stationNameMessage);
String stationName = inputView.getInput();
validateStationName(lineName, stationName);
return stationName;
}
private static String scanDeleteEdgeLineName(InputView inputView) {
OutputView.printDeleteEdgeOptionMessage(LINE_MESSAGE);
String lineName = inputView.getInput();
validateLineState(lineName);
return lineName;
}
private static String scanDeleteEdgeStationName(InputView inputView, String lineName) {
OutputView.printDeleteEdgeOptionMessage(STATION_MESSAGE);
String stationName = inputView.getInput();
validateStationState(stationName, lineName);
return stationName;
}
private static int scanOrder(InputView inputView, String lineName) {
OutputView.printOrderInputMessage();
String order = inputView.getInput();
validateOrder(order, lineName);
return Integer.parseInt(order);
}
private static void validateStationName(String lineName, String stationName) {
if (StationRepository.validateUniqueName(stationName)) {
throw new NonExistentNameException(STATION_MESSAGE);
}
if (validateLineIncludeStation(lineName, stationName)) {
throw new IncludeInLineException();
}
}
private static boolean validateLineIncludeStation(String lineName, String stationName) {
for (int i = 0; i < LineRepository.lines().size(); i++) {
Line line = LineRepository.lines().get(i);
if (line.getName().equals(lineName)) {
return !line.validateLineIncludeStation(stationName);
}
}
return false;
}
private static void validateOrder(String order, String lineName) {
if (!validateOrderType(order)) {
throw new OrderTypeException();
}
int orderNumber = Integer.parseInt(order);
if ((!validateOrderPositiveNumber(orderNumber)) || (!validateOrderRange(orderNumber, lineName))){
throw new OrderRangeException();
}
}
private static boolean validateOrderType(String order) {
try {
Integer.parseInt(order);
return true;
} catch (RuntimeException e) {
return false;
}
}
private static boolean validateOrderRange(int order, String lineName) {
for (int i = 0; i < LineRepository.lines().size(); i++) {
Line line = LineRepository.lines().get(i);
if (line.getName().equals(lineName)) {
int orderIndex = order - ORDER_INDEX_CONSTANT;
return line.validateRange(orderIndex);
}
}
return false;
}
private static boolean validateOrderPositiveNumber(int order) {
if (order <= 0) {
return false;
}
return true;
}
}