Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.ArrayList;
- public class Person implements Comparable<Person>{
- public String Name;
- public int Age;
- private boolean Gender;
- private Person Father;
- private Person Mother;
- private Person StepMother;
- private Person StepFather;
- private ArrayList<Person> StepChildren = new ArrayList<Person>();
- private ArrayList<Person> Children = new ArrayList<Person>();
- public Person(String Name, int Age, boolean Gender){
- this.Name=Name;
- this.Age = Age;
- this.Gender = Gender;
- }
- public void SetFather(Person Father){
- this.Father= Father;
- this.Father.AddChild(this);
- }
- public void SetMother(Person Father){
- this.Mother= Father;
- this.Mother.AddChild(this);
- }
- public Person GetFather(){
- return this.Father;
- }
- public String About(){
- String GENDER;
- GENDER = Gender ?"Male":"Female";
- return this.Name+" is "+Age+" years old and "+GENDER;
- }
- public void AddChild(Person Child){
- Children.add(Child);
- Collections.sort(Children, Person.AgeComparator);
- }
- public ArrayList<Person> GetChildren(){
- return this.Children;
- }
- public static final Comparator<Person> AgeComparator = new Comparator<Person>(){
- @Override
- public int compare(Person o1, Person o2) {
- return o2.Age - o1.Age; // This will work because age is positive integer
- }
- };
- public int compareTo(Person p) {
- return p.Age - this.Age;
- }
- public static void main(String[] args){
- Person me = new Person("Dylan",19,true);
- me.SetFather(new Person("Ian", 51,true));
- me.GetFather().AddChild(new Person("Tom",22,true));
- System.out.println(me.GetFather().About());
- for(Person s: me.GetFather().GetChildren()){
- System.out.println(s.About());
- }
- }
- }
Add Comment
Please, Sign In to add comment