cockroachlurcher

Person Class

Aug 30th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.Comparator;
  3. import java.util.ArrayList;
  4.  
  5.  
  6.  
  7. public class Person implements Comparable<Person>{
  8.     public String Name;
  9.     public int Age;
  10.     private boolean Gender;
  11.     private Person Father;
  12.     private Person Mother;
  13.     private Person StepMother;
  14.     private Person StepFather;
  15.     private ArrayList<Person> StepChildren = new ArrayList<Person>();
  16.     private ArrayList<Person> Children = new ArrayList<Person>();
  17.    
  18.     public Person(String Name, int Age, boolean Gender){
  19.         this.Name=Name;
  20.         this.Age = Age;
  21.         this.Gender = Gender;
  22.     }
  23.     public void SetFather(Person Father){
  24.         this.Father= Father;
  25.         this.Father.AddChild(this);
  26.     }
  27.    
  28.     public void SetMother(Person Father){
  29.         this.Mother= Father;
  30.         this.Mother.AddChild(this);
  31.     }
  32.     public Person GetFather(){
  33.         return this.Father;
  34.     }
  35.     public String About(){
  36.         String GENDER;
  37.         GENDER = Gender ?"Male":"Female";
  38.         return this.Name+" is "+Age+" years old and "+GENDER;
  39.     }
  40.     public void AddChild(Person Child){
  41.         Children.add(Child);
  42.     Collections.sort(Children, Person.AgeComparator);
  43.     }
  44.     public ArrayList<Person> GetChildren(){
  45.         return this.Children;
  46.     }
  47.      
  48.    
  49.     public static final Comparator<Person> AgeComparator = new Comparator<Person>(){
  50.             @Override
  51.             public int compare(Person o1, Person o2) {
  52.                 return o2.Age - o1.Age;  // This will work because age is positive integer
  53.             }
  54.     };
  55.     public int compareTo(Person p) {
  56.           return p.Age - this.Age;
  57.     }      
  58.    
  59.    
  60.    
  61.    
  62.    
  63.     public static void main(String[] args){
  64.         Person me = new Person("Dylan",19,true);
  65.         me.SetFather(new Person("Ian", 51,true));
  66.         me.GetFather().AddChild(new Person("Tom",22,true));
  67.         System.out.println(me.GetFather().About());
  68.             for(Person s: me.GetFather().GetChildren()){
  69.                 System.out.println(s.About());
  70.             }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment