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

Java family tree #20

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
72aa171
Created classes and added fields in Human class
Sile9t May 21, 2024
0fe96a4
Expanded Human class and added FamilyTree method
Sile9t May 21, 2024
08f6e09
Expanded FamilyTree class, added generators
Sile9t May 21, 2024
f126ec6
Fixed bugs
Sile9t May 21, 2024
d0d1e4a
Added serializer class
Sile9t May 28, 2024
e33844c
Organized files
Sile9t May 31, 2024
fb6aa52
Added HumanIterator
Sile9t May 31, 2024
59a18a1
Implemented familytree iterator
Sile9t May 31, 2024
988c785
Init service class
Sile9t May 31, 2024
b0e562a
Init HumanBuilder
Sile9t May 31, 2024
79da669
Added HumanBuilder build method
Sile9t May 31, 2024
667f309
Added service addHuman and getFamilyTree mathods
Sile9t May 31, 2024
1b3983b
Added service addHuman and getFamilyTree mathods
Sile9t May 31, 2024
dfc7c36
Added sortByDate method
Sile9t May 31, 2024
f68963e
Added sortByName and HumanNameComparator
Sile9t May 31, 2024
51000ce
refactored methods name
Sile9t Jun 2, 2024
683f7b9
Added FamilyTreeItem interface
Sile9t Jun 2, 2024
810f51d
Added Human class FamilyTreeItem interface implementation
Sile9t Jun 2, 2024
66457ae
Switched classes and methods to generick
Sile9t Jun 2, 2024
02b5f89
Refactored files ctructuring
Sile9t Jun 8, 2024
a4a2f19
Init Presenter class
Sile9t Jun 8, 2024
e6367d8
Init View interface
Sile9t Jun 8, 2024
17086d7
Added Presentre ctor
Sile9t Jun 8, 2024
0443112
Init ConsoleView class
Sile9t Jun 8, 2024
3346cca
Added View printAnswer and start methods
Sile9t Jun 8, 2024
2ce69d9
Added ConsoleView.start method
Sile9t Jun 8, 2024
699d160
Added ConsoleView.addHuman method
Sile9t Jun 11, 2024
4486e41
Added ConsoleView.finish method
Sile9t Jun 11, 2024
72c9841
Added Consoel.View command methods
Sile9t Jun 11, 2024
131ddc1
Added Command base class
Sile9t Jun 11, 2024
caa3bb1
Extended Command base class
Sile9t Jun 11, 2024
6f8fdea
Added AddHuman command
Sile9t Jun 11, 2024
25be7cd
Added GetFamilyTree command
Sile9t Jun 11, 2024
57dd231
Added SortByName command
Sile9t Jun 11, 2024
2b5ca97
Added SortByBirthDate command
Sile9t Jun 11, 2024
e61d38c
Added Finish command
Sile9t Jun 11, 2024
dcd4fd6
Added Menu class
Sile9t Jun 11, 2024
852d597
Added commands into ConsoleView
Sile9t Jun 11, 2024
f6ceb46
Finished ConsoleView.addHuman method
Sile9t Jun 11, 2024
ce76437
Refactored code, removed unused methods
Sile9t Jun 12, 2024
dbc9a81
Refactored ConsoleView, Presenter and Service dependencies
Sile9t Jun 13, 2024
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 File
Binary file not shown.
8 changes: 8 additions & 0 deletions src/ru/gb/family_tree/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import view.*;

public class Main {
public static void main(String[] args) {
View view = new ConsoleView();
view.start();
}
}
15 changes: 15 additions & 0 deletions src/ru/gb/family_tree/model/builders/HumanBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model.builders;

import java.time.LocalDate;

import model.human.Human;

public class HumanBuilder {
private int genId;
public Human build(String name){
return new Human(genId++, name, LocalDate.now());
}
public Human build(String name, Human mother, Human father){
return new Human(genId++, name, LocalDate.now(), mother, father);
}
}
52 changes: 52 additions & 0 deletions src/ru/gb/family_tree/model/familyTree/FamilyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package model.familyTree;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import model.human.comparators.NameComparator;

public class FamilyTree<T extends FamilyTreeItem<T>> implements Serializable, Iterable<T> {
private ArrayList<T> tree;
public FamilyTree() {
tree = new ArrayList<T>();
}
public FamilyTree(ArrayList<T> tree) {
this.tree = tree;
}
public ArrayList<T> tree(){
return tree;
}
public Boolean add(T el){
if (tree.contains(el)) return false;
return tree.add(el);
}
public T remove(int index){
return tree.remove(index);
}
public T getFirst(){
return tree.getFirst();
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("FamilyTree:\n");
if (tree == null) sb.append("Empty\n");
for(var el : tree)
sb.append(el.toString() + "\n");
return sb.toString();
}
@Override
public Iterator<T> iterator() {
return new FamilyTreeIterator<T>(tree);
}
public void sort() {
Collections.sort(tree);
}
public void sortByName(){
tree.sort(new NameComparator<T>());
}
public T get(int id) {
return tree.get(id);
}
}
8 changes: 8 additions & 0 deletions src/ru/gb/family_tree/model/familyTree/FamilyTreeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package model.familyTree;

import java.io.Serializable;

public interface FamilyTreeItem<T> extends Comparable<T>,Serializable {
String getName();
String toString();
}
21 changes: 21 additions & 0 deletions src/ru/gb/family_tree/model/familyTree/FamilyTreeIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model.familyTree;

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

public class FamilyTreeIterator<T extends FamilyTreeItem<T>> implements Iterator<T>{
private int index;
private List<T> tree;
public FamilyTreeIterator(List<T> list) {
tree = list;
}
@Override
public boolean hasNext() {
return tree.size() > index;
}
@Override
public T next() {
return tree.get(index++);
}

}
40 changes: 40 additions & 0 deletions src/ru/gb/family_tree/model/familyTree/FamilyTreeSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package model.familyTree;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FamilyTreeSerializer<T extends FamilyTreeItem<T>> {
public void write(String path, FamilyTree<T> obj){
try {
var outputStream = new ObjectOutputStream(new FileOutputStream(path));
outputStream.writeObject(obj);
outputStream.close();
}
catch (IOException e){
System.out.println("File not exist or wrong path!");
}
}
public FamilyTree<T> read(String path){
try{
var inputStream = new ObjectInputStream(new FileInputStream(path));
@SuppressWarnings("unchecked")
FamilyTree<T> ft = (FamilyTree<T>)inputStream.readObject();
inputStream.close();
return ft;
}
catch (IOException e){
System.out.println("File not exist or wrong path!");
return null;
}
catch (ClassCastException e){
System.out.println("Enable to cast object!");
return null;
}
catch (ClassNotFoundException e){
System.out.println("Can't find class library!");
return null;
}
}
}
5 changes: 5 additions & 0 deletions src/ru/gb/family_tree/model/human/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model.human;

public enum Gender {
Male, Female
}
100 changes: 100 additions & 0 deletions src/ru/gb/family_tree/model/human/Human.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package model.human;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;

import model.familyTree.FamilyTreeItem;

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

public Human(long id, String name, LocalDate birthDate) {
this(id, name, birthDate, null, null, null);
}
public Human(long id, String name, LocalDate birthDate, Human mother, Human father) {
this(id, name, birthDate, mother, father, null);
}
public Human(long id, String name, LocalDate birthDate, Human mother,
Human father, ArrayList<Human> children) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
this.gender = id % 2 == 0? Gender.Male : Gender.Female;
this.mother = mother;
if (this.mother != null) this.mother.children.add(this);
this.father = father;
if (this.father != null) this.father.children.add(this);
this.children = children == null? (new ArrayList<Human>()) : children;
}

public String getName(){
return name;
}
public LocalDate getBirthDate(){
return birthDate;
}
public Gender getGender(){
return gender;
}
public Human getMother(){
return mother;
}
public Human getFather(){
return father;
}
public List<Human> getChildren(){
return children;
}
public Boolean addChild(Human child){
if (children.contains(child)) return false;
return children.add(child);
}
public Boolean addChildren(ArrayList<Human> children){
return children.addAll(children);
}
public Boolean setDeathDate(LocalDate deathDate){
if (this.deathDate != null) return false;
this.deathDate = deathDate;
return true;
}
public LocalDate getDeathDate(){
return deathDate;
}
public int getAge(){
LocalDate d2 = LocalDate.now();
if (deathDate != null) d2 = deathDate;
return DiffBetween(deathDate,d2).getYears();
}
private Period DiffBetween(LocalDate d1, LocalDate d2){
return Period.between(d1, d2);
}
@Override
public String toString(){
return String.format("%d %s %s born: %s mother: %s father: %s", id, name,
gender, birthDate, mother != null? mother.name : "none",
father != null? father.name : "none");
}
// public String FamilyTree(){
// return FamilyTree(0);
// }
// public String FamilyTree(int tabsCount){
// StringBuilder sb = new StringBuilder();
// sb.append(this.toString() + "\n");
// String tabs = "\t".repeat(tabsCount);
// tabsCount++;
// for(var child : children)
// sb.append(tabs + child.FamilyTree(tabsCount));
// return sb.toString();
// }
@Override
public int compareTo(Human o) {
return birthDate.compareTo(o.getBirthDate());
}
}
13 changes: 13 additions & 0 deletions src/ru/gb/family_tree/model/human/comparators/NameComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model.human.comparators;

import java.util.Comparator;

import model.familyTree.FamilyTreeItem;

public class NameComparator<T extends FamilyTreeItem<T>> implements Comparator<T>{

@Override
public int compare(T o1, T o2) {
return o1.getName().compareTo(o2.getName());
}
}
38 changes: 38 additions & 0 deletions src/ru/gb/family_tree/model/services/Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package model.services;

import model.builders.HumanBuilder;
import model.familyTree.FamilyTree;
import model.human.Human;

public class Service {
private FamilyTree<Human> tree;
private HumanBuilder humanBuilder;
public Service() {
tree = new FamilyTree<>();
humanBuilder = new HumanBuilder();
}
public void addHuman(String name){
var human = humanBuilder.build(name);
tree.add(human);
}
public void addHuman(String name, int motherId, int fatherId){
var human = humanBuilder.build(name, getHuman(motherId), getHuman(fatherId));
tree.add(human);
}
public String getFamilyTree(){
StringBuilder sb = new StringBuilder();
sb.append("Family tree:\n");
for(Human human : tree)
sb.append(human + "\n");
return sb.toString();
}
public void sortByBirthDate(){
tree.sort();
}
public void sortByName(){
tree.sortByName();
}
public Human getHuman(int id) {
return tree.get(id);
}
}
45 changes: 45 additions & 0 deletions src/ru/gb/family_tree/presenter/Presenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package presenter;

import model.familyTree.FamilyTreeItem;
import model.human.Human;
import model.services.Service;
import view.View;

public class Presenter {
private View view;
private Service service;

public Presenter(View view){
this.view = view;
this.service = new Service();
}

public void addTreeItem(String name) {
service.addHuman(name);
getFamilyTree();
}

public void addTreeItem(String name, int motherId, int fatherId){
service.addHuman(name, motherId, fatherId);
getFamilyTree();
}

Copy link
Owner

Choose a reason for hiding this comment

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

добавить методы сохранения и загрузки

Copy link
Author

Choose a reason for hiding this comment

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

Они реализованы в классе FamilyTreeSerializer, я подумал что такое дублирование будет лишним и удалил вызывающие методы

Copy link
Owner

Choose a reason for hiding this comment

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

Они то реализованы, но как то этот функционал не прикручен к основному проекту

Copy link
Author

Choose a reason for hiding this comment

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

Оно было в этом коммите 51000ce , но впоследствии я их удалил. Теперь буду знать, что не стоит так делать.

public FamilyTreeItem<Human> getTreeItem(int id) {
return service.getHuman(id);
}

public void getFamilyTree() {
String answer = service.getFamilyTree();
view.printAnswer(answer);
}

public void sortByName() {
service.sortByName();
getFamilyTree();
}

public void sortByBirthDate() {
service.sortByBirthDate();
getFamilyTree();
}
}
Loading