-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCars.java
More file actions
50 lines (40 loc) · 1.46 KB
/
Cars.java
File metadata and controls
50 lines (40 loc) · 1.46 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 Domain;
import utils.NumberGenerator;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Cars { //자동차들의 정보 저장
private static final int DEFAULT_POSITION = 0; //초기 위치=0
private final List<Car> cars; //자동차들의 정보를 저장할 리스트 선언
public Cars(final List<Car> cars){
this.cars = Collections.unmodifiableList(cars);
}
public static Cars InitialInform(final List<String> carNames){ //carNames의 각 요소를 Car객체로 변환하여 리스트에 저장
List<Car> cars = carNames.stream()
.map(Car::InitialInform)
.collect(Collectors.toList());
return new Cars(cars);
}
public void moveAll(final NumberGenerator numberGenerator){
for(Car car : cars){
int number = numberGenerator.generate();
car.move(number);
}
}
public Cars findWinners(){
int maxPosition = getMaxPosition();
List<Car> winningCars = cars.stream()
.filter(car -> car.getPosition() == maxPosition)
.collect(Collectors.toList());
return new Cars(winningCars);
}
private int getMaxPosition(){
return cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(DEFAULT_POSITION);
}
public List<Car> getCars(){
return cars;
}
}