// GrandPa1 + GrandMa1 --> father (Son, child №1)
grandPa1.children.add(father);
grandMa1.children.add(father);
// GrandPa1 + GrandMa1 --> father (Daughter, child №2)
grandPa2.children.add(mother);
grandMa2.children.add(mother);
// father + mother --> grandson (Son, child №3)
father.children.add(grandSon);
mother.children.add(grandSon);
System.out.println(grandPa1);
System.out.println(grandMa1);
System.out.println(grandPa2);
System.out.println(grandMa2);
System.out.println(father);
System.out.println(mother);
System.out.println(grandSon);
/*
Output:
Имя: Степан, пол: мужской, возраст: 75, дети: Степаныч
Имя: Степанида, пол: женский, возраст: 68, дети: Степаныч
Имя: Петр, пол: мужской, возраст: 50, дети: Петровна
Имя: Анна, пол: женский, возраст: 40, дети: Петровна
Имя: Степаныч, пол: мужской, возраст: 40, дети: Мелкий
Имя: Петровна, пол: женский, возраст: 28, дети: Мелкий
Имя: Мелкий, пол: мужской, возраст: 8
Итого:
1. Дед Степан женат на бабке Степаниде, имеют сына Степаныча (1й дед, 1ая бабка, 1й ребенок);
2. Дед Петр женат на бабке Анне, имеют дочь Петровна (2й дед, 2ая бабка, 2й ребенок);
3. Отец Степаныч женат на матери Петровне, имеют сына Мелкого (1й отец, 1ая мать, 3й ребенок, он же внук двух пожилых пар).
*/
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) {
Human grandPa1 = new Human("Степан", true, 75);
Human grandMa1 = new Human("Степанида", false, 68);
Human grandPa2 = new Human("Петр", true, 50);
Human grandMa2 = new Human("Анна", false, 40);
Human father = new Human("Степаныч", true, 40);
Human mother = new Human("Петровна", false, 28);
Human grandSon = new Human("Пиздюк", true, 8);
grandPa1.children.add(father);
grandMa1.children.add(father);
grandPa2.children.add(mother);
grandMa2.children.add(mother);
father.children.add(grandSon);
mother.children.add(grandSon);
System.out.println(grandPa1);
System.out.println(grandMa1);
System.out.println(grandPa2);
System.out.println(grandMa2);
System.out.println(father);
System.out.println(mother);
System.out.println(grandSon);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
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;
}
}
}