forked from next-step/java-baseball-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalls.java
More file actions
67 lines (58 loc) · 1.85 KB
/
Balls.java
File metadata and controls
67 lines (58 loc) · 1.85 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
63
64
65
66
67
package baseball.core;
import java.util.*;
public class Balls {
public static final int BALL_COUNT = 3;
private final List<Ball> balls;
public Balls(List<Integer> numbers) {
if (numbers == null || numbers.size() != BALL_COUNT) {
throw new IllegalArgumentException("Balls의 크기는 " + BALL_COUNT + "이어야 합니다.");
}
this.balls = convertIntegerToBall(numbers);
}
private static List<Ball> convertIntegerToBall(List<Integer> numbers) {
List<Ball> balls = new ArrayList<>();
int position = 1;
for (int number : numbers) {
balls.add(new Ball(number, position++));
}
return balls;
}
public PlayResult play(Balls other) {
int strikeCount = 0;
int ballCount = 0;
for (Ball ball : other.balls) {
BallStatus ballStatus = this.play(ball);
if (ballStatus.isStrike()) {
strikeCount += 1;
}
if (ballStatus.isBall()) {
ballCount += 1;
}
}
return new PlayResult(strikeCount, ballCount);
}
private BallStatus play(Ball other) {
Set<BallStatus> result = new HashSet<>();
for (Ball ball : this.balls) {
result.add(ball.play(other));
}
if (result.contains(BallStatus.STRIKE)) {
return BallStatus.STRIKE;
}
if (result.contains(BallStatus.BALL)) {
return BallStatus.BALL;
}
return BallStatus.NOTHING;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Balls balls1 = (Balls) o;
return Objects.equals(balls, balls1.balls);
}
@Override
public int hashCode() {
return Objects.hash(balls);
}
}