forked from next-step/java-baseball-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleInputView.java
More file actions
43 lines (37 loc) · 1.16 KB
/
ConsoleInputView.java
File metadata and controls
43 lines (37 loc) · 1.16 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
package baseball.console;
import baseball.core.Balls;
import baseball.core.InputView;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class ConsoleInputView implements InputView {
private final Scanner scanner = new Scanner(System.in);
@Override
public Balls getBalls(String message) {
Balls result = null;
do {
System.out.print(message);
String input = scanner.nextLine();
List<Integer> numbers = input.chars().boxed().map(c -> c - '0').collect(Collectors.toList());
try {
result = new Balls(numbers);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
} while (result == null);
return result;
}
@Override
public boolean shouldRetry(String message) {
while (true) {
System.out.print(message);
String input = scanner.nextLine().trim();
if (input.equals("1")) {
return true;
}
if (input.equals("2")) {
return false;
}
}
}
}