-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathPersonName.java
More file actions
48 lines (37 loc) · 1.13 KB
/
PersonName.java
File metadata and controls
48 lines (37 loc) · 1.13 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
package nextstep.ladder.domain;
import java.util.Objects;
public class PersonName {
public static final int MAX_NAME_LENGTH = 5;
private String name;
public PersonName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
if (name.length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("Name is too long");
}
this.name = name;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonName that = (PersonName) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(" ".repeat((9 - name.length()) / 2));
sb.append(name);
sb.append(" ".repeat(9 - sb.length()));
return sb.toString();
}
}