-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathParticipant.java
70 lines (56 loc) · 2 KB
/
Participant.java
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
59
60
61
62
63
64
65
66
67
68
69
70
package nextstep.ladder.domain;
import nextstep.ladder.exception.CanNotMoveException;
import nextstep.ladder.exception.CannotRegisterNameException;
public class Participant {
public static final int MIN_NAME_LENGTH = 1;
public static final int MAX_NAME_LENGTH = 5;
private final String name;
private int position;
public static Participant nameOf(String name, int position) {
validateNameLength(name.trim());
return new Participant(name.trim(), position);
}
private Participant(String name, int position) {
this.name = name;
this.position = position;
}
private static void validateNameLength(String name) {
if (name.length() < 1) {
throw new CannotRegisterNameException(String.format("이름은 %d 글자 이상이어야 합니다.", Participant.MIN_NAME_LENGTH));
}
if (name.length() > 5) {
throw new CannotRegisterNameException(String.format("이름은 %d 글자를 초과할 수 없습니다.", Participant.MAX_NAME_LENGTH));
}
}
public void moveFront(int participantCount) {
canMoveFront(participantCount);
this.position++;
}
private void canMoveFront(int participantCount) {
if (position == participantCount - 1) {
throw new CanNotMoveException("제일 오른쪽에 위치해, 더 이상 오른쪽으로 이동할 수 없습니다.");
}
}
public void moveBack() {
canMoveBack();
this.position--;
}
private void canMoveBack() {
if (this.position == 0) {
throw new CanNotMoveException("제일 왼쪽에 위치해, 더 이상 왼쪽으로 이동할 수 없습니다.");
}
}
public String getName() {
return name;
}
public int getPosition() {
return position;
}
@Override
public String toString() {
return "Participant{" +
"name='" + name + '\'' +
", position=" + position +
'}';
}
}