/**
* 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;
field(s) for children ...
Gender(String label) {
this.label = label;/**
* @return the number of children of this Person
}
@Override */
public Stringint toStringgetChildCount() {
return label;...
}
public/**
static Gender
* @param child
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 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.
* @returnIf agender pre-existing Gender instance
*/is null, all children are returned.
... Gender* valueOf(String label) {
...Can be used to get all daughters or sons of a person.
}
}
public class Person implements Iterable<Person> {
* @param gender
... fields for name, gender, mother and father ...
... constructor */
public Collection<Person> getChildren(Gender gender) {
...
... methods for name, gender, mother and father ...
... field(s) for children ...
/**}
/**
* Adds the provided Person as a child of this Person.
* Also @returnsets the number of children ofchild's father or mother to this Person,
*/
depending public int getChildCount() {
...
}
/**
* @param childon this Person's gender.
* To ensure consistency, if the provided Person already
* has a parent of that gender,
* @return if this Person has the provided Person as ait is removed as a child of that parent.
* @param child
*/
public booleanvoid hasChildaddChild(Person child) {
...
}
}
/**
* Returns all children of this Person with the provided Gender.
public class PersonTest extends TestCase {
public void testAddChild() {
* IfGender genderfemale is null= Gender.valueOf("female"), allmale children are returned.
= Gender.valueOf("male");
* CanPerson bemother 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) {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);
...
}
}
public class PersonTest extends TestCase {
public void testAddChild() {
father2.addChild(child);
// ???
child.addChild(father2);
Gender female = Gender.valueOf("female"), male = Gender.valueOf("male");// ???
}
}
public class Family {
/**
* Adds a Person motheras =a new Person("Chris"); mother.setGender(female);
family member
* @param person the Person father1 = new Person("Pat"); father1.setGender(male); to add
*/
public void addMember(Person person) {
...
Person father2 =}
new Person("Alex"); father2.setGender(male); /**
* Finds a Personmember childwith =the new Person("Jean");given name
* mother.addChild(child);@param name
* @return the assertEquals(1, mother.getChildCount());
assertTrue(mother.hasChild(child));
assertEquals(mother, child.getMother());Person in this Family with the provided name
*/
public Person mother.addChild(child);findMember(String name) {
assertEquals(1, mother.getChildCount());
...
}
father1.addChild(child);
assertTrue(father1.hasChild(child));//
/**
* Writes assertEquals(father1, child.getFather());
father2.addChild(child);the contents of this Family to the OutputStream,
assertFalse(father1.hasChild(child));
assertTrue(father2.hasChild(child));* so it can be reconstructed using load.
* assertEquals(father2, child.getFather());
@param out
father2.setGender(female);
*/
public void save(OutputStream out) father2.addChild(child);... {
// ???...
}
child.addChild(father2);/**
* Helper // ???
}
}
public class Family {
/**method that splits a line into a list of tokens,
* Addseither awords Personor asquoted anames new(quotes familyare memberremoved).
* @param line person- the Personstring to addtokenize
*/
public... voidList<String> addMembertokenize(PersonString personline) {
... no need to implement this method ...
}
/**
* FindsLoads acontents member withfrom the givenprovided name
InputStream into * @param namethis Family.
* @return@param the Person in this Family with the provided name
*/
public Personvoid findMember(String name)load(InputStream in) ... {
...
}
}
public interface Relation //
{
/**
* WritesReturns the contentsCollection of thisPersons Familyrelated to the provided OutputStream,Person
* so it can be reconstructed using load.
* @param out
*/
public void save(OutputStream out) ... {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);
}
/**
* 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) {
...
}
}
public class Relation2 implements Relation {
public Relation2(Relation rel1, Relation rel2) {
...
}
@Override
public Collection<Person> getRelativesOf(Person person) {
...
}
}
|