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
57 lines (48 loc) · 1.77 KB
/
BaseballService.java
File metadata and controls
57 lines (48 loc) · 1.77 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
package baseball.api;
import baseball.domain.BaseballInputClient;
import baseball.domain.Number;
import baseball.domain.Numbers;
import baseball.domain.RandomNumbersSpecification;
import baseball.error.NumberDuplicateException;
import baseball.error.NumberOfInputException;
import baseball.domain.Result;
import java.util.List;
public class BaseballService {
private final RandomNumbersSpecification randomNumbersSpecification;
private final BaseballInputClient baseballInputClient;
public BaseballService(RandomNumbersSpecification randomNumbersSpecification, BaseballInputClient baseballInputClient) {
this.randomNumbersSpecification = randomNumbersSpecification;
this.baseballInputClient = baseballInputClient;
}
public void startBaseball() {
Numbers storeNumbers = storeRandomNumbers();
while (true) {
try {
Numbers inputNumbers = inputNumbers();
Result result = inputNumbers.compareTo(storeNumbers);
if (result.isThreeStrikes()) {
break;
}
System.out.println(result.hint());
} catch (IllegalArgumentException e) {
break;
}
}
}
public Numbers storeRandomNumbers() {
try {
return randomNumbersSpecification.storeNumber();
} catch (NumberDuplicateException e) {
return storeRandomNumbers();
}
}
public Numbers inputNumbers() {
List<Number> request = baseballInputClient.inputNumbers();
try {
return new Numbers(request);
} catch (NumberOfInputException | NumberDuplicateException e) {
System.out.println(e.getMessage());
return inputNumbers();
}
}
}