Простите, но ЧТО БЛ?!?!?!?!?!?
Вот вам:
- дед1
- дед 2
- бабка1
- бабка 2
- гребаный ГИТЛЕР с дед1+бабка1
- гребаная ЕВА с дед2+бабка2
- отпрысок1 от ГИТЛЕР+ЕВА
- отпрысок2 от ГИТЛЕР+ЕВА
- отпрысок3 от ГИТЛЕР+ЕВА
ИТОГО: 9 РАЗНЫХ объектов, из которых 4 без парентов, а остальные с ними.
все это написано минут за 15, и целых 45 я пытаюсь понять, что не так
package com.javarush.task.task07.task0724;
import java.util.*;
/*
Семейная перепись
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> list= new ArrayList<>();
Human ded1 = new Human("Дедуля", true, 77, null, null);
list.add(ded1);
Human ded2 = new Human("Максим", true, 81, null, null);
list.add(ded2);
Human babka1 = new Human("Бабуля", false, 67, null, null);
list.add(babka1);
Human babka2 = new Human("Пулеметчица", false, 88, null, null);
list.add(babka2);
Human hitler = new Human("Гитлер", true, 45, ded1, babka1);
list.add(hitler);
Human eva = new Human("Ева", false, 43, ded2, babka2);
list.add(eva);
Human child1 = new Human("Гнойный", true, 11, hitler, eva);
list.add(child1);
Human child2 = new Human("Оксимиронка", false, 13, hitler, eva);
list.add(child2);
Human child3 = new Human("Монеточка", false, 14, hitler, eva);
list.add(child3);
for(Human i: list) {
System.out.println(i);
}
}
public static class Human {
String name;
boolean sex;
int age;
Human father;
Human mother;
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, Human father, Human mother) {
this(name, sex, age);
this.father = father;
this.mother = mother;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
if (this.father != null)
text += ", отец: " + this.father.name;
if (this.mother != null)
text += ", мать: " + this.mother.name;
return text;
}
}
}