-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathInputView.java
More file actions
85 lines (71 loc) · 2.34 KB
/
InputView.java
File metadata and controls
85 lines (71 loc) · 2.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package baseball.view;
import baseball.global.exception.CustomException;
import baseball.global.message.ErrorMessage;
import java.util.Scanner;
public class InputView {
public int[] enterNumbers() {
String input = readLine();
validateStringFormat(input);
int[] nums = validateNumberFormat(input);
return nums;
}
public int enterFinishType() {
String input = readLine();
int num = parseFinishType(input);
return num;
}
private int parseFinishType(String input) {
if (input.length() != 1 || !Character.isDigit(input.charAt(0))) {
throw CustomException.of(ErrorMessage.INVALID_FINISH_INPUT);
}
int num = input.charAt(0) - '0';
if (num != 1 && num != 2) {
throw CustomException.of(ErrorMessage.INVALID_FINISH_INPUT);
}
return num;
}
private void validateStringFormat(String str) {
if (isEmptyOrBlank(str)) {
throw CustomException.of(ErrorMessage.BLANK_INPUT);
}
if (str.length() != 3) {
throw CustomException.of(ErrorMessage.INVALID_INPUT_LENGTH);
}
}
private int[] validateNumberFormat(String str) {
int[] nums = new int[3];
for (int i = 0; i < 3; i++) {
validateDigit(str.charAt(i));
int num = str.charAt(i) - '0';
validatePositiveNumber(num);
nums[i] = num;
}
validateDuplicate(nums);
return nums;
}
private void validateDigit(char c) {
if (!Character.isDigit(c)) {
throw CustomException.of(ErrorMessage.NON_DIGIT_CHARACTER_FOUND);
}
}
private void validatePositiveNumber(int num) {
if (num <= 0) {
throw CustomException.of(ErrorMessage.NON_POSITIVE_NUMBER_FOUND);
}
}
private void validateDuplicate(int[] arr) {
if (hasDuplicate(arr)) {
throw CustomException.of(ErrorMessage.DUPLICATE_NUMBER_FOUND);
}
}
private boolean isEmptyOrBlank(String str) {
return str == null || str.isBlank();
}
private static boolean hasDuplicate(int[] arr) {
return arr[0] == arr[1] || arr[0] == arr[2] || arr[1] == arr[2];
}
private String readLine() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
}