forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
39 lines (31 loc) · 897 Bytes
/
Person.java
File metadata and controls
39 lines (31 loc) · 897 Bytes
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
public class Person implements Comparable<Person> {
private String name;
public Person(String name) {
setName(name);
}
public void setName(String name) {
if (name == null || name.equals("")) {
throw new IllegalArgumentException("name can't be null or empty");
}
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
public int compareTo(Person other) {
return name.compareTo(other.name);
}
public boolean equals(Object other) {
if (null == other) return false;
if (this == other) return true;
if (!(other instanceof Person)) return false;
Person that = (Person) other;
return this.name.equals(that.name);
}
public int hashCode() {
return name.charAt(0) - 'A';
}
}