-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathRacing.java
More file actions
50 lines (42 loc) · 1.19 KB
/
Racing.java
File metadata and controls
50 lines (42 loc) · 1.19 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
package racingcar;
import java.util.List;
import java.util.stream.Collectors;
public class Racing {
private final List<Car> cars;
private final int attempts;
public Racing(List<Car> cars, int attempts) {
this.cars = cars;
this.attempts = attempts;
}
public void start() {
System.out.println("\n실행 결과");
for (int i = 0; i < attempts; i++) {
raceRound();
printPositions();
}
}
private void raceRound() {
for (Car car : cars) {
car.move();
}
}
private void printPositions() {
for (Car car : cars) {
System.out.printf("%s : %s%n", car.getName(), "-".repeat(car.getPosition()));
}
System.out.println();
}
public List<String> getWinners() {
int maxPosition = getMaxPosition();
return cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.map(Car::getName)
.collect(Collectors.toList());
}
private int getMaxPosition() {
return cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(0);
}
}