Но в целом интересно - как добавить 3 детей Father и Mother ?
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> family = new ArrayList<>();
Human child1 = new Human("Irina", false,10);
family.add(child1);
Human child2 = new Human("Karina", false, 6);
family.add(child2);
Human child3 = new Human("Miraslav", true, 3);
family.add(child3);
Human father = new Human("Anton", true, 27, child1);
family.add(father);
Human mother = new Human("Vika", false, 33, child2);
family.add(mother);
Human grandFather = new Human("Oleg", true, 56, mother);
family.add(grandFather);
Human grandFather2 = new Human("Aleksand", true,56,father);
family.add(grandFather2);
Human grandMother = new Human("Tatsyana", false, 49,father);
family.add(grandMother);
Human grandMother2 = new Human("Lida", false, 56, mother);
family.add(grandMother2);
Collections.reverse(family);
for(Human x : family){
System.out.println(x);
}
}
public static class Human {
//напишите тут ваш код
String name;
Boolean sex;
int age;
ArrayList <Human> children = new ArrayList<Human>();
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, Human human ) {
this.name = name;
this.sex = sex;
this.age = age;
children.add(human);
}
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;
}
}
}