-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAllRacingCars.java
More file actions
62 lines (48 loc) · 1.63 KB
/
AllRacingCars.java
File metadata and controls
62 lines (48 loc) · 1.63 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
package racingcar.model.domain;
import static racingcar.common.ErrorMessage.ERROR_SINGLE_RACING_CAR_NAME;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class AllRacingCars {
public static final int MIN_CAR_COUNT = 2;
private final List<RacingCar> cars;
private AllRacingCars(List<RacingCar> cars) {
validate(cars);
this.cars = cars;
}
public static AllRacingCars from(List<String> carNames) {
return new AllRacingCars(convertToRacingCar(carNames));
}
public Round playOneRound() {
return Round.from(moveAllRacingCars());
}
public FinalWinners getFinalWinners() {
return FinalWinners.of(cars, calculateMaxPosition());
}
private void validate(List<RacingCar> cars) {
if (cars.size() < MIN_CAR_COUNT) {
throw new IllegalArgumentException(ERROR_SINGLE_RACING_CAR_NAME);
}
}
private static List<RacingCar> convertToRacingCar(List<String> carNames) {
return carNames.stream().map(RacingCar::from).toList();
}
private int calculateMaxPosition() {
return cars.stream()
.mapToInt(RacingCar::getPosition)
.max()
.orElse(0);
}
private Map<RacingCar, Integer> moveAllRacingCars() {
return cars.stream()
.map(car -> {
car.move();
return car;
})
.collect(Collectors.toMap(
Function.identity(),
RacingCar::getPosition
));
}
}