By my logic this should print out all the family. I've read a few of the other comments and I suppose there is nothing wrong in my code. But validation says otherwise
package com.codegym.task.task08.task0824;
/*
Make a family
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
ArrayList<Human> childrens = new ArrayList<>();
Human child1 = new Human("Walter", 21, true, childrens);
Human child2 = new Human("Paula", 14, false, childrens);
Human child3 = new Human("Jose", 19, true, childrens);
ArrayList<Human> children = new ArrayList<>();
children.add(child1);
children.add(child2);
children.add(child3);
Human mother = new Human("Andrea", 47, false, children);
Human father = new Human("Pedro", 50, true, children);
ArrayList<Human> maternal = new ArrayList<>();
maternal.add(mother);
Human granma = new Human("Gloria", 69, false, maternal);
Human granpa = new Human("Mario", 73, true, maternal);
ArrayList<Human> paternal = new ArrayList<>();
paternal.add(father);
Human grandPa1 = new Human("Salomon", 76, true, paternal);
Human grandMa1 = new Human("Teresa", 70, false, paternal);
System.out.println("paternal Granfather is: " + grandPa1.toString());
System.out.println("paternal Granmother is: " + grandMa1.toString());
System.out.println("Maternal Granfather is: " + granma.toString());
System.out.println("Maternal GranMother is: " + granpa.toString());
System.out.println("Father is: " + father);
System.out.println("Mother is : " + mother);
System.out.println("Children are: " + child1.toString());
System.out.println("Children are: " + child2.toString());
System.out.println("Children are: " + child3.toString());
}
public static class Human {
String name = "Unknown";
int age = 0;
boolean sex = false;
ArrayList<Human> children = new ArrayList<>();
public Human(){
}
public Human (String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Human(String name, int age, boolean sex, ArrayList<Human> children) {
this.name = name;
this.age = age;
this.sex = sex;
this.children = children;
}
public String toString() {
String text = "";
text += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}