// This is a simple example of a class.  All people have names and ages; some have
// spouses as well. The data is all private; access to it comes via getters and setters.

public class Person {
	private String name;
	private int age;
	private Person spouse;
	
	public Person(String name) {
		this.name = name;
		age = 0;
		spouse = null;
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}
	
	public Person getSpouse() {
		return spouse;
	}

	public void setSpouse(Person spouse) {
		this.spouse = spouse;
	}
	
	public void marries( Person spouse) {
		setSpouse(spouse);
		spouse.setSpouse(this);
	}

	public boolean isMarried() {
		return getSpouse() != null;
	}
	
	public String toString() {
		if (isMarried())
			return String.format( "%s is married to %s", getName(), getSpouse().getName());
		else
			return String.format( "%s is %d years old.", getName(), getAge());
	}
	
	public static void main(String[] args) {
		Person p1 = new Person( "John");
		Person p2 = new Person( "Mary");
		
		p1.marries(p2);
		System.out.println(p1);
		System.out.println(p2);

	}

}
