-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCar.java
More file actions
50 lines (40 loc) · 1.14 KB
/
Car.java
File metadata and controls
50 lines (40 loc) · 1.14 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
50
package domain;
public class Car {
private static final String BLANK = " ";
private static final int MAXIMAL_LENGTH = 5;
private final String name;
private int position;
public Car(String name) {
validate(name);
this.name = name;
this.position = 0;
}
public Car(String name, int position) {
validate(name);
this.name = name;
this.position = position;
}
private void validate(String name) {
if (name.length() > MAXIMAL_LENGTH) {
throw new IllegalArgumentException("[ERROR] 자동차 이름은 5자 이하여야 한다.");
}
if (name.contains(BLANK)) {
throw new IllegalArgumentException("[ERROR] 자동차 이름은 공백은 포함하지 않아야 한다.");
}
}
public boolean isSamePosition(Car car) {
return car.position == this.position;
}
public int compareTo(Car car) {
return this.position - car.position;
}
public String getName() {
return name;
}
public int getPosition() {
return position;
}
public void go() {
position++;
}
}