package com.javarush.task.task08.task0824; /* Собираем семейство */ import java.util.ArrayList; import java.util.Arrays; public class Solution { public static void main(String[] args) { ArrayList<Human> children = new ArrayList<>(); Human children1 = new Human("Григорий", true, 27); Human children2 = new Human("Ядвига", false, 23); Human children3 = new Human("Клеопатра", false, 19); children.add(children1); children.add(children2); children.add(children3); Human father = new Human("Василий", true, 45, children); Human mother = new Human("Галина", false, 43, children); ArrayList<Human> childrenofgrand1 = new ArrayList<>(); ArrayList<Human> childrenofgrand2 = new ArrayList<>(); childrenofgrand1.add(father); childrenofgrand2.add(mother); Human grandfather1 = new Human("Дмитрий", true, 69, childrenofgrand1); Human grandmother1 = new Human("Мария", false, 63, childrenofgrand1); Human grandfather2 = new Human("Алексей", true, 63, childrenofgrand2); Human grandmother2 = new Human("Инна", false, 59, childrenofgrand2); System.out.println(grandfather1); System.out.println(grandfather2); System.out.println(grandmother1); System.out.println(grandmother2); System.out.println(father); System.out.println(mother); System.out.println(children1); System.out.println(children2); System.out.println(children3); } 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; } public Human(String name, boolean sex, int age){ this.name = name; this.sex = sex; this.age = age; } 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; } } }