Could please someone give me a hand? I am stuck with this task and cannot see the error in my code.
The for each loop correctly should in line 47-48 should be (just for some reason the system did not update the modification in my code):
for (Human humansToPrintOut : everybody) {
System.out.println(humansToPrintOut.toString());
Your help is appreciated, thank you!
package com.codegym.task.task08.task0824;
/*
Make a family
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
//write your code here
Human grandFather1 = new Human("grandFather1", true, 65);
Human grandFather2 = new Human("grandFather2", true, 70);
Human grandMother1 = new Human("grandMother1", false, 60);
Human grandMother2 = new Human("grandMother2", false, 65);
Human father = new Human("father", true, 35);
Human mother = new Human("mother", false, 33);
Human child1 = new Human("kid1", true, 7, new ArrayList<>());
Human child2 = new Human("kid2", false, 9, new ArrayList<>());
Human child3 = new Human("kid3", true, 5, new ArrayList<>());
ArrayList<Human> children = new ArrayList<>();
children.add(child1);
children.add(child2);
children.add(child3);
ArrayList<Human> grandFathers = new ArrayList<>();
grandFathers.add(grandFather1);
grandFathers.add(grandFather2);
ArrayList<Human> grandMothers = new ArrayList<>();
grandMothers.add(grandMother1);
grandMothers.add(grandMother2);
ArrayList<Human> everybody = new ArrayList<>();
children.addAll(everybody);
grandFathers.addAll(everybody);
grandMothers.addAll(everybody);
everybody.add(father);
everybody.add(mother);
for (Human humansToPrintOut : everybody) {
System.out.println(everybody);
}
}
public static class Human {
//write your code here
String name;
boolean sex;
int age;
ArrayList<Human>children;
public Human(String name, boolean sex, int age, ArrayList<Human>children){
name = this.name;
sex = this.sex;
age = this.age;
children = this.children;
}
public Human(String name, boolean sex, int age){
name = this.name;
sex = this.sex;
age = this.age;
}
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;
}
}
}