Вроде проставил детям null, но их на печать не выводит
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
ArrayList<Human> parent1 = new ArrayList<>();
ArrayList<Human> parent2 = new ArrayList<>();
ArrayList<Human> kids = new ArrayList<>();
Human ded1 = new Human("Makar", true, 80, parent1);
Human ded2 = new Human("Pahom", true, 76, parent2);
Human baba1 = new Human("Faina", false, 78, parent1);
Human baba2 = new Human("Frosya", false, 75, parent2);
Human otec = new Human("Oleg", true, 44, kids);
Human mat = new Human("Olga", false, 40, kids);
parent1.add(otec);
parent2.add(mat);
Human sin = new Human("Ivan", true, 20, null);
Human doch1 = new Human("Shluha", false, 17, null);
Human doch2 = new Human("Prostitute", false, 19, null);
kids.add(sin);
kids.add(doch1);
kids.add(doch2);
System.out.println(ded1.toString());
System.out.println(ded2.toString());
System.out.println(baba1.toString());
System.out.println(baba2.toString());
System.out.println(otec.toString());
System.out.println(mat.toString());
System.out.println(sin.toString());
System.out.println(doch1.toString());
System.out.println(doch2.toString());
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children;
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;
}
}
}