Ну и описание задачки.. Не уверен, что понял верно....
Как Я понял: вроде выводит всё правильно, но не проходит.
Посмотрите у кого прошла валидация, пжл.
вывод в консоль:
Имя: Alex, пол: мужской, возраст: 20
Имя: Elena, пол: женский, возраст: 25
Имя: Ivan, пол: мужской, возраст: 29
Имя: Vasya, пол: мужской, возраст: 50, дети: Alex, Elena, Ivan
Имя: Tanya, пол: женский, возраст: 45, дети: Alex, Elena, Ivan
Имя: Kate, пол: мужской, возраст: 85, дети: Vasya, Tanya
Имя: Varvara, пол: мужской, возраст: 80, дети: Vasya, Tanya
Имя: Yakov, пол: мужской, возраст: 87, дети: Vasya, Tanya
Имя: Petr, пол: мужской, возраст: 81, дети: Vasya, Tanya
Process finished with exit code 0
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("Alex", true, 20);
Human child2 = new Human("Elena", false, 25);
Human child3 = new Human("Ivan", true, 29);
ArrayList<Human> children1 = new ArrayList<>();
children1.add(child1);
children1.add(child2);
children1.add(child3);
ArrayList<Human> children2 = new ArrayList<>();
Human father1 = new Human("Vasya", true, 50, children1);
Human mother1 = new Human("Tanya", false, 45, children1);
children2.add(father1);
children2.add(mother1);
Human grandFather1 = new Human("Yakov", true, 87, children2);
Human grandFather2 = new Human("Petr", true, 81, children2);
Human grandMother1 = new Human("Kate", true, 85, children2);
Human grandMother2 = new Human("Varvara", true, 80, children2);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
System.out.println(father1);
System.out.println(mother1);
System.out.println(grandMother1);
System.out.println(grandMother2);
System.out.println(grandFather1);
System.out.println(grandFather2);
}
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;
this.children = new ArrayList<>();
}
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;
}
}
}