-
Notifications
You must be signed in to change notification settings - Fork 69
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
Sile9t
wants to merge
41
commits into
Liberate520:master
Choose a base branch
from
Sile9t:java-family-tree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Java family tree #20
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 0fe96a4
Expanded Human class and added FamilyTree method
Sile9t 08f6e09
Expanded FamilyTree class, added generators
Sile9t f126ec6
Fixed bugs
Sile9t d0d1e4a
Added serializer class
Sile9t e33844c
Organized files
Sile9t fb6aa52
Added HumanIterator
Sile9t 59a18a1
Implemented familytree iterator
Sile9t 988c785
Init service class
Sile9t b0e562a
Init HumanBuilder
Sile9t 79da669
Added HumanBuilder build method
Sile9t 667f309
Added service addHuman and getFamilyTree mathods
Sile9t 1b3983b
Added service addHuman and getFamilyTree mathods
Sile9t dfc7c36
Added sortByDate method
Sile9t f68963e
Added sortByName and HumanNameComparator
Sile9t 51000ce
refactored methods name
Sile9t 683f7b9
Added FamilyTreeItem interface
Sile9t 810f51d
Added Human class FamilyTreeItem interface implementation
Sile9t 66457ae
Switched classes and methods to generick
Sile9t 02b5f89
Refactored files ctructuring
Sile9t a4a2f19
Init Presenter class
Sile9t e6367d8
Init View interface
Sile9t 17086d7
Added Presentre ctor
Sile9t 0443112
Init ConsoleView class
Sile9t 3346cca
Added View printAnswer and start methods
Sile9t 2ce69d9
Added ConsoleView.start method
Sile9t 699d160
Added ConsoleView.addHuman method
Sile9t 4486e41
Added ConsoleView.finish method
Sile9t 72c9841
Added Consoel.View command methods
Sile9t 131ddc1
Added Command base class
Sile9t caa3bb1
Extended Command base class
Sile9t 6f8fdea
Added AddHuman command
Sile9t 25be7cd
Added GetFamilyTree command
Sile9t 57dd231
Added SortByName command
Sile9t 2b5ca97
Added SortByBirthDate command
Sile9t e61d38c
Added Finish command
Sile9t dcd4fd6
Added Menu class
Sile9t 852d597
Added commands into ConsoleView
Sile9t f6ceb46
Finished ConsoleView.addHuman method
Sile9t ce76437
Refactored code, removed unused methods
Sile9t dbc9a81
Refactored ConsoleView, Presenter and Service dependencies
Sile9t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/ru/gb/family_tree/model/familyTree/FamilyTreeIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/ru/gb/family_tree/model/familyTree/FamilyTreeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package model.human; | ||
|
||
public enum Gender { | ||
Male, Female | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/ru/gb/family_tree/model/human/comparators/NameComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
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(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
добавить методы сохранения и загрузки
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Они реализованы в классе FamilyTreeSerializer, я подумал что такое дублирование будет лишним и удалил вызывающие методы
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Они то реализованы, но как то этот функционал не прикручен к основному проекту
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Оно было в этом коммите 51000ce , но впоследствии я их удалил. Теперь буду знать, что не стоит так делать.