Skip to content

Instantly share code, notes, and snippets.

@ProZhar
Created December 3, 2015 09:15
Show Gist options
  • Save ProZhar/cd3fb35cef7ea557b647 to your computer and use it in GitHub Desktop.
Save ProZhar/cd3fb35cef7ea557b647 to your computer and use it in GitHub Desktop.
com.javarush.test.level08.lesson11.home06
package com.javarush.test.level08.lesson11.home06;
/* Вся семья в сборе
1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
3. Вывести все объекты Human на экран.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Solution
{
public static void main(String[] args)
{
//напишите тут ваш код
Map<String, Human> family = new HashMap<>();
family.put("Ребенок3", new Human("Ребенок3", true, 3, null));
family.put("Ребенок2", new Human("Ребенок2", true, 2, null));
family.put("Ребенок1", new Human("Ребенок1", false, 1, null));
family.put("Мать", new Human("Мать", false, 22, new Human[]{family.get("Ребенок1"), family.get("Ребенок2"), family.get("Ребенок3")}));
family.put("Отец", new Human("Отец", true, 33, new Human[]{family.get("Ребенок1"), family.get("Ребенок2"), family.get("Ребенок3")}));
family.put("Бабка2", new Human("Бабка1", false, 55, new Human[]{family.get("Мать")}));
family.put("Бабка1", new Human("Бабка2", false, 66, new Human[]{family.get("Отец")}));
family.put("Дед2", new Human("Дед2", true, 88, new Human[]{family.get("Мать")}));
family.put("Дед1", new Human("Дед1", true, 99, new Human[]{family.get("Отец")}));
for (Map.Entry<String, Human> entry : family.entrySet())
{
System.out.println(entry);
}
}
public static class Human
{
//напишите тут ваш код
private String name;
private boolean sex;
private int age;
private ArrayList<Human> children = new ArrayList<>();
public Human(String name, boolean sex, int age, Human[] args)
{
this.name = name;
this.sex = sex;
this.age = age;
if (args != null)
{
Collections.addAll(this.children, args);
}
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment