Skip to content

Instantly share code, notes, and snippets.

@beatgammit
Last active December 12, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beatgammit/4690251 to your computer and use it in GitHub Desktop.
Save beatgammit/4690251 to your computer and use it in GitHub Desktop.
Attempt at a super explicit class definition for CS 340. Pedigree is assumed to be a tree structure as described in getPedigree().
import java.util.Date;
/**
* @author T. Jameson Little
*
* Person represents a person in a family tree.
* For this application, father and mother are assumed to be biological parents.
*
* Domain:
* - birthdate: Date when the Person was born (Date object)
* - deathdate: Date when the Person died (can can be empty/null)
* - father: father of the person (Person)
* - mother: mother of the person (Person)
*
* Age is calculated from the birthdate and the deathdate. If the deathdate is not provided,
* age is just the current time - birthdate.
*/
public class Person {
private String name;
private Date birthdate, deathdate;
private Person father, mother;
/**
* Default constructor. All values are set to null.
*
* @postcondition All getters return null, except getName() which returns
* the empty string and getPedigree() which returns an empty
* Pedigree
*/
public Person() {
}
/**
* The same pre-conditions and post-conditions follow as for manually calling
* the set functions:
*
* @precondition birthdate must be earlier than deathdate (if both non-null)
* @precondition father must not be the same as mother
* @precondition if name is null, it will be treated as the empty string
* @precondition if mother is not null and birthdate is not null,
* mother's birthdate must be earlier than birthdate or null and
* mother's deathdate must be later than birthdate or null
* @precondition if father is not null and birthdate is not null,
* father's birthdate must be earlier than birthdate or null and
* father's birthdate must be no earlier than 9 months before birthdate
*
* @postcondition getName() returns name or the empty string if name is null
* @postcondition getBirthdate() returns birthdate
* @postcondition getDeathdate() returns deathdate
* @postcondition getFather() returns father
* @postcondition getMother() returns mother
*
* @param name The name of this Person
* @param birthdate The birth date of this person
* @param deathdate The death date of this person
* @param father The father of this person
* @param mother The mother of this person
* @throws IllegalArgumentException if father == mother
*/
public Person(String name, Date birthdate, Date deathdate, Person father, Person mother) throws IllegalArgumentException {
if (father == mother) {
throw new IllegalArgumentException();
}
this.setName(name);
this.setBirthdate(birthdate);
this.setDeathdate(deathdate);
this.setFather(father);
this.setMother(mother);
}
/**
* Get the name of this person. This function returns the empty string
* if no name has been provided.
*
* This function has no side effects.
*
* @return the name or the empty string if none provided
*/
public String getName() {
return this.name == null ? "" : this.name;
}
/**
* Set the name of this person.
*
* @postcondition getName() returns name
*
* @param name the name to set. If null, getName() will still return the empty string
*/
public void setName(String name) {
this.name = name;
}
/**
* A Person's age is calculated as the getDeathdate() - getBirthdate().
* If either getDeathdate or getBirthdate returns null, this function returns -1.
*
* @precondition deathdate and birthdate are not null
* @precondition deathdate is later than birthdate
*
* @return the calculated age or -1 if the pre-conditions are not met
*/
public long getAge() {
if (this.getDeathdate() == null || this.getBirthdate() == null) {
return -1;
}
if (this.getDeathdate().getTime() < this.getBirthdate().getTime()) {
return -1;
}
return (this.getDeathdate().getTime() - this.getBirthdate().getTime()) / 1000 / 60 / 60 / 24 / 365;
}
/**
* Gets the birth date of this Person. This may return null if no birth date
* has been set. Due to the pre-conditions of setBirthdate, a birth date cannot
* be set after the current time.
*
* @return the birth date
*/
public Date getBirthdate() {
return birthdate;
}
/**
* Sets the birth date.
*
* @precondition birthdate must not be null
* @precondition birthdate must not be later than the current date.
* @precondition birthdate must not be later than the current death date
* the only exception is if the current death date is null
* @precondition if mother is not null and father's death date is not null,
* birthdate must not be later than the death date of mother
* @precondition if father is not null and father's death date is not null
* birthdate must not be later than the 9 months after the death date of father
*
* If any of the pre-conditions is not met, this function throws an
* IllegalArgumentException
*
* @param birthdate the birth date to set
* @throws IllegalArgumentException if a pre-condition is not met
*/
public void setBirthdate(Date birthdate) throws IllegalArgumentException {
if (birthdate == null || (new Date()).getTime() < birthdate.getTime()) {
throw new IllegalArgumentException();
}
if (this.getDeathdate() != null && this.getDeathdate().getTime() < birthdate.getTime()) {
throw new IllegalArgumentException();
}
if (this.getFather() != null && this.getFather().getDeathdate() != null && birthdate.getTime() < this.getFather().getDeathdate().getTime()) {
throw new IllegalArgumentException();
}
if (this.getMother() != null && this.getMother().getDeathdate() != null && birthdate.getTime() < this.getMother().getDeathdate().getTime()) {
throw new IllegalArgumentException();
}
this.birthdate = birthdate;
}
/**
* Gets the birth date of this Person. This may return null if no birth date
* has been set. Due to the pre-conditions of setBirthdate, a death date cannot
* be set after the current time.
*
* @return the death date
*/
public Date getDeathdate() {
return deathdate;
}
/**
* Sets the death date.
*
* @precondition deathdate must not be null
* @precondition deathdate must not be later than the current date.
* @precondition deathdate must not be earlier than the current birth date
* the only exception is if the current birth date is null
* @precondition if birth date is null, death date must not be earlier,
* deathdate cannot be earlier than the birth date for either
* mother or father (if they exist and have a birth date)
*
* If any of the pre-conditions is not met, this function throws an
* IllegalArgumentException
*
* @param birthdate the birth date to set
* @throws IllegalArgumentException if a pre-condition is not met
*/
public void setDeathdate(Date deathdate) {
if (deathdate == null || (new Date()).getTime() < deathdate.getTime()) {
throw new IllegalArgumentException();
}
if (this.getBirthdate() != null) {
if (deathdate.getTime() < this.getBirthdate().getTime()) {
throw new IllegalArgumentException();
}
} else {
if (this.getFather() != null && this.getFather().getBirthdate() != null
&& birthdate.getTime() < this.getFather().getBirthdate().getTime()) {
throw new IllegalArgumentException();
}
if (this.getMother() != null && this.getMother().getBirthdate() != null
&& birthdate.getTime() < this.getMother().getBirthdate().getTime()) {
throw new IllegalArgumentException();
}
}
this.deathdate = deathdate;
}
/**
* Gets the father of this person. This may return null if no father has
* been specified.
*
* @return the father
*/
public Person getFather() {
return father;
}
/**
* Sets the father of this person. If father is non-null and father == mother,
* this throws an IllegalArgumentException.
*
* @precondition father must not be the same as mother
* @postcondition getFather() returns father
*
* @param father the father to set
* @throws IllegalArgumentException if father != null && father == mother
*/
public void setFather(Person father) throws IllegalArgumentException {
if (father != null && father == this.mother) {
throw new IllegalArgumentException();
}
this.father = father;
}
/**
* Gets the mother of this person. This may return null if no mother has
* been specified.
*
* @return the mother
*/
public Person getMother() {
return mother;
}
/**
* Sets the mother of this person. If mother is non-null and mother == father,
* this throws an IllegalArgumentException.
*
* @precondition mother must not be the same as father
* @postcondition getMother() returns mother
*
* @param father the father to set
* @throws IllegalArgumentException if mother != null && mother == father
*/
public void setMother(Person mother) {
if (mother != null && mother == this.father) {
throw new IllegalArgumentException();
}
this.mother = mother;
}
/**
* Calculates and returns a Pedigree. A Pedigree is formed by adding one's self
* to a Pedigree and creating a Pedigree for each parent (if non-null).
*
* @precondition no cycles (can't be your own grandparent)
*
* Cycles are not explicitly detected, so getting a pedigree of a cyclic
* structure can lead to stack overflow. This situation is not likely to
* happen given the preconditions of the setFather() and setMother() functions.
*
* @return the pedigree
*/
public Pedigree getPedigree() {
return new Pedigree(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment