com.javarush.task.task08.task0824.Solution
Имя: Toma, пол: женский, возраст: 72, Luda
Имя: Valera, пол: мужской, возраст: 75, Luda
Имя: Nil, пол: мужской, возраст: 89, Igor
Имя: Rima, пол: женский, возраст: 85, Igor
Имя: Igor, пол: мужской, возраст: 49, Archi, Nika, Nastja
Имя: Luda, пол: женский, возраст: 44, Archi, Nika, Nastja
Имя: Archi, пол: мужской, возраст: 5
Имя: Nika, пол: женский, возраст: 19
Имя: Nastja, пол: женский, возраст: 24
Process finished with exit code 0
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<>();
children.add(new Human("Archi",true,5));
children.add(new Human("Nika",false,19));
children.add(new Human("Nastja",false,24));
ArrayList<Human> father = new ArrayList<>();
father.add(new Human("Igor",true,49, children));
ArrayList<Human> mother = new ArrayList<>();
mother.add(new Human("Luda",false,44, children));
ArrayList<Human> gparents = new ArrayList<Human>();
gparents.add(new Human("Toma",false,72,mother));
gparents.add(new Human("Valera",true,75,mother));
gparents.add(new Human("Nil",true,89,father));
gparents.add(new Human("Rima", false,85,father));
for (int a = 0; a<gparents.size();a++){
System.out.println(gparents.get(a).toString());
}
for (int a = 0; a<father.size();a++){
System.out.println(father.get(a).toString());
}
for (int a = 0; a<mother.size();a++){
System.out.println(mother.get(a).toString());
}
for (int a = 0; a<children.size();a++){
System.out.println(children.get(a).toString());
}
//напишите тут ваш код
}
public static class Human {
public String name;
public boolean sex;
public int age;
public ArrayList<Human> children = new ArrayList<>();
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;
if(this.children!=null){
for (Human child : children) {
text += ", " + child.name;
}}
return text;
}
}
}