-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathBalls.java
More file actions
58 lines (42 loc) · 1.38 KB
/
Balls.java
File metadata and controls
58 lines (42 loc) · 1.38 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
package baseball;
import java.util.ArrayList;
import java.util.List;
public class Balls {
private static final int PLAY_AGAIN = 1;
private final List<Ball> answers;
public Balls(List<Integer> answers) {
if (!ValidationUtils.ValidNoList(answers)) {
throw new IllegalArgumentException("입력한 수를 다시 확인해 주세요.");
}
this.answers = mapBall(answers);
}
private static List<Ball> mapBall(List<Integer> answers) {
List<Ball> balls = new ArrayList<>();
for (int i = 0; i < answers.size(); i++) {
balls.add(new Ball(i + 1, answers.get(i)));
}
return balls;
}
public PlayResult play(List<Integer> balls) {
Balls userBalls = new Balls(balls);
PlayResult result = new PlayResult();
for (Ball answer : answers) {
BallStatus status = userBalls.play(answer);
result.report(status);
}
return result;
}
public BallStatus play(Ball userBall) {
return answers.stream()
.map(answer -> answer.play(userBall))
.filter(status -> status != BallStatus.NOTHING)
.findFirst()
.orElse(BallStatus.NOTHING);
}
public boolean again(int input) {
if (input == PLAY_AGAIN) {
return true;
}
return false;
}
}