Помогите пожалуйста разобраться
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/*
Собираем семейство
1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
3. Выведи все объекты Human на экран (Подсказка: используй метод toString() класса Human).
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children_is_null = new ArrayList<>();
Human child1 = new Human("Макар", true, 5, children_is_null);
Human child2 = new Human("Люба", false, 3, children_is_null);
Human child3 = new Human("Никита", true, 15, children_is_null);
ArrayList<Human> child = new ArrayList<>();
Collections.addAll(child, child1, child2, child3);
Human papa = new Human("Павел", true, 40, child);
Human mama = new Human("Ольга", false, 36, child);
ArrayList<Human> parent = new ArrayList<>();
Collections.addAll(parent, papa, mama);
Human grand_father = new Human("Николай", true, 75, parent);
Human grand_mother = new Human("Мария", false, 67, parent);
ArrayList<Human> all_obj = new ArrayList<>();
Collections.addAll(all_obj, child1,child2,child3,papa,mama,grand_father,grand_mother);
for (Human human : all_obj) {
System.out.println(human);
}
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children;
//напишите тут ваш код
public Human(String name, boolean sex, int age, ArrayList<Human> children){
this.name = name;
this.sex = sex;
this.age = age;
if (!(children == null)){
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;
}
}
}