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

FamilyTreeHomeWork #1180

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea
homeWork.iml
.out
14 changes: 14 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import family_tree.model.family_tree.FamilyTree;
import family_tree.model.human.Gender;
import family_tree.model.human.Human;
import family_tree.model.writer.FileHandler;
import family_tree.view.ConsoleUI;

import java.time.LocalDate;

public class Main {
public static void main(String[] args) {
ConsoleUI consoleUI = new ConsoleUI();
consoleUI.start();
}
}
2 changes: 0 additions & 2 deletions src/Test.java

This file was deleted.

26 changes: 26 additions & 0 deletions src/family_tree/model/FamilyTreeIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package family_tree.model;

import family_tree.model.family_tree.FamilyTreeItem;

import java.util.Iterator;
import java.util.List;

public class FamilyTreeIterator<T extends FamilyTreeItem<T>> implements Iterator<T> {
private int index;
private List<T> list;

public FamilyTreeIterator(List<T> list){
index = 0;
this.list = list;
}

@Override
public boolean hasNext() {
return index > list.size();
}

@Override
public T next() {
return list.get(index++);
}
}
76 changes: 76 additions & 0 deletions src/family_tree/model/family_tree/FamilyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package family_tree.model.family_tree;

import family_tree.model.human.comparators.ComparatorByAge;
import family_tree.model.human.comparators.ComparatorByName;
import family_tree.model.FamilyTreeIterator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class FamilyTree<E extends FamilyTreeItem<E>> implements Serializable, Iterable<E> {
private long humanId;
private List<E> humans;

public FamilyTree(){
this(new ArrayList<>());
}

public FamilyTree(List<E> humans){
this.humans = humans;
}

public void add(E human){
this.humans.add(human);
}

public List<E> getByName(String name){
List<E> res = new ArrayList<>();
for (E human : humans) {
if (human.getName().equals(name)){
res.add(human);
}
}
return res;
}

public E getById(long id){
for (E human: humans){
if (human.getId() == id){
return human;
}
}
return null;
}

public void sortByAge(){
Collections.sort(humans, new ComparatorByAge<>());
}

public void sortByName(){
Collections.sort(humans, new ComparatorByName<>());
}

public Iterator<E> iterator(){
return new FamilyTreeIterator<>(humans);
}

@Override
public String toString(){
return getInfo();
}
public String getInfo() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("В семье ");
stringBuilder.append(humans.size());
stringBuilder.append(" человек(а) \n");
for (E human : humans){
stringBuilder.append(human);
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
}

18 changes: 18 additions & 0 deletions src/family_tree/model/family_tree/FamilyTreeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package family_tree.model.family_tree;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;

public interface FamilyTreeItem<T> extends Serializable {
String getName();
void setId(long id);
long getId();
LocalDate getBirthDate();
LocalDate getDeathDate();
boolean addParent(T parent);
boolean addChild(T human);
List<T> getChildren();
int getAge();

}
5 changes: 5 additions & 0 deletions src/family_tree/model/human/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package family_tree.model.human;

public enum Gender {
Male, Female;
}
187 changes: 187 additions & 0 deletions src/family_tree/model/human/Human.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package family_tree.model.human;

import family_tree.model.family_tree.FamilyTreeItem;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;

public class Human implements FamilyTreeItem<Human> {
private long id;
private String name;
private Gender gender;
private LocalDate birthDate;
private LocalDate deathDate;
private Human mother;
private Human father;
private List<Human> children;

public Human(String name, LocalDate birthDate, LocalDate deathDate, Gender gender, Human mother, Human father){
id = -1;
this.name = name;
this.birthDate = birthDate;
this.deathDate = deathDate;
this.gender = gender;
this.mother = mother;
this.father = father;
children = new ArrayList<>();
}

public Human(String name, LocalDate birthDate, Gender gender){
this(name, birthDate, null, gender, null, null);
}

public Human(){
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public LocalDate getBirthDate() {
return birthDate;
}

public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}

public LocalDate getDeathDate() {
return deathDate;
}

public void setDeathDate(LocalDate deathDate) {
this.deathDate = deathDate;
}

public String getName(){
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Human> getChildren() {
return children;
}

public void setChildren(List<Human> children) {
this.children = children;
}

public Human getMother(Human mother) {
return mother;
}

public void setMother(Human mother) {
this.mother = mother;
}

public Human getFather() {
return father;
}

public void setFather(Human father) {
this.father = father;
}

public boolean addChild(Human child){
if (!children.contains(child)){
children.add(child);
return true;
}
return false;
}

public boolean addParent(Human parent){
if (parent.gender.equals(Gender.Female)) {
setMother(parent);
} else if (parent.gender.equals(Gender.Male)) {
setFather(parent);
}
return true;
}

public void setGender(Gender gender){
this.gender = gender;
}

public int getAge(){
if (deathDate == null){
return getPeriod(birthDate, LocalDate.now());
}else {
return getPeriod(birthDate, deathDate);
}
}
public int getPeriod(LocalDate birthDate, LocalDate deathDate){
Period diff = Period.between(birthDate, deathDate);
return diff.getYears();
}

public String toString(){
return getInfo();
}

public String getInfo(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("id: ");
stringBuilder.append(id);
stringBuilder.append(", имя: ");
stringBuilder.append(name);
stringBuilder.append(", возраст: ");
stringBuilder.append(getAge());
stringBuilder.append(", пол: ");
stringBuilder.append(gender);
stringBuilder.append(", мать: ");
stringBuilder.append(getMotherInfo());
stringBuilder.append(", отец: ");
stringBuilder.append(getFatherInfo());
stringBuilder.append(", ");
stringBuilder.append(getChildrenInfo());
return stringBuilder.toString();
}

public String getMotherInfo(){
if (this.mother == null)
return "Неизвестно";
return this.mother.name;
}
private String getFatherInfo() {
if (this.father == null)
return "Неизвестно";
return this.father.name;
}

public String getChildrenInfo(){
StringBuilder chil = new StringBuilder();
chil.append("Дети: ");
if (children.size() != 0){
chil.append(children.get(0).getName());
for (int i = 1; i < children.size(); i++) {
chil.append(", ");
chil.append(children.get(i).getName());
}
}else {
chil.append("отсутствуют");
}
return chil.toString();
}

@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (!(obj instanceof Human)){
return false;
}
Human human = (Human) obj;
return human.getId() == getId();
}
}
Loading