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

task5_FamilyTree_group6381 #1191

Open
wants to merge 9 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
Binary file added out/production/homeWork/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/homeWork/model/human/Gender.class
Binary file not shown.
Binary file added out/production/homeWork/model/human/Human.class
Binary file not shown.
Binary file added out/production/homeWork/model/human/TreeNode.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/homeWork/model/writer/Writer.class
Binary file not shown.
Binary file added out/production/homeWork/model/writer/tree.txt
Binary file not shown.
Binary file added out/production/homeWork/presenter/Presenter.class
Binary file not shown.
Binary file added out/production/homeWork/view/ConsoleUI.class
Binary file not shown.
Binary file added out/production/homeWork/view/MainMenu.class
Binary file not shown.
Binary file added out/production/homeWork/view/View.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/homeWork/view/commands/Command.class
Binary file not shown.
Binary file added out/production/homeWork/view/commands/Finish.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/homeWork/view/commands/Load.class
Binary file not shown.
Binary file added out/production/homeWork/view/commands/Save.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
27 changes: 27 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import model.FamilyTree.FamilyTree;
import model.human.Gender;
import model.human.Human;

import java.io.IOException;
import java.time.LocalDate;
import model.writer.FileHandler;
import presenter.Presenter;
import view.ConsoleUI;
import view.MainMenu;
import view.View;
import model.service.Service;





public class Main {


public static void main(String[] args) throws IOException {
ConsoleUI consoleUI = new ConsoleUI();
consoleUI.start();

}

}
2 changes: 0 additions & 2 deletions src/Test.java

This file was deleted.

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


import model.human.Human;
import model.human.comparators.HumanComparatorByAge;
import model.human.comparators.HumanComparatorByName;
import model.human.TreeNode;

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

public class FamilyTree<E extends TreeNode<E>> implements Serializable, Iterable<E> {
private long humansId;
private List<E> humanList;

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

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

public boolean add(E human) {
if (human == null) {
return false;
}
if (!humanList.contains(human)) {
humanList.add(human);
human.setId(humansId++);
addToParents(human);
addToChildren(human);
return true;
}
return false;
}

private void addToParents(E human) {
for (E parent : human.getParents()) {
if (!parent.getChildren().contains(human)) {
parent.addChild(human);
}
}
}

private void addToChildren(E human) {
if (human.getChildren() != null) {

for (E child : human.getChildren()) {
if (!child.getParents().contains(human)) {
child.addParent(human);}
}
}
}


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

public String getInfo() {
StringBuilder sb = new StringBuilder();
sb.append("tree:\n");
sb.append(humanList.size()).append(" members\n");
for (E human : humanList) {
sb.append(human);
sb.append("\n");
}
return sb.toString();
}
public void sortByAge() {
humanList.sort(new HumanComparatorByAge<>());
}


public void sortByName(){
humanList.sort(new HumanComparatorByName<>());
}



@Override
public Iterator<E> iterator() {
return new HumanIterator<>(humanList);
}


public void addHuman(E human) {
add(human);
}
}







33 changes: 33 additions & 0 deletions src/model/FamilyTree/HumanIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package model.FamilyTree;
import model.human.TreeNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HumanIterator <E>implements Iterator<E> {
private int index;
private List<E> humanList;

public HumanIterator(List<E> humanList) {
this.index = 0;
this.humanList = humanList;
}


@Override
public boolean hasNext() {
return index<humanList.size();
}

@Override
public E next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException("No more elements");
}
return humanList.get(index++);

}
public void remove() {
throw new UnsupportedOperationException("Remove operation is not supported");
}
}
6 changes: 6 additions & 0 deletions src/model/human/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model.human;

public enum Gender {
Male, Female;

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

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

public class Human implements TreeNode<Human>, Serializable, Comparable<Human> {


private long id;
private String name;
private Gender gender;
private LocalDate birthDate;
private LocalDate deathDate;
private List<Human> parents;
private List<Human> children;
private Human mother;
private Human father;

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

public Human() {
this.parents = new ArrayList<>();
this.children = new ArrayList<>();
}


public long getId() {
return id;
}

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

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


public Gender getGender() {
return gender;
}

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

public boolean addParent(Human parent) {
if (parent == null) return false;
if (!parents.contains(parent)) {
parents.add(parent);
if (parent.getGender() == Gender.Female) {
setMother(parent);
} else if (parent.getGender() == Gender.Male) {
setFather(parent);
}
return true;
}
return false;
}


public List<Human> getParents() {
List<Human> list = new ArrayList<>(2);
if (father != null) {
list.add(father);
}
if (mother != null) {
list.add(mother);
}
return list;
}


public LocalDate getBirthDate() {
return birthDate;
}

public LocalDate getDeathDate() {
return deathDate;
}


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


public Human getMother() {
return mother;
}

public Human getFather() {
return father;
}

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

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

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


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

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


public String getName() {

return name;
}

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

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

private int getPeriod(LocalDate birthDate, LocalDate deathDate) {
Period diff = Period.between(birthDate, deathDate);
return diff.getYears();
}

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

public String getInfo() {
StringBuilder sb = new StringBuilder();
sb.append(" id: ").append(id);
sb.append(" name: ").append(getName());
sb.append(" gender: ").append(getGender());
sb.append(" age: ").append(getAge());
sb.append(" mother: ").append(getMotherInfo());
sb.append(" father: ").append(getFatherInfo());
sb.append(" children: ").append(getChildrenInfo());


return sb.toString();
}

public String getMotherInfo() {
String res = "mother ";
Human mother = getMother();
if (mother != null) {
res += mother.getName();
} else {
res += "null";
}
return res;
}

public String getFatherInfo() {
String res = "father ";
Human father = getFather();
if (father != null) {
res += father.getName();
} else {
res += "null";
}
return res;
}


public String getChildrenInfo() {
StringBuilder res = new StringBuilder();
res.append(" ");
if (!children.isEmpty()) {
for (Human child : children) {
res.append(child.getName());
res.append(" ");
}
} else {res.append("null");}
return res.toString();

}


@Override
public int compareTo(Human o) {
return name.compareTo(o.name);
}
}
Loading