/**
* This
class represents the gender of a Person.
* It
cannot be instantiated outside this class.
* It
provides all legal Gender values as static variables.
*/
public class Gender {
... String label;
... Gender(String label) {
this.label = label;
}
@Override
public String toString() {
return label;
}
public static Gender
MALE = new Gender("male"),
FEMALE = new Gender("female");
/**
* Returns a pre-existing Gender instance for the provided label, or
* null of there is no such instance.
* @param label
* @return a pre-existing
Gender instance
*/
... Gender valueOf(String label) {
...
}
}
public class Person implements Iterable<Person> {
...
fields for name, gender, mother and father ...
...
constructor ...
...
methods for name, gender, mother and father ...
...
field(s) for children ...
/**
* @return the number of children of this Person
*/
public int getChildCount() {
...
}
/**
* @param child
* @return if this Person has the provided Person as a child
*/
public boolean hasChild(Person child) {
...
}
/**
* Returns all children of this Person with the
provided Gender.
* If gender is null, all children are
returned.
* Can be used to get all daughters or sons of
a person.
* @param gender
*/
public Collection<Person> getChildren(Gender gender) {
...
}
/**
* Adds the provided Person as a child of this
Person.
* Also sets the child's father or mother to
this Person,
* depending on this Person's gender.
* To ensure consistency, if the provided
Person already
* has a parent of that gender,
* it is removed as a child of that parent.
* @param child
*/
public void addChild(Person child) {
...
}
}
public class PersonTest extends TestCase {
public void testAddChild()
{
Gender female = Gender.valueOf("female"),
male = Gender.valueOf("male");
Person mother = new Person("Chris"); mother.setGender(female);
Person father1 = new Person("Pat"); father1.setGender(male);
Person father2 = new Person("Alex"); father2.setGender(male);
Person child = new Person("Jean");
mother.addChild(child);
assertEquals(1, mother.getChildCount());
assertTrue(mother.hasChild(child));
assertEquals(mother, child.getMother());
mother.addChild(child);
assertEquals(1, mother.getChildCount());
father1.addChild(child);
assertTrue(father1.hasChild(child));
assertEquals(father1, child.getFather());
father2.addChild(child);
assertFalse(father1.hasChild(child));
assertTrue(father2.hasChild(child));
assertEquals(father2, child.getFather());
father2.setGender(female);
father2.addChild(child);
// ???
child.addChild(father2);
// ???
}
}
public class Family {
/**
* Adds a Person as a new family member
* @param person the Person to add
*/
public void addMember(Person person) {
...
}
/**
* Finds a member with the given name
* @param name
* @return the Person in this Family with the provided name
*/
public Person findMember(String name) {
...
}
//
/**
* Writes the contents of this Family to the
OutputStream,
* so it can be reconstructed using load.
* @param out
*/
public void save(OutputStream out) ... {
...
}
/**
* Helper method that splits a line into a list
of tokens,
* either words or quoted names (quotes are
removed).
* @param line - the string to tokenize
*/
... List<String> tokenize(String line) {
...
no need to implement this method ...
}
/**
* Loads contents from the provided InputStream
into this Family.
* @param in
*/
public void load(InputStream in) ... {
...
}
}
public interface Relation {
/*
* Returns the Collection of Persons related to
the provided Person
* according to this Relation.
* E.g. if this Relation corresponds to the
concept of niece,
* it should return all Persons that are nieces
of person.
*/
Collection<Person>
getRelativesOf(Person person);
}
public class Relation2 implements Relation {
public Relation2(Relation rel1, Relation rel2) {
...
}
@Override
public Collection<Person>
getRelativesOf(Person person) {
...
}
}
|