-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathInputView.java
More file actions
42 lines (37 loc) · 1.22 KB
/
InputView.java
File metadata and controls
42 lines (37 loc) · 1.22 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
package view;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
public class InputView {
private static final String RETRY_NUMBER = "1";
public static final String NOT_VALID = "NOT_VALID";
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public String getNumericInput() throws IOException {
String input = br.readLine();
if (!validateInput(input)) return NOT_VALID;
return input;
}
public boolean validateInput(String input){
if (input.length() != 3) return false;
try {
Integer.parseInt(input);
} catch (NumberFormatException e){
return false;
}
return isNotDuplicatedNumber(input);
}
private boolean isNotDuplicatedNumber(String input) {
Set<Character> charSet = new HashSet<>();
char[] charArray = input.toCharArray();
for (char c : charArray) {
charSet.add(c);
}
return charSet.size() == 3;
}
public boolean getInput() throws IOException {
String given = br.readLine();
return given.equals(RETRY_NUMBER);
}
}