Write a family database program. Create a class to represent a person and to sto
ID: 3853786 • Letter: W
Question
Write a family database program. Create a class to represent a person and to store references to the person’s mother, Father, and any children that the person has. Read a file of names to initialize the name and the parent-child relationships of each person. (You might want to create a file representing your own family tree. Store the overall list of persons as an ArrayList. Write an overall driver class that asks for a name and prints the maternal and paternal family line of that person.
You need to create the following classes:
Person class: Shell has been provided
FamilyInfo class: shell has been provided
FamilyTest class: shell has been provided
You also need to create a file representing the family info such as the following:
List the name of all the people in your family and end the list with the String “End”
For every person in the step a, write the following
Person’s name
Person’s mother
Person’s father
You must create a file of the information about your own family. Here is a sample file
Explanation / Answer
Change the file path accordingly
FamilyTest.java
---------------------------------------------------
// This program tests the FamilyInfo and Person classes by reading a
// file with family information and showing the maternal line, paternal
// line and children for various people.
import java.io.*;
import java.util.*;
public class FamilyTest {
public static void main (String[] args) throws FileNotFoundException {
// Interact with user via the console
Scanner console = new Scanner(System.in);
// Show program introduction:
System.out.println("This program reads an input file with family");
System.out.println("information and provides information about the");
System.out.println("maternal line, paternal line and children of");
System.out.println("various people. ");
// Read given family information into FamilyInfo object
//Scanner input = new Scanner(new File("names.txt.txt"));
Scanner input = new Scanner(new File("C: esults ames.txt.txt"));
System.out.println("Input file is names.txt.txt in local project folder.");
FamilyInfo family = new FamilyInfo();
family.read(input); // BIG method, that reads data file into object
input.close(); // all data in file is read, so file is no longer needed
// Loop main program as long as user desires:
String name = "Keep running";
while(!name.equals("quit")) {
System.out.print(" Person's names.txt ('quit' to end)? ");
name = console.nextLine(); // read line from user
System.out.println();
if (name.equalsIgnoreCase("quit")) break; // break loop upon quit
Person next = family.getPerson(name);
if (next == null) {
System.out.println("No match.");
} else {
showMaternal(next);
showPaternal(next);
showChildren(next);
System.out.println(" Shown as TREE (printSideways):");
FamilyTree structure = new FamilyTree(next);
structure.buildTree(family);
structure.printSideways();
}
}
}
// Shows maternal ancestors for given person
public static void showMaternal(Person current) {
System.out.println("Maternal line:");
int level = 1;
while (current != null) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(current.getName());
current = current.getMother();
level++;
}
System.out.println();
}
// Shows paternal ancestors for given person
public static void showPaternal(Person current) {
System.out.println("Paternal line:");
int level = 1;
while (current != null) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(current.getName());
current = current.getFather();
level++;
}
System.out.println();
}
// Shows children for given person
public static void showChildren(Person current) {
System.out.println("Children:");
if (current.numKids() == 0) {
System.out.println(" none");
} else {
for (int i = 0; i < current.numKids(); i++) {
System.out.println(" " + current.nthKid(i).getName());
}
System.out.println();
}
}
}
-----------------------------------------------------------------------------------------------
FamilyInfo.java
------------------------------------------------
import java.util.*;
import java.io.*;
public class FamilyInfo {
private Person person;
private ArrayList<Person> persons;
public FamilyInfo(){
person = null;
persons = new ArrayList<Person>();
}
public void read (Scanner input) {
//int index = 0;
//input.useDelimiter("[^a-zA-Z']");
String name;
String personName, motherName, fatherName;
name = input.nextLine();
while (name.length() > 0 && !name.equals("END OF NAMES, CHILD/MOTHER/FATHER records follow:")) {
if(!found(name)){
persons.add(new Person(name));
}
name = input.nextLine();
}
name = input.nextLine();
while (name.length() > 0 && !name.equals("END OF FILE")) {
if (name.equals("-")) {
name = input.nextLine();
continue;
}
// Process each CHILD/MOTHER/FATHER records
if(!found(name)){
System.out.println("Not possible.Input wrong");
System.exit(1);
} else{
personName = name;
motherName = input.nextLine();
fatherName = input.nextLine();
processParents(personName, motherName, fatherName);
}
name = input.nextLine();
}
}
public Boolean found(String name) {
if (persons.isEmpty()) {
return false;
}
for(Person person:persons) {
if (person.getName().equals(name)) {
return true;
}
}
return false;
}
public Person getPerson(String name) {
for(Person person:persons) {
if (person.getName().equalsIgnoreCase(name)) {
return person;
}
}
return null;
}
private void processParents(String personName, String motherName, String fatherName) {
Person p = getPerson(personName);
if (!motherName.equals("unknown")) {
Person mother = getPerson(motherName);
if(mother == null){
System.out.println("Mother not found.Not possible.Input wrong");
System.exit(1);
}
p.setMother(mother);
mother.addChild(p);
}
if (!fatherName.equals("unknown")) {
Person father = getPerson(fatherName);
if(father == null){
System.out.println("Father not found.Not possible.Input wrong");
System.exit(1);
}
p.setFather(father);
father.addChild(p);
}
}
}
------------------------------------------------------------------------------------------
FamilyTree.java
------------------------------------------------
import java.util.*;
import java.io.*;
public class FamilyTree {
private Person rootPerson;
private FamilyInfo rootFamily;
public FamilyTree(Person rootPerson) {
this.rootPerson = rootPerson;
}
public void buildTree(FamilyInfo family) {
rootFamily = family;
if (rootPerson == null) {
System.out.println("No person with that names.txt");
System.exit(1);
/*
* No need to build any more family as all info
* is there in the rootNode of our tree rootPerson
*/
}
}
public void printSideways() {
Person current, mother, father;
if (rootPerson == null) {
System.out.println("No person in family with that names.txt");
return;
}
Queue<Person> q = new LinkedList<Person>();
q.add(rootPerson);
q.add(null);
while(!q.isEmpty()) {
current = q.remove();
if (current == null) {
System.out.println();
if (!q.isEmpty()) {
q.add(null);
}
} else {
System.out.print(":" + current.getName() + ": ");
mother = current.getMother();
father = current.getFather();
if (mother != null) q.add(mother);
if (father != null) q.add(father);
}
}
}
}
----------------------------------------------------------------------
Person.java
-----------------------------------------
import java.util.*;
public class Person {
// insert instance variables here
private String name;
private Person mother;
private Person father;
private ArrayList<Person> children;
/** Creates a new instance of Person */
public Person(String name)
{
this.name = name;
this.mother = null;
this.father = null;
this.children = null;
}
public void setMother(Person mother){
this.mother = mother;
}
public void setFather(Person father){
this.father = father;
}
public void addChild(Person child) {
if (children == null) {
children = new ArrayList<Person>();
}
children.add(child);
}
public ArrayList<Person> getChildren(){
return this.children;
}
public int numKids(){
if(children == null){
return 0;
}
return children.size();
}
public Person nthKid(int childNumber) {
if(children == null){
return null;
}
return children.get(childNumber);
}
public Person getMother(){
return this.mother;
}
public Person getFather(){
return this.father;
}
public String getName(){
return name;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.