Что не так?
вывод :
Имя: ded1, пол: мужской, возраст: 88, дети: mother
Имя: ded2, пол: мужской, возраст: 88, дети: father
Имя: babka1, пол: женский, возраст: 88, дети: mother
Имя: babka2, пол: женский, возраст: 88, дети: father
Имя: father, пол: мужской, возраст: 45, дети: son1, son2, son3
Имя: mother, пол: женский, возраст: 45, дети: son1, son3
Имя: son1, пол: мужской, возраст: 12
Имя: son2, пол: мужской, возраст: 23
Имя: son3, пол: мужской, возраст: 2
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Human son1 = new Human("son1", true, 12);
Human son2 = new Human("son2", true, 23);
Human son3 = new Human("son3", true, 2);
Human father = new Human("father", true, 45, son1, son2, son3);
Human mother = new Human("mother", false, 45, son1, son3);
Human ded1 = new Human("ded1", true, 88, mother);
Human ded2 = new Human("ded2", true, 88, father);
Human babka1 = new Human("babka1", false, 88, mother);
Human babka2 = new Human("babka2", false, 88, father);
ArrayList<Human> humans = new ArrayList<>(Arrays.asList(ded1, ded2, babka1, babka2, father, mother, son1, son2, son3));
for (Human s : humans)
System.out.println(s);
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children ;
Human(String name, boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
this. children = new ArrayList<>(0);
}
Human(String name, boolean sex, int age, Human ... children){
this.name = name;
this.sex = sex;
this.age = age;
this.children = new ArrayList<>(Arrays.asList(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;
}
}
}