Name: Grandpa1, sex: male, age: 100, children: father, mother
Name: Grandpa2, sex: male, age: 99, children: father, mother
Name: Grandma1, sex: female, age: 99, children: father, mother
Name: Grandma2, sex: female, age: 98, children: father, mother
Name: father, sex: male, age: 50, children: firstChild, secondChild, thirdChild
Name: mother, sex: female, age: 47, children: firstChild, secondChild, thirdChild
Name: firstChild, sex: male, age: 18
Name: secondChild, sex: male, age: 19
Name: thirdChild, sex: female, age: 20
package com.codegym.task.task08.task0824;
import java.util.ArrayList;
/*
Make a family
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> human = new ArrayList<Human>();
Human stChild = new Human("firstChild",true,18,human);
Human ndChild = new Human("secondChild",true,19,human);
Human rdChild = new Human("thirdChild",false,20,human);
ArrayList<Human> children = new ArrayList<Human>();
children.add(stChild);
children.add(ndChild);
children.add(rdChild);
Human father = new Human("father",true,50,children);
Human mother = new Human("mother",false,47,children);
ArrayList<Human> parents =new ArrayList<Human>();
parents.add(father);
parents.add(mother);
Human Grandpa1 = new Human("Grandpa1",true,100,parents);
Human Grandpa2 = new Human("Grandpa2",true,99,parents);
Human Grandma1 = new Human("Grandma1",false,99,parents);
Human Grandma2 = new Human("Grandma2",false,98,parents);
ArrayList<Human> family =new ArrayList<Human>();
family.add(Grandpa1);
family.add(Grandpa2);
family.add(Grandma1);
family.add(Grandma2);
family.addAll(parents);
family.addAll(children);
for(Human person : family){
System.out.println(person.toString());
}
}
public static class Human {
//write your code here
String name;
boolean sex;
int age;
ArrayList<Human> children;
Human(String name, boolean sex, int age, ArrayList<Human> children){
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
}
public String toString() {
String text = "";
text += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}