Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Первый проект! #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FamilyTree</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
43 changes: 43 additions & 0 deletions src/ru/GeekBrains/Voronyuk00/FamilyTree/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.GeekBrains.Voronyuk00.FamilyTree;

public class App {

public static void main(String[] args) {
// TODO Auto-generated method stub
Human person1 = new Human("Анна", "Воронюк",1971,7,29,Gender.female);
Human person2 = new Human("Евгений","Воронюк",1967,5,9,Gender.male);
Human person3 = new Human("Алексей","Воронюк",2000,4,12,Gender.male);
Human person4 = new Human("Марина", "Воронюк",2000,10,5,Gender.female);
Human person5 = new Human("Галина","Агафонова", 1940,8,14,2017,9,11,Gender.female);

// Создание семейного дерева
FamilyTree familyList = new FamilyTree();
familyList.addPerson(person1);
familyList.addPerson(person2);
familyList.addPerson(person3);
familyList.addPerson(person4);
familyList.addPerson(person5);

person1.addChild(person3);
person1.addChild(person4);
person2.addChild(person3);
person2.addChild(person4);

person3.addFather(person1);
person3.addMother(person2);
person4.addFather(person1);
person4.addMother(person2);
person1.addMother(person5);

System.out.println(person5 + " Возраст " + person5.getAge());

person5.addChild(person1);
person5.showChildren();

familyList.searchPerson("Алексей","Воронюк");
familyList.searchPerson("Иван","Высоцкий");


}

}
40 changes: 40 additions & 0 deletions src/ru/GeekBrains/Voronyuk00/FamilyTree/FamilyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ru.GeekBrains.Voronyuk00.FamilyTree;

import java.util.ArrayList;
import java.util.List;
/**
* Класс для создания семейного дерева
*/
public class FamilyTree {

private List<Human> familyList;
/**
* Конструктор для создания семейного дерева
*/
public FamilyTree(){
familyList = new ArrayList<>();
}

public void addPerson(Human h) {
familyList.add(h);
}
/**
*
* @param name имя человека
* @param surname фамилия человека
* @return информация о человеке
*/
public void searchPerson(String name, String surname) {
boolean hasPerson = false;
for(Human h : familyList) {
if(h.getName().equalsIgnoreCase(name) && h.getSurname().equalsIgnoreCase(surname)) {
System.out.println(h);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Старайтесь писать методы, которые возвращают результат своей работы, а не выводят в консоль

hasPerson = true;
}
}
if(!hasPerson) {
System.out.print("Такого человека нет");
}
}
}

5 changes: 5 additions & 0 deletions src/ru/GeekBrains/Voronyuk00/FamilyTree/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.GeekBrains.Voronyuk00.FamilyTree;

public enum Gender {
male,female;
}
144 changes: 144 additions & 0 deletions src/ru/GeekBrains/Voronyuk00/FamilyTree/Human.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ru.GeekBrains.Voronyuk00.FamilyTree;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
/**
* Класс для создания человека
*/
public class Human {

private String name;
private String surname;
private LocalDate dob, dod;
Gender gender;
List<Human> children;
Human father, mother;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

/**
* Конструктор для создания человека
* @param name - имя человека
* @param surname - фамилия человека
* @param year - год рождения человека
* @param month - месяц рождения человека
* @param day - день рождения человека
* @param gender - пол человека
*/
Human(String name, String surname, int year, int month,int day,Gender gender){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public

this.name = name;
this.surname = surname;
this.dob = LocalDate.of(year, month, day);
this.gender = gender;
children = new ArrayList<>();
}
/**
* Конструктор для создания человека
* @param name - имя человека
* @param surname - фамилия человека
* @param year - год рождения человека
* @param month - месяц рождения человека
* @param day - день рождения человека
@param year2 - год смерти человека
* @param month2 - месяц смерти человека
* @param day2 - день смерти человека
* @param gender - пол человека
*/
Human(String name, String surname, int year, int month,int day,int year2, int month2, int day2,Gender gender){
this.name = name;
this.surname = surname;
this.dob = LocalDate.of(year, month, day);
this.dod = LocalDate.of(year2, month2, day2);
this.gender = gender;
children = new ArrayList<>();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нарушение принципа DRY


/**
* Метод для получения имени
* @return имя
*/
public String getName() {
return this.name;
}
/**
* Метод для получения фамилии
* @return фамилия
*/
public String getSurname() {
return this.surname;
}

/**
* Добавление матери человека
* @param h - мать человека
*/
public void addMother(Human h) {
this.mother = h;
}
/**
* Добавление отца человека
* @param h - отец человека
*/
public void addFather(Human h) {
this.father = h;
}
/**
* Метод для вывода матери человека
* @return мать человека
*/
public Human showMother() {
return this.mother;
}
/**
* Метод для вывода отца человека
* @return отец человека
*/
public Human showFather() {
return this.mother;
}
/**
* Метод для получения возраста
* @return возраст
*/
public int getAge() {
if(dod == null) {
return Period.between(this.dob, LocalDate.now()).getYears();
}
else {
return Period.between(dob, dod).getYears();
}
}

public Gender getGender() {
return this.gender;
}

/**
* Метод для добавления ребенка
* @param child - добавляемый ребёнок
*/
public void addChild(Human child) {
children.add(child);
}
/**
* Метод для демонстрации детей
*/
public void showChildren() {
boolean hasChildren = false;
for(Human h : children) {
System.out.println(h);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возвращаем результат

hasChildren = true;
}

if(!hasChildren) {
System.out.println("Нет детей");
}
}
@Override
public String toString() {
return "Имя = " + name + ", фамилия = " + surname + ", дата рождения = " + dob + ", пол = " + gender ;
}




}