Не проходит последние условие. Но виводит на екран все правильно.
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList <Human > nochildren = new ArrayList <>();
ArrayList <Human> children = new ArrayList <Human>();
children.add ( new Human ("Andrew", 23, true, nochildren));
children.add ( new Human ("Kris", 25, false, nochildren));
children.add (new Human ("Ivan", 16, true, nochildren));
ArrayList <Human > father = new ArrayList <>();
father.add(new Human("father ", 50, true, children));
ArrayList <Human > mother = new ArrayList <>();
mother.add(new Human("mother ", 50, false, children));
ArrayList <Human > grandfa1= new ArrayList <>();
grandfa1.add (new Human("grandfa1", 80, true, father));
ArrayList <Human > grandMa1= new ArrayList <>();
grandMa1.add(new Human ("grandma1", 77, false, father));
ArrayList <Human> grandFa2 = new ArrayList <>();
grandFa2.add(new Human ("grandFa2", 76, true, mother));
ArrayList <Human > grandMa2 = new ArrayList <>();
grandMa2.add(new Human ("grandma2", 75, false, mother));
System.out.println(grandMa2.toString());
System.out.println(grandFa2.toString());
System.out.println(grandMa1.toString());
System.out.println(grandfa1.toString());
System.out.println(mother.toString());
System.out.println(father.toString());
System.out.println(children.get(0).toString());
System.out.println(children.get(1).toString ());
System.out.println(children.get(2).toString());
}
public static class Human {
String name;
int age;
boolean sex;
ArrayList<Human> children;
public Human (String name, int age, boolean sex, ArrayList<Human>children){
this.name=name;
this.age = age;
this.sex=sex;
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;
}
}
}