Я может условие не до конца понял?
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
Human son = new Human("сын", true, 1);
Human daughter = new Human("дочь1", false, 2);
Human daughter1 = new Human("дочь2", false, 3);
ArrayList<Human> list1 = new ArrayList<>();
list1.add(son);
list1.add(daughter);
list1.add(daughter1);
Human mother = new Human("мать", false, 22, list1);
Human father = new Human("отец", true, 23, list1);
ArrayList<Human> list2 = new ArrayList<>();
list2.add(mother);
list2.add(father);
Human grandmother1 = new Human("бабушка1", false, 52, list2);
Human grandmother2 = new Human("бабушка2", false, 52, list2);
Human grandfather1 = new Human("дедушка1", true, 53, list2);
Human grandfather2 = new Human("дедушка2", true, 53, list2);
System.out.println(son.toString());
System.out.println(daughter.toString());
System.out.println(daughter1.toString());
System.out.println(mother.toString());
System.out.println(father.toString());
System.out.println(grandfather1.toString());
System.out.println(grandfather2.toString());
System.out.println(grandmother1.toString());
System.out.println(grandmother2.toString());
}
public static class Human {
String name;
Boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
public Human(String name, Boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public 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 += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}