-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCars.java
More file actions
52 lines (43 loc) · 1.4 KB
/
Cars.java
File metadata and controls
52 lines (43 loc) · 1.4 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
package domain;
import java.util.List;
import java.util.stream.Collectors;
import static view.OutputView.printGameStatus;
public class Cars {
private final List<Car> cars;
public Cars(List<String> cars) {
validate(cars);
this.cars = cars
.stream()
.map(Car::new)
.collect(Collectors.toList());
}
private void validate(List<String> cars) {
boolean checkName = cars.stream()
.distinct()
.count() != cars.size();
if (checkName) {
throw new IllegalArgumentException("[ERROR] 중복된 이름입니다.");
}
}
public void moveCars() {
cars.stream()
.filter(car -> Engine.isPower())
.forEach(Car::go);
}
public void printCars() {
cars.forEach(car ->
printGameStatus(car.getName(), car.getPosition()));
}
public Winners findWinner() {
Car maxPositionCar = findMaxPositionCar();
return new Winners(cars.stream()
.filter(maxPositionCar::isSamePosition)
.map(Winner::new)
.collect(Collectors.toList()));
}
private Car findMaxPositionCar() {
return cars.stream()
.max(Car::compareTo)
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 입력된 차량이 없습니다."));
}
}