Почему то не выводит детей, в чем причина?
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> children = new ArrayList<Human>();
Human child1 = new Human("Gosha",true, 15, null);
Human child2 = new Human("Sasha",true, 18, null);
Human child3 = new Human("Glasha",true, 20, null);
children.add(child1);
children.add(child2);
children.add(child3);
ArrayList<Human> fathers = new ArrayList<Human>();
ArrayList<Human> mothers = new ArrayList<Human>();
Human father = new Human("Daddy", true, 45, children);
Human mother = new Human("Mammy", true, 39, children);
fathers.add(father);
mothers.add(mother);
Human grandfather1 = new Human("Matvey", true, 70, fathers);
Human grandfather2 = new Human("Hasan", true, 69, mothers);
Human grandmother1 = new Human("Marfa", true, 72, fathers);
Human grandmother2 = new Human("Maria", true, 55, mothers);
System.out.println(grandfather1.toString());
System.out.println(grandmother1.toString());
System.out.println(grandfather2.toString());
System.out.println(grandmother2.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(child1.toString());
System.out.println(child2.toString());
System.out.println(child3.toString());
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<Human>();
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;
}
}
}
