package com.javarush.task.task08.task0824; import java.util.ArrayList; import java.util.List; import java.util.Collections; /* Собираем семейство */ public class Solution { public static void main(String[] args) { Human c1 = new Human("c1", true, 5); Human c2 = new Human("c2", false, 15); Human c3 = new Human("c3", true, 25); Human m = new Human("m1", false, 35); Human f = new Human("f1", true, 45); Human d1 = new Human("d1", true, 55); Human g1 = new Human("g1", false, 65); Human d2 = new Human("d2", true, 75); Human g2 = new Human("g2", false, 85); Collections.addAll(f.children, c1, c2, c3); Collections.addAll(m.children, c1, c2, c3); d1.children.add(m); g1.children.add(m); g2.children.add(f); d2.children.add(f); c1.toString(); c2.toString(); c3.toString(); m.toString(); f.toString(); g1.toString(); d1.toString(); g2.toString(); d2.toString(); } public static class Human { public String name; public boolean sex; public int age; public ArrayList<Human> children = new ArrayList<Human>(); 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; } } }