Здравствуйте, посмотрите пожалуйста что не так, не проходит проверку последнее условие. Хотя результат вроде выводится адекватный.
У нас есть 5 персонажей и если посмотреть на их родословную то условие по количеству "Дедов бабушек матерей отцов и детей" соблюдено. Инцеста между объектами тоже не допущено вроде как. В чем может быть проблема?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("СынаСын", true, 2);
Human son = new Human("Сын", true, 23, child1);
Human mather = new Human("Мать", false, 40, son);
Human father = new Human("Отец", true, 50, son);
Human grandmather = new Human("Бабушка", false, 80, father);
Human grandfather = new Human("Дед", true, 85, father);
System.out.println(grandfather.toString());
System.out.println(grandmather.toString());
System.out.println(father.toString());
System.out.println(mather.toString());
System.out.println(son.toString());
System.out.println(child1.toString());
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
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, Human human){
this.name = name;
this.sex = sex;
this.age = age;
children.add(human);
}
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;
}
}
}