Имя: ded1, пол: мужской, возраст: 78, дети: peter
Имя: babka1, пол: женский, возраст: 78, дети: peter
Имя: ded2, пол: мужской, возраст: 78, дети: lois
Имя: babka2, пол: женский, возраст: 78, дети: lois
Имя: lois, пол: женский, возраст: 40, дети: child1
Имя: peter, пол: мужской, возраст: 40, дети: child1
Имя: child1, пол: женский, возраст: 15
Имя: child2, пол: женский, возраст: 12
Имя: child3, пол: женский, возраст: 17
Вывод такой.
Валидацию не прохожу.
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("child1", false, 15);
Human child2 = new Human("child2", false, 12);
Human child3 = new Human("child3", false, 17);
ArrayList<Human> children1 = new ArrayList<Human>();
children1.add(child1);
children1.add(child2);
children1.add(child3);
Human father = new Human("peter", true, 40, children1);
ArrayList<Human> children2 = new ArrayList<Human>();
children2.add(father);
Human mother = new Human("lois", false, 40, children1);
ArrayList<Human> children3 = new ArrayList<Human>();
children3.add(mother);
Human ded1 = new Human("ded1", true, 78, children2);
Human babka1 = new Human("babka1", false, 78, children2);
Human ded2 = new Human("ded2", true, 78, children3);
Human babka2 = new Human("babka2", false, 78, children3);
System.out.println(ded1);
System.out.println(babka1);
System.out.println(ded2);
System.out.println(babka2);
System.out.println(mother);
System.out.println(father);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children;
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 = 0;
if (children != null) {
childCount = this.children.size();
}
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
}
return text;
}
}
}