-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathStationRepository.java
More file actions
51 lines (41 loc) · 1.53 KB
/
Copy pathStationRepository.java
File metadata and controls
51 lines (41 loc) · 1.53 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
package subway.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import subway.message.ErrorMessage;
public class StationRepository {
private static final List<Station> stations = new ArrayList<>();
public static List<Station> stations() {
return Collections.unmodifiableList(stations);
}
private static boolean stationNameExists(String name) {
return stations.stream().anyMatch(station -> station.getName().equals(name));
}
public static void validateStationNameDuplicate(String stationName)
throws IllegalArgumentException {
if (stationNameExists(stationName)) {
throw new IllegalArgumentException(
ErrorMessage.STATION_REPOSITORY_STATION_ALREADY_EXIST.toString()
);
}
}
public static void validateStationNameExist(String stationName)
throws IllegalArgumentException {
if (!stationNameExists(stationName)) {
throw new IllegalArgumentException(
ErrorMessage.STATION_REPOSITORY_STATION_DOES_NOT_EXIST.toString()
);
}
}
public static void addStation(Station station) throws IllegalArgumentException {
validateStationNameDuplicate(station.getName());
stations.add(station);
}
public static boolean deleteStation(String name) {
return stations.removeIf(station -> Objects.equals(station.getName(), name));
}
public static void deleteAll() {
stations.clear();
}
}