Частично угнал архитектуру отсюда
Для меня просто как то уж слишком запутанный метод Ту Стринг. Не совсем понимаю почему крашится
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children = new ArrayList<Human>();
Human grandFa1 = new Human("Дед1",true,60);
Human grandFa2 = new Human("Ded2",true,62);
Human grandMa1 = new Human("Babka1",false,55);
Human grandMa2 = new Human("Babka2",false,56);
Human father = new Human("Batya",true,35);
Human mother = new Human("Mamka",false,30);
Human child1 = new Human("child1",true,5);
Human child2 = new Human("Child2",true,7);
Human child3 = new Human("child 3",true,15);
children.add(father);
children.add(mother);
children.add(child1);
children.add(child2);
children.add(child3);
System.out.println(grandFa1.toString());
System.out.println(grandFa2.toString());
System.out.println(grandMa2.toString());
System.out.println(grandMa1.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(child1.toString());
System.out.println(child2.toString());
System.out.println(child3.toString());
}
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 = 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;
}
}
}