forked from woowacourse-precourse/java-baseball-6
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIOUtils.java
More file actions
47 lines (41 loc) · 1.3 KB
/
IOUtils.java
File metadata and controls
47 lines (41 loc) · 1.3 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
package baseball.utils;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class IOUtils {
public static void validateInput(String input) {
if (input.length() != 3) {
throw new IllegalArgumentException();
}
Set<Character> charSet = new HashSet<>();
for (char ch : input.toCharArray()) {
if (!Character.isDigit(ch) || ch == '0' || !charSet.add(ch)) {
throw new IllegalArgumentException();
}
}
}
public static Boolean getResult(List<Integer> computerNumbers, List<Integer> userNumbers) {
int strikes = 0;
int balls = 0;
String result = "";
for (int i = 0; i < 3; i++) {
if (userNumbers.get(i).equals(computerNumbers.get(i))) {
strikes++;
} else if (computerNumbers.contains(userNumbers.get(i))) {
balls++;
}
}
if (strikes == 3) {
result = "3스트라이크";
System.out.println(result);
return true;
}
if (strikes == 0 && balls == 0) {
result = "낫싱";
} else {
result = balls + "볼 " + strikes + "스트라이크";
}
System.out.println(result);
return false;
}
}