-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathSectionRepository.java
More file actions
44 lines (37 loc) · 1.58 KB
/
Copy pathSectionRepository.java
File metadata and controls
44 lines (37 loc) · 1.58 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
package subway.repository;
import subway.domain.Line;
import java.util.*;
public class SectionRepository {
private static LineRepository lineRepository = new LineRepository();
private static StationRepository stationRepository = new StationRepository();
public static void addSection(String lineName, String stationName, int order) {
if (!validate(lineName, stationName)) {
throw new IllegalArgumentException();
}
try {
Line targetLine = lineRepository.lines.stream().filter(l -> lineName.equals(l.getName())).findFirst().get();
if (targetLine.existStation(stationName)) {
throw new IllegalStateException();
}
targetLine.addStation(stationName, order);
} catch (IndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException();
}
}
public static boolean deleteSection(String lineName, String stationName) {
if (!validate(lineName, stationName)) {
throw new IllegalArgumentException();
}
Line line = lineRepository.lines.stream().filter(l -> lineName.equals(l.getName())).findFirst().get();
if (line.checkStationSize()) {
throw new IllegalStateException();
}
return line.stations.removeIf(s -> Objects.equals(s.getName(), stationName));
}
static boolean validate(String lineName, String stationName) {
if (!lineRepository.checkExistLine(lineName) || !stationRepository.checkExistStation(stationName)) {
return false;
}
return true;
}
}