вроде выводит как надо, но что-то не учел.
package com.javarush.task.task08.task0824;
import java.util.*;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
ArrayList<Human> humans = new ArrayList<>();
ArrayList<Human> grandparents = new ArrayList<>();
ArrayList<Human> parents1 = new ArrayList<>();
ArrayList<Human> parents2 = new ArrayList<>();
ArrayList<Human> child = new ArrayList<>();
child.add(new Human("children1", 3, false, new ArrayList<Human>()));
child.add(new Human("children2", 3, false, new ArrayList<Human>()));
child.add(new Human("children3", 3, false, new ArrayList<Human>()));
parents1.add(new Human("father", 43, true, child));
parents2.add(new Human("mother", 43, false, child));
grandparents.add(new Human("grandfather", 63, true, parents1));
grandparents.add(new Human("grandfather", 63, true, parents2));
grandparents.add(new Human("grandmother", 63, false, parents1));
grandparents.add(new Human("grandmother", 63, false, parents2));
humans.addAll(grandparents);
humans.addAll(parents1);
humans.addAll(parents2);
humans.addAll(child);
System.out.println(humans);
}
public static class Human {
//напишите тут ваш код
String name;
int age;
boolean sex;
ArrayList<Human> children;
public Human(String name, int age, boolean sex, ArrayList<Human> child){
this.name = name;
this.age = age;
this.sex = sex;
children = new ArrayList<>(child);
}
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;
}
}
}