forked from next-step/java-baseball-playground
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputNumber.java
More file actions
58 lines (47 loc) · 1.73 KB
/
InputNumber.java
File metadata and controls
58 lines (47 loc) · 1.73 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
package util;
import exception.DuplicateNumberException;
import exception.NotOnlyNumberException;
import java.util.ArrayList;
import java.util.Scanner;
import static util.Constant.*;
/**
* 사용자가 입력한 숫자에대해 다루는 클래스
*/
public class InputNumber {
private final Scanner sc= new Scanner(System.in);
private final ArrayList<Integer> inputNumArray= new ArrayList<>();
public ArrayList<Integer> getInputNumArray() {
return inputNumArray;
}
public char[] inputNumberScanner() {
this.inputNumArray.clear();
String scStr = sc.nextLine();
return scStr.toCharArray();
}
public void inputNumberValidation(char[] scChar) {
ArrayIndexExceptionValidation(scChar);
for (char c : scChar) {
notOnlyExceptionValidation(c);
DuplicateNumberExceptionValidation(c);
this.inputNumArray.add(c - CHAR_TO_INT);
}
}
private void DuplicateNumberExceptionValidation(char c) {
if (inputNumArray.contains(c - CHAR_TO_INT)) {
throw new DuplicateNumberException("서로다른 숫자만 입력해주세요");
}
}
private void notOnlyExceptionValidation(char c) {
if (OutOfRangeNumber(c)) {
throw new NotOnlyNumberException("0~9사이의 숫자만 입력해주세요");
}
}
private boolean OutOfRangeNumber(char scChar) {
return scChar - CHAR_TO_INT > INPUT_NUMBER_MAXVALUE || scChar - CHAR_TO_INT < INPUT_NUMBER_MINVALUE;
}
private void ArrayIndexExceptionValidation(char[] scChar) {
if (scChar.length != INPUT_NUMBER_LENGTH) {
throw new ArrayIndexOutOfBoundsException("세자리 수를 입력해주세요.");
}
}
}