forked from next-step/java-baseball-playground
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseballService.java
More file actions
31 lines (27 loc) · 1.02 KB
/
BaseballService.java
File metadata and controls
31 lines (27 loc) · 1.02 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
package baseball.service;
import baseball.domain.Action;
import baseball.domain.Ball;
import baseball.domain.Baseball;
import baseball.domain.BaseballStatus;
public class BaseballService {
public boolean isStrike(Ball userBall, Ball randomBall) {
return userBall.getNumber() == randomBall.getNumber();
}
public boolean isBall(Ball userBall, Baseball randomBall) {
return randomBall.getBaseballs().stream().anyMatch(ball -> ball.getNumber()==userBall.getNumber());
}
public BaseballStatus compare(Baseball userBall, Baseball randomBall) throws Exception {
int strike = 0;
int ball = 0;
for (int i = 0; i < userBall.getBaseballs().size(); i++) {
if (isStrike(userBall.getBaseballs().get(i), randomBall.getBaseballs().get(i))) {
strike++;
continue;
}
if (isBall(userBall.getBaseballs().get(i), randomBall)) {
ball++;
}
}
return new BaseballStatus(ball, strike);
}
}