Write in Java. Write a family database program. Create a class to represent a Pe
ID: 3706162 • Letter: W
Question
Write in Java. Write a family database program. Create a class to represent a Person that stores the names of the person, as well as that person's mother, father and children (the children should be stored in an ArrayList).
Create a Driver class with a main method to test your class. Prompt the user to create a new person, view information stored about anyone they've previously entered, or exit the program. When they enter a new person, they have to enter the person's name, their mother's name, and their father's name (keep in mind that their parents' names could be blank Strings (""), indicating that you've reached the top of the family tree). Each new person should be stored in an ArrayList of Person objects. If the user chooses to view information about someone, the program should print out that person's name, their maternal line (their mother, their mother's mother, etc.), their paternal line, and the children they may have.
The menu should be displayed until the user chooses to exit the program.
Explanation / Answer
Person class:
import java.util.ArrayList;
//Person class start
public class Person
{
//data members of the class
String personName,montherName,fatherName;
int childCount;
ArrayList child;
//default constructor
public Person() {}
//Parameterized constructor
public Person(String personName, String montherName, String fatherName, int childCount, ArrayList child)
{
super();
this.personName = personName;
this.montherName = montherName;
this.fatherName = fatherName;
this.childCount = childCount;
this.child = child;
}
//toString() method to display the object into readable format
public String toString()
{
return "Person [personName=" + personName + ", Mother's Name=" + montherName + ", Father's Name=" + fatherName
+ ",Number of children=" + childCount + ", Children=" + child + "]";
}
}//end of Person class
Test class:
import java.util.*;
//Driver class with a main method to test Person class.
public class Driver
{
static ArrayList<Person> personList= new ArrayList<Person>();
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int choice=0;
while(true)
{
/*Prompt the user to create a new person, view information stored
about anyone they've previously entered, or exit the program. */
System.out.println("1.Create new person");
System.out.println("2.View infromation");
System.out.println("0.Exit");
choice=in.nextInt();
//array list to hold children names
ArrayList<String> child=new ArrayList<String>();
//switch case based on the user choice
switch(choice)
{
//case 1 asks the person information
case 1: System.out.print("Enter person's name:");
String personName=in.next();
System.out.print("Enter "+personName+"'s mother name:");
String montherName=in.next();
System.out.print("Enter "+personName+"'s father name:");
String fatherName= in.next();
System.out.print("Enter number of children for "+personName+":");
int numOfChild=in.nextInt();
//loop to iterate number of children times
for(int i=1;i<=numOfChild;i++)
{
//asking children names
System.out.println("Enter name of child "+i);
String childName=in.next();
//adding children to array list
child.add(childName);
}
//creating person object with data
Person person = new Person(personName,montherName,fatherName,numOfChild,child);
//adding person object to ArrayList
personList.add(person);
//break the case 1
break;
//case 2 to print the person information
case 2: System.out.println(personList.toString());break;
//case 3 to exit the program
case 0: System.out.println("Bye.Have a nice day.");
System.exit(0);
}
}
}
}
Output:
1.Create new person
2.View infromation
0.Exit
1
Enter person's name:Rahul
Enter Rahul's mother name:Julie
Enter Rahul's father name:Colin
Enter number of children for Rahul:2
Enter name of child 1
Suhas
Enter name of child 2
Lima
1.Create new person
2.View infromation
0.Exit
2
[Person [personName=Rahul, Mother's Name=Julie, Father's Name=Colin,Number of children=2, Children=[Suhas, Lima]]]
1.Create new person
2.View infromation
0.Exit
0
Bye.Have a nice day.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.