среда, 25 января 2017 г.

level08.lesson11.home06

package com.javarush.test.level08.lesson11.home06;

/* Вся семья в сборе1. Создай класс Human с полями имя (String), пол (boolean), 
возраст (int), дети (ArrayList).2. Создай объекты и заполни их так, чтобы получилось: 
два дедушки, две бабушки, отец, мать, трое детей.3. Вывести все объекты Human на экран.*/
import java.util.ArrayList;
import java.util.Arrays;

public class Solution
{
    public static void main(String[] args)
    {
        //напишите тут ваш код        Human children1 = new Human("Galya", false, 18,null);
        Human children2 = new Human("Gala", false, 14,null);
        Human children3 = new Human("Ivan", true, 14,null);
Human father = new Human("Ivan", true, 40,new ArrayList(Arrays.asList(children1,children2,children3)));
Human mother = new Human("Nastya", false, 38,new ArrayList(Arrays.asList(children1,children2,children3)));
Human grandfather1 = new Human("Vasya", true, 60,new ArrayList(Arrays.asList(father)));
Human grandfather2 = new Human("Petro", true, 62,new ArrayList(Arrays.asList(father)));
Human grandmother1 = new Human("Olya", false, 55,new ArrayList(Arrays.asList(mother)));
Human grandmother2 = new Human("Masha", false, 57,new ArrayList(Arrays.asList(mother)));
        System.out.println(children1);
        System.out.println(children2);
        System.out.println(children3);
        System.out.println(father);
        System.out.println(mother);
        System.out.println(grandfather1);
        System.out.println(grandfather2);
        System.out.println(grandmother1);
        System.out.println(grandmother2);


    }

    public static class Human
    {
        //напишите тут ваш код        String name;
        Boolean sex;
        Integer age;
        ArrayList children;
        public Human (String name, Boolean sex, Integer age)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;

        }
public Human (String name, Boolean sex, Integer age, ArrayList 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 =0;
            if (this.children != null)
            {
                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;
        }
    }

}

Комментариев нет:

Отправить комментарий