Genealogy Tree in Java
I have to make a Genealogy Tree in Java. I have created 3 classes so far. The Person and all people class are the ones where most of the code have to go. However I am not totally sure if I am going about this the right way. I was wondering if you could go give me some advice on the best way to tackle the problem. I have included below the code that I have done so far.
The questions I have to answer are as follows.....
1. Check whether a given person is a parent of another given person. Cf.
Section 2.
2. Given a person, collect the list of children of that person, which can of
course be empty.
There are two ways of doing this, with more marks allocated to the cleverer
one. Call the given person p.
Naive algorithm: Scan the list of all people, using a loop, considering the
next person c of the list at each iteration of the loop. If p is a parent of
c, add c to the list of children of p. Of course, you have to start with an
empty list of children, which after the loop will contain all the children. The
drawback of this algorithm is that if there are many people, for example
all known people alive, it will take a prohibitively long time to find all the
children of a given person.
8
*Cleverer algorithm: In the class Person, explicitly add the list of children,
and make sure this information is updated when people are added to the
system. This will be much faster, but it requires care to maintain the
information consistent. Notice that this is redundant information, that can
be reconstructed from the mother and father information.
3. Check whether a given person is a grandparent of another person.
4. Given two people, check whether they are siblings.
5. Check whether a given person is an aunt of another person.
6. Collect the list of all uncles of a given person.
7. Check whether two people are first cousins.
8. Collect the list of ancestors of a given person.
9. Collect the list of descendants of a given person.
10. *Given two people, find a closest common ancestor, or return null if it
doesn?t exist (among the people known to the system, of course).
11. *Given two people, find a closest common descendant, or return null if it
doesn?t exist (same remark here).
Code is below
package GenealogyTrees;
import GenealogyTrees.Gender;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
*arrayList
* @author msc85tjm
*/
public class Person {
/** Creates a new instance of People */
private String familyName;
private String givenName;
private String dateOfBirth;
private String placeOfBirth;
private String dateOfDeath;
private String placeOfDeath;
private Gender sex;
private static int UNIQUE_USER_ID = 1000;
private int uniqueUserID;
private Person father;
private Person mother;
private ArrayList<Person> children;
public Person(String fN, String gN, String dOB, String pOB, String dOD, String pOD, Gender s, int uUID) {
familyName = fN;
givenName = gN;
dateOfBirth = dOB;
placeOfBirth = pOB;
dateOfDeath = dOD;
placeOfDeath = pOD;
sex = s;
uniqueUserID = UNIQUE_USER_ID;
UNIQUE_USER_ID++;
children = new ArrayList<Person>();
}
public Person(BufferedReader in) throws IOException {
String person = in.readLine();
String[] p = person.split(",");
uniqueUserID = Integer.parseInt(p[0]);
setFamilyName(p[1]);
setGivenName(p[2]);
setDateOfBirth(p[3]);
setPlaceOfBirth(p[4]);
setDateOfDeath(p[5]);
setPlaceOfDeath(p[6]);
int gender = Integer.parseInt(p[7]);
if (gender == 0) {
setSex(Gender.male);
} else {
setSex(Gender.female);
}
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getGivenName() {
return givenName;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getPlaceOfBirth() {
return placeOfBirth;
}
public void setPlaceOfBirth(String placeOfBirth) {
this.placeOfBirth = placeOfBirth;
}
public String getDateOfDeath() {
return dateOfDeath;
}
public void setDateOfDeath(String dateOfDeath) {
this.dateOfDeath = dateOfDeath;
}
public String getPlaceOfDeath() {
return placeOfDeath;
}
public void setPlaceOfDeath(String placeofDeath) {
this.placeOfDeath = placeOfDeath;
}
public Gender getSex() {
return sex;
}
public void setSex(Gender sex) {
this.sex = sex;
}
public int getUniqueUserID() {
return uniqueUserID;
}
public void setUniqueUserID(int uniqueUserID) {
this.uniqueUserID = uniqueUserID;
}
private void setMother(Person mother) {
this.mother = mother;
}
private Person getMother() {
return mother;
}
private void setFather(Person father) {
this.father = father;
}
private Person getFather() {
return father;
}
//array of children for which a child is stored.
private ArrayList<Person> children() {
return children;
}
private boolean add(Person c) {
return children.add(c);
}
private boolean remove(Person c) {
return children.remove(c);
}
private boolean contains(Person c) {
return children.contains(c);
}
public static boolean isParent(Person p, Person c) {
if (c.getMother() == p)
{
return true;
}
else if (c.getFather() == p)
{
return true;
}
else{
return false;
}
}
public boolean isCousin() {
return true;
}
public boolean isGrandParent(Person p, Person c) {
return true;
}
public boolean isUncle() {
return true;
}
public boolean isAunt() {
return true;
}
public boolean isAncestors() {
return true;
}
public boolean isDecendants() {
return true;
}
//writes to the file the information in this order.
private void saveTO(PrintWriter out) {
out.print(getUniqueUserID() + "," + getFamilyName() + "," + getGivenName() + "," + getDateOfBirth() + "," + getPlaceOfBirth() + "\n" + getDateOfDeath() + "," +
getPlaceOfDeath() + "," + getSex());
}
//strings the information in this order in the file.
public String toString() {
return getUniqueUserID() + "," + getFamilyName() + "," + getGivenName() + "," + getDateOfBirth() +
"," + getPlaceOfBirth() + "\n" + getDateOfDeath() + "," + getPlaceOfDeath() + "," + getSex();
}
}
package GenealogyTrees;
import java.util.ArrayList;
/**
*
* @author msc85tjm
*/
public class AllPeople {
ArrayList AllPeople = new ArrayList();
/** Creates a new instance of allPeople */
// //add method - adds a chid to the arraylist and returns true if it's done.
// private boolean add(Person c)
// {
//
// return children.add();
//
// }
// //remove method - removes a child to the arraylist and returns true if it's done.
// private boolean remove(Person c)
//
// {
// return children.remove.();
// }
public boolean isParent()
{
return true;
}
public boolean isCousin()
{
return true;
}
public boolean isGrandParent()
{
return true;
}
public boolean isUncle()
{
return true;
}
public boolean isAunt()
{
return true;
}
public boolean isAncestors()
{
return true;
}
public boolean isDecendants ()
{
return true;
}
//create methods to answer questions (sort methods)
//save to method needed.
//work out a way to save the data in the file without it being deleted when computer is restarted.
//use Boolean and exceptions
}
package GenealogyTrees;
/**
*
* @author msc85tjm
*/
public enum Gender {
male, female;
}