Thursday, 18 February 2016

Class Composition Example (Genealogy)


Person.java
  
   

package my.kkc.geneology;

public class Person {
    private String name;
    private Person father;
    private Person mother;

    public Person() {
        father = new Person();
        mother = new Person();
    }

    public Person(String name, Person father, Person mother) {
        this.name = name;
        this.father = father;
        this.mother = mother;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getFather() {
        return father;
    }

    public void setFather(Person father) {
        this.father = father;
    }

    public Person getMother() {
        return mother;
    }

    public void setMother(Person mother) {
        this.mother = mother;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (name != null ? !name.equals(person.name) : person.name != null) return false;
        if (father != null ? !father.equals(person.father) : person.father != null) return false;
        return mother != null ? mother.equals(person.mother) : person.mother == null;

    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (father != null ? father.hashCode() : 0);
        result = 31 * result + (mother != null ? mother.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", father=" + father +
                ", mother=" + mother +
                '}';
    }
}
  
Marriage.java
  
   
package my.kkc.geneology;

public class Marriage {
    private Person husband;
    private Person wife;

    public Marriage() {
        husband = new Person();
        wife = new Person();
    }

    public Marriage(Person husband, Person wife) {
        this.husband = husband;
        this.wife = wife;
    }

    public Person getHusband() {
        return husband;
    }

    public void setHusband(Person husband) {
        this.husband = husband;
    }

    public Person getWife() {
        return wife;
    }

    public void setWife(Person wife) {
        this.wife = wife;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Marriage marriage = (Marriage) o;

        if (husband != null ? !husband.equals(marriage.husband) : marriage.husband != null) return false;
        return wife != null ? wife.equals(marriage.wife) : marriage.wife == null;

    }

    @Override
    public int hashCode() {
        int result = husband != null ? husband.hashCode() : 0;
        result = 31 * result + (wife != null ? wife.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Marriage{" +
                "husband=" + husband +
                ", wife=" + wife +
                '}';
    }
}
  
Main.java
  
   
package my.kkc.geneology;

public class Main {
    public static void main(String[] args) {
        geneologyFn();
    }
    static void geneologyFn() {
        Person a = new Person("Jivabhai",null,null);
        Person b = new Person("Jyotiben",null,null);
        Person c = new Person("Karsanbhai",a,b);
        Person d = new Person("Ambaben",null,null);
        Person e = new Person("Godadbhai",a,b);
        Person f = new Person("Navalben",null,null);
        Person g = new Person("Kundan",c,d);
        Person h = new Person("Vinod",e,f);

        Marriage m1 = new Marriage(a,b);
        Marriage m2 = new Marriage(c,d);
        Marriage m3 = new Marriage(e,f);

        System.out.println(g.getFather().getFather());
        System.out.println(h.getMother().getFather());

    }
}
  

No comments:

Post a Comment