-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathInputView.java
More file actions
102 lines (90 loc) · 2.95 KB
/
InputView.java
File metadata and controls
102 lines (90 loc) · 2.95 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package view;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InputView {
Scanner scanner;
public static final int DIGIT = 3; // 자리수
private static final String ASK_INPUT_NUM = "숫자를 입력해 주세요: ";
private static final String INPUT_ERROR_INT = "입력한 값이 숫자가 아닙니다. ";
private static final String INPUT_ERROR_BOUND = "1과 2중 값을 입력해주세요. ";
public InputView() {
scanner = new Scanner(System.in);
}
/**
* 1. input number
* 2. validation
* 3. return number
*/
public List<Integer> input() {
System.out.print(ASK_INPUT_NUM);
String inputValue = scanner.nextLine();
inputValidation(inputValue);
return parseToInputList(inputValue);
}
/**
* 두 가지 validation 진행
* 1. 자리수 확인
* 2. input type 확인
*/
public void inputValidation(String inputValue) {
if (!isValidateDigitCount(inputValue)) {
throw new IllegalArgumentException("입력한 숫자의 자릿수를 " + DIGIT + "자리 수로 입력해주세요.");
}
if (!isValidateInt(inputValue)) {
throw new IllegalArgumentException(INPUT_ERROR_INT);
}
}
/**
* @param inputValue: input() 메소드에서 Scanner로 받은 값
* @return 자리 수 확인 후 true/false
*/
public boolean isValidateDigitCount(String inputValue) {
int digitCount = String.valueOf(inputValue).length();
return digitCount == DIGIT;
}
/**
* @param inputValue: input() 메소드에서 Scanner로 받은 값
* @return int 확인 후 true/false
*/
public boolean isValidateInt(String inputValue) {
try {
Integer.parseInt(String.valueOf(inputValue));
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* List로 변환하는 메소드
* @param inputValue int
* @return List<Integer>
*/
public List<Integer> parseToInputList(String inputValue) {
List<Integer> intList = new ArrayList<>();
for (int i = 0; i<DIGIT; i++) {
char digitChar = inputValue.charAt(i);
int digit = Character.getNumericValue(digitChar);
intList.add(digit);
}
return intList;
}
/**
* 게임 다시 할건지 사용자로 부터 답을 받는 메소드
* @return true/false
*/
public boolean isReplay() {
String inputValue = scanner.nextLine();
if (!isValidateInt(inputValue)) {
throw new IllegalArgumentException(INPUT_ERROR_INT);
}
int isReplay = Integer.parseInt(inputValue);
if (isReplay == 1) {
return true;
} else if (isReplay == 2) {
return false;
} else {
throw new IllegalArgumentException(INPUT_ERROR_BOUND);
}
}
}