-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNameValid.java
More file actions
49 lines (41 loc) · 1.33 KB
/
NameValid.java
File metadata and controls
49 lines (41 loc) · 1.33 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
package racingcar.model;
import java.util.ArrayList;
import java.util.Arrays;
public class NameValid {
final ArrayList<String> names;
public ArrayList<String> toArrayList(String carsString) {
String[] carNames = carsString.trim().split(",");
return new ArrayList<>(Arrays.asList(carNames));
}
public NameValid(String carName) {
this.names = toArrayList(carName);
isValid();
}
// 자동차 이름이 5자 이하인지 확인
public void isNameValid() {
for (String name : names) {
if (name.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.");
}
}
}
// 자동차 이름이 중복되는지 확인
public void isNameDuplicate() {
for (int i = 0; i < names.size(); i++) {
for (int j = i + 1; j < names.size(); j++) {
if (names.get(i).equals(names.get(j))) {
// 에러 반환
throw new IllegalArgumentException("중복된 이름이 존재합니다.");
}
}
}
}
// 자동차 이름이 유효한지 확인
public void isValid() {
isNameValid();
isNameDuplicate();
}
public ArrayList<String> getNames() {
return names;
}
}