Ребята, по традиции вопрос, что я делаю не так.
Иерархию семейки соблюдаю.
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
public class Solution {
public static ArrayList<Human> allHumans = new ArrayList<Human>();
public static void main(String[] args) {
//напишите тут ваш код
Human dedula1 = new Human("Ded1",true,95);
Human granny1 = new Human("Babula1",false,95);
Human dedula2 = new Human("dedka2",true,95);
Human granny2 = new Human("Babka2",false,95);
Human father = new Human("Batek",true,40);
Human mother = new Human("Mamka",false,41);
Human docha = new Human("Ekaterina",false,25);
Human sina1 = new Human("Gleb",true,20);
Human sina2 = new Human("Petka",true,20);
granny1.children.add(father);
dedula1.children = granny1.children;
granny2.children.add(mother);
dedula2.children = granny2.children;
father.children.add(docha);
father.children.add(sina1);
father.children.add(sina2);
mother.children = father.children;
for (Human h:Solution.allHumans
) {
System.out.println(h.toString());
}
}
public static class Human {
//напишите тут ваш код
public String name;
public boolean sex;
public int age;
public ArrayList<Human> children;
public Human(String name, boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
children = new ArrayList<Human>();
Solution.allHumans.add(this);
}
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;
}
}
}