-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathInputView.java
More file actions
55 lines (43 loc) · 1.7 KB
/
InputView.java
File metadata and controls
55 lines (43 loc) · 1.7 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
package view;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
public class InputView {
private static final String CAR_NAME_DELIMITER = ",";
private final BufferedReader reader;
public InputView(final InputStream inputStream) {
this.reader = new BufferedReader(new InputStreamReader(inputStream));
}
public String[] readCarNames() throws IOException {
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
final String userInput = removeBlank(reader.readLine());
validateCarNamesFormat(userInput);
return userInput.split(CAR_NAME_DELIMITER);
}
public Integer readNumberOfAttempts() throws IOException {
System.out.println("시도할 회수는 몇회인가요?");
final String userInput = removeBlank(reader.readLine());
validateNumberOfAttempts(userInput);
return Integer.parseInt(userInput);
}
private void validateCarNamesFormat(final String names) {
if (!Pattern.matches("^[A-z0-9,]+$", names)) {
throw new IllegalArgumentException("형식에 맞게 다시 입력하세요.\n");
}
}
private void validateNumberOfAttempts(final String text) {
try {
Integer.parseInt(text);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("1부터 10 이하의 숫자를 입력하세요.\n");
}
}
private String removeBlank(final String text) {
if (text == null) {
return null;
}
return text.replaceAll(" ", "");
}
}