Валидатор чудит опять...
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> chirdrenNull = new ArrayList<>();
Human children1 = new Human("Ваня", true, 10, chirdrenNull);
Human children2 = new Human("Катя", false, 12, chirdrenNull);
Human children3 = new Human("Антон", true, 5, chirdrenNull);
ArrayList<Human> childrens1 = new ArrayList<>();
Collections.addAll(childrens1, children1, children2, children3);
ArrayList<Human> childrens2 = new ArrayList<>();
ArrayList<Human> childrens3 = new ArrayList<>();
Human father = new Human("Сергей", true, 35, childrens1);
Human mother = new Human("Светлана", false, 30, childrens1);
Collections.addAll(childrens2, father);
Collections.addAll(childrens3, mother);
Human ded1 = new Human("Акакий", true, 70, childrens2);
Human babka2 = new Human("Зоя", false, 65, childrens2);
Human ded2 = new Human("Афанасий", true, 71, childrens3);
Human babka1 = new Human("Зина", false, 65, childrens3);
ArrayList<Human> people = new ArrayList<>();
Collections.addAll(people, ded1, ded2 ,babka1, babka2, father, mother, children1, children2, children3);
for (int i = 0; i < people.size(); i++) {
System.out.println(people.get(i));
}
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human>children;
public Human(String name, boolean sex, int age, ArrayList<Human> children) {
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
System.out.println(this.toString());
}
public Human(String name, boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
children = new ArrayList<>();
}
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;
}
}
}