Example 23.14 gives the code for the final version of the Person class. In its current state it can be expected to perform predictably in many application contexts.
Example 23.14: Person.java (Final Version)
1 import java.util.*; 2 import java.io.*; 3 4 public class Person implements Cloneable, Comparable, Serializable { 5 private String first_name = null; 6 private String middle_name = null; 7 private String last_name = null; 8 private Calendar birthday = null; 9 private String gender = null; 10 11 public static final String MALE = "Male"; 12 public static final String FEMALE = "Female"; 13 14 public Person(String f_name, String m_name, String l_name, int dob_year, int dob_month, 15 int dob_day, String gender){ 16 first_name = f_name; 17 middle_name = m_name; 18 last_name = l_name; 19 this.gender = gender; 20 21 birthday = Calendar.getInstance(); 22 birthday.set(dob_year, dob_month, dob_day); 23 } 24 25 public int getAge(){ 26 Calendar today = Calendar.getInstance(); 27 int now = today.get(Calendar.YEAR); 28 int then = birthday.get(Calendar.YEAR); 29 return (now - then); 30 } 31 32 public String getFullName(){ return (first_name + " " + middle_name + " " + last_name); } 33 34 public String getFirstName(){ return first_name; } 35 public void setFirstName(String f_name) { first_name = f_name; } 36 37 public String getMiddleName(){ return middle_name; } 38 public void setMiddleName(String m_name){ middle_name = m_name; } 39 40 public String getLastName(){ return last_name; } 41 public void setLastName(String l_name){ last_name = l_name; } 42 43 public String getNameAndAge(){ return (getFullName() + " " + getAge()); } 44 45 public String getGender(){ return gender; } 46 public void setGender(String gender){ this.gender = gender; } 47 48 public void setBirthday(int year, int month, int day){ birthday.set(year, month, day); } 49 50 public String toString(){ 51 return this.getFullName() + " " + gender + " " + birthday.get(Calendar.DATE) + "/" 52 + birthday.get(Calendar.MONTH) + "/" + birthday.get(Calendar.YEAR); 53 } 54 55 public boolean equals(Object o){ 56 if(o == null) return false; 57 boolean is_equal = false; 58 if(o instanceof Person){ 59 if(this.first_name.equals(((Person)o).first_name) && 60 this.middle_name.equals(((Person)o).middle_name) && 61 this.last_name.equals(((Person)o).last_name) && this.gender.equals(((Person)o).gender) && 62 (this.birthday.get(Calendar.YEAR) == ((Person)o).birthday.get(Calendar.YEAR)) && 63 (this.birthday.get(Calendar.MONTH) == ((Person)o).birthday.get(Calendar.MONTH)) && 64 (this.birthday.get(Calendar.DATE) == ((Person)o).birthday.get(Calendar.DATE)) ){ 65 is_equal = true; 66 } 67 } 68 return is_equal; 69 } 70 71 public int hashCode(){ 72 return this.toString().hashCode(); 73 } 74 75 public Object clone() throws CloneNotSupportedException { 76 super.clone(); 77 return new Person(new String(first_name), new String(middle_name), new String(last_name), 78 birthday.get(Calendar.YEAR), birthday.get(Calendar.MONTH), birthday.get(Calendar.DATE), 79 new String(gender)); 80 } 81 82 public int compareTo(Object o){ 83 return this.toString().compareTo(o.toString()); 84 } 85 86 } //end Person class