-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathBall.java
More file actions
47 lines (35 loc) · 1001 Bytes
/
Ball.java
File metadata and controls
47 lines (35 loc) · 1001 Bytes
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
package baseball;
import java.util.Objects;
public class Ball {
private final int position, ballNo;
public Ball(int position, int ballNo) {
this.position = position;
this.ballNo = ballNo;
}
public BallStatus play(Ball ball) {
// 1,4 - 2,5 / nothing
// 1,4 - 2,4 / ball
// 1,4 - 1,4 / strike
if (this.equals(ball)) {
return BallStatus.STRIKE;
}
if (ball.matchBallNumber(ballNo)) {
return BallStatus.BALL;
}
return BallStatus.NOTHING;
}
private boolean matchBallNumber(int ballNo) {
return this.ballNo == ballNo;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Ball)) return false;
Ball ball = (Ball) o;
return position == ball.position && ballNo == ball.ballNo;
}
@Override
public int hashCode() {
return Objects.hash(position, ballNo);
}
}