Класс Solution должен содержать один метод - Solution содержит метод createHumans()
Если пишу все в main, тоже не проходит валидацию...
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
List<Human> humans = createHumans();
humans.forEach(h -> System.out.println(h));
}
public static List<Human> createHumans() {
List<Human> humans = new ArrayList<>();
Human child1 = new Human("Петр", true, 5);
humans.add(child1);
Human child2 = new Human("Федя", true, 7);
humans.add(child2);
Human child3 = new Human("Катя", false, 10);
humans.add(child3);
Human mother = new Human("Света", false, 30, Arrays.asList(new Human[] { child1, child2, child3 }));
humans.add(mother);
Human father = new Human("Иван", true, 35, Arrays.asList(new Human[] { child1, child2, child3 }));
humans.add(father);
Human grandFather1 = new Human("Петр", true, 75, Arrays.asList(new Human[] { mother }));
humans.add(grandFather1);
Human grandFather2 = new Human("Федя", true, 80, Arrays.asList(new Human[] { father }));
humans.add(grandFather2);
Human grandMother1 = new Human("Катя", false, 70, Arrays.asList(new Human[] { mother }));
humans.add(grandMother1);
Human grandMother2 = new Human("Оля", false, 75, Arrays.asList(new Human[] { father }));
humans.add(grandMother2);
return humans;
}
public static class Human {
public String name;
public boolean sex;
public int age;
public List<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, List<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;
}
}
}