Получается связана должна быть вся семья через метод toString()? Вообще не понимаю как сделать, обычный вывод пожст вывел
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
ArrayList<Human> children = new ArrayList<Human>();
ArrayList<Human> grandF = new ArrayList<>();
ArrayList<Human> grandM = new ArrayList<>();
ArrayList<Human> father = new ArrayList<>();
ArrayList<Human> mother = new ArrayList<>();
Human gF1 = new Human("GrandFather1",true,90);
Human gF2 = new Human("GrandFather2",true, 88);
grandF.add(gF1);
grandF.add(gF2);
Human gM1 = new Human("GrandMother1",false,88);
Human gM2 = new Human("GrandMother2",false,87);
grandM.add(gM1);
grandM.add(gM2);
Human Fr = new Human("Father",true,45);
father.add(Fr);
Human Mr = new Human("Mother",false, 45);
mother.add(Mr);
children.add(new Human("Kid1",true,11));
children.add(new Human("Kid2",false,12));
children.add(new Human("kid3",true,9));
System.out.println(Fr.toString());
System.out.println(Mr.toString());
for(int i=0;i<children.size();i++){
System.out.println(children.get(i));
}
for(int i=0;i<grandF.size();i++){
System.out.println(grandF.get(i));
}
for(int i=0;i<grandM.size();i++){
System.out.println(grandM.get(i));
}
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
public Human(String name,boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
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;
}
}
}