All pastes #2113339 Raw Edit

Stuff

public text v1 · immutable
#2113339 ·published 2012-02-08 20:59 UTC
rendered paste body
package oving3;

import java.util.ArrayList;

public class Person {
	
	String name;
	Person mother;
	Person father;
	ArrayList<Person> children;

	public Person(){
		
	}
	
	public Person(String name) {
		this.name = name;
		this.children = new ArrayList<Person>();
	}
	
/*Create a method that tests whether this Person is the mother of the Person given as an argument to the method. 
It is not enough to check whether the Person has this Person registered as his or her mother, you must check 
that the Person is in this Person's children list, too. Consistency is key here.
*/
	public boolean isMotherOf(Person child){
		if (this.children != null){
			return (child.mother == this && this.children.contains(child));
		} else return false;
	}
	
	public boolean isFatherOf(Person child){
		if (this.children != null){
			return (child.father == this && this.children.contains(child));
		} else return false;
	}
	
	public boolean isSiblingOf(Person sibling){
		if (this.mother != null && sibling.mother != null && this.father != null && sibling.father != null && this != sibling){
			return (this.mother == sibling.mother && this.father == sibling.father);
		} else return false;
	}
	
	public void setFather(Person father){
		this.father = father;
	}
	
	public void setMother(Person mother){
		this.mother = mother;
	}

	public void setChild(Person child){
		this.children.add(child);
	}
	
	public void setChild(Person child1, Person child2){
		this.children.add(child1);
		this.children.add(child2);
	}
	
	public void setChild(Person child1, Person child2, Person child3){
		this.children.add(child1);
		this.children.add(child2);
		this.children.add(child3);
	}

	@Override
	public String toString() {
		String string = "Navn :" + this.name + "\nFar : ";
		if (this.father != null){
			string += this.father.name + "\nMor : ";
		} else string += "ukjent\nMor : ";
		if (this.mother != null){
			string += this.mother.name + "\nBarn : ";
		} else string += "ukjent\nBarn : ";
		if (this.children != null && this.children.size() > 0){
			for (int i = 0; i < this.children.size(); ++i){
				if (i > 0)
					string += ", ";
				string += children.get(i).name;
			}
		} else string += "ingen";
		string += "\n";
		return string;
			
		
	}


	
	
}