-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModel.java
More file actions
45 lines (38 loc) · 1.21 KB
/
Model.java
File metadata and controls
45 lines (38 loc) · 1.21 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
package racingcar.Model;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Model {
public List<Car> cars = new ArrayList<>();
public List<String> winner = new ArrayList<>();
public int frequency = 0;
public void moveCars() {
for (Car car : cars) {
car.move();
}
}
public void splitNames(String carNames) {
StringTokenizer st = new StringTokenizer(carNames, ",");
while (st.hasMoreTokens()) {
String str = st.nextToken();
Car car = new Car(str, 0, false);
cars.add(car);
if (str.length() > 5) {
throw new IllegalArgumentException("Car name should be less than 5 characters");
}
}
}
public void findWinner() {
int max = 0;
for (int i = 0; i < cars.size(); i++) { // 최댓값 찾기
if (cars.get(i).getProgress() >= max) {
max = cars.get(i).getProgress();
}
}
for (int i = 0; i < cars.size(); i++) { // 우승자 찾기
if (cars.get(i).getProgress() >= max) {
winner.add(cars.get(i).getName());
}
}
}
}