-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathLineRepository.java
More file actions
46 lines (37 loc) · 1.54 KB
/
Copy pathLineRepository.java
File metadata and controls
46 lines (37 loc) · 1.54 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
package subway.repository;
import subway.domain.Line;
import subway.domain.Station;
import subway.repository.StationRepository;
import java.util.*;
public class LineRepository {
public static final List<Line> lines = new ArrayList<>();
private static StationRepository stationRepository = new StationRepository();
public static List<Line> lines() {
return Collections.unmodifiableList(lines);
}
public static void addLines(List<Line> lineList){
lineList.forEach(line -> lines.add(line));
}
public static void addLine(String lineName, String upwardName, String downwardName) {
if (stationRepository.checkNameLength(lineName)) {
throw new IllegalStateException();
}
if (checkExistLine(lineName) || !stationRepository.checkExistStation(upwardName)
|| !stationRepository.checkExistStation(downwardName) || upwardName.equals(downwardName)) {
throw new IllegalArgumentException();
}
Line line = new Line(lineName);
line.stations.add(new Station(upwardName));
line.stations.add(new Station(downwardName));
lines.add(line);
}
public static boolean deleteLineByName(String lineName) {
if (!checkExistLine(lineName)) {
throw new IllegalArgumentException();
}
return lines.removeIf(line -> Objects.equals(line.getName(), lineName));
}
public static boolean checkExistLine(String lineName) {
return lines.stream().anyMatch(o -> o.getName().equals(lineName));
}
}