// This code example is from the following source: // // Book Title: Programming with Objects, A Comparative Presentation // of Object-Oriented Programming with C++ and Java // // Chapter: Chapter 15 Extending Classes // // Section: Section 15.20 A Java Study Of A Small Class Hierarchy Exhibiting Moderately Complex Behavior // // The links to the rest of the code in this book are at // // http://programming-with-objects.com/pwocode.html // // For further information regarding the book, please visit // // http://programming-with-objects.com // //Manager.java import java.util.*; // for Comparator, Collections, etc //////////////////////////// class Date ///////////////////////////// class Date implements Cloneable { private int month; private int day; private int year; public Date( int mm, int dd, int yy ) { month = mm; day = dd; year = yy; } public String toString() { return month + " : " + day + " : " + year; } public Object clone() { Date date = null; try { date = ( Date ) super.clone(); } catch( CloneNotSupportedException e ) {} return date; } } ///////////////////////////// class Cat ///////////////////////////// class Cat implements Cloneable { private String name; private int age; public Cat( String nam, int a ) { name = nam ; age = a; } public String toString() { return " Name: " + name + " Age: " + age; } public Object clone() { Cat cat = null; try { cat = ( Cat ) super.clone(); } catch( CloneNotSupportedException e ) {} return cat; } } ///////////////////////////// class Dog ///////////////////////////// class Dog implements Cloneable { private String name; private int age; public Dog( String nam, int a ) { name = nam; age = a; } public String toString() { return "\nName: " + name + " Age: " + age; } public String getName() { return name; } public int getAge() { return age; } public void print() { System.out.println( this ); } public Object clone() { Dog dog = null; try { dog = ( Dog ) super.clone(); } catch( CloneNotSupportedException e ) {} return dog; } public double getDogCompareParameter(){ return 0; } //(G) public static class Dog_Comparator implements Comparator { //(H) public int compare( Object o1, Object o2 ) { Dog d1 = (Dog) o1; Dog d2 = (Dog) o2; if ( d1.getDogCompareParameter() == d2.getDogCompareParameter() ) return 0; return ( d1.getDogCompareParameter() < d2.getDogCompareParameter() ) ? -1 : 1; } } } /////////////////////////// class Employee ////////////////////////// class Employee { // intentionally left uncloneable String firstName, lastName; Date dateOfBirth; Employee[] friends; Auto[] autos; Cat kitty; ArrayList dogs; Map phoneList; public Employee( String first, String last ) { firstName = first; lastName = last; } public Employee( String first, String last, Date dob ) { firstName = first; lastName = last; dateOfBirth = dob == null ? null : (Date) dob.clone(); } public Employee( String first, String last, Date dob, Cat kit ) { firstName = first; lastName = last; dateOfBirth = dob == null ? null : (Date) dob.clone(); kitty = kit == null ? null : (Cat) kit.clone(); } public Employee( String first, String last, ArrayList dogs ) { firstName = first; lastName = last; this.dogs = dogs == null ? null : (ArrayList) dogs.clone(); } Employee( String first, String last, Date dob, Employee[] fnds ) { firstName = first; lastName = last; dateOfBirth = dob == null ? null : (Date) dob.clone(); friends = fnds == null ? null : (Employee[]) fnds.clone(); } Employee( String first, String last, Map phoneList ) { firstName = first; lastName = last; this.phoneList = phoneList == null ? null : new TreeMap( (TreeMap) phoneList ); // creates the same mappings } Employee( String first, String last, Date dob, Employee[] fnds, Auto[] ats, Cat c ) { firstName = first; lastName = last; dateOfBirth = dob == null ? null : (Date) dob.clone(); friends = fnds == null ? null : (Employee[]) fnds.clone(); autos = ats == null ? null : (Auto[]) ats.clone(); kitty = c == null ? null : (Cat) c.clone(); } Employee( Employee e ) { firstName = e.firstName; lastName = e.lastName; dateOfBirth = e.dateOfBirth == null ? null : (Date) e.dateOfBirth.clone(); friends = e.friends == null ? null : (Employee[]) e.friends.clone(); autos = e.autos == null ? null : (Auto[]) e.autos.clone(); kitty = e.kitty == null ? null : (Cat) e.kitty.clone(); phoneList = e.phoneList == null ? null : new TreeMap( (TreeMap) e.phoneList ); } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void addDogToDogs( Dog newDog ) { //(I) if ( dogs == null ) dogs = new ArrayList(); dogs.add( newDog ); Collections.sort( dogs, new Dog.Dog_Comparator() ); //(J) } public String toString() { String str = ""; if ( dogs != null ) { str += "\nDOGS: "; ListIterator iter = dogs.listIterator(); while ( iter.hasNext() ) str += (Dog) iter.next(); str += "\n"; } if ( autos != null ) { str += "\nAUTOS: "; for ( int i=0; i