-
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
HW 3 семинар #34
base: master
Are you sure you want to change the base?
HW 3 семинар #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package ru.famally_tree.FamilyTree; | ||
|
||
import ru.famally_tree.Human.Human; | ||
import ru.famally_tree.Human.SortByAgeComporator; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class FamilyTree implements Serializable { | ||
|
||
private List<Human> humans; | ||
private long humansId; | ||
|
||
public FamilyTree() { | ||
this(new ArrayList<>()); | ||
} | ||
|
||
public FamilyTree(List<Human> humans) { | ||
this.humans = humans; | ||
|
||
} | ||
|
||
public boolean addHumans(Human human) { | ||
if (human == null) { | ||
return false; | ||
} | ||
if (!humans.contains(human)) { | ||
humans.add(human); | ||
human.setId(humansId++); | ||
|
||
addToParent(human); | ||
addToChildren(human); | ||
|
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void addToParent(Human human) { | ||
for (Human parent : human.getParents()) { | ||
parent.addChildren(human); | ||
} | ||
} | ||
|
||
private void addToChildren(Human human) { | ||
for (Human child : human.getChildren()) { | ||
child.addParent(human); | ||
} | ||
} | ||
|
||
public List<Human> getSibLins(int id) { | ||
Human human = getById(id); | ||
if (human == null) { | ||
return null; | ||
} | ||
List<Human> res = new ArrayList<>(); | ||
for (Human parent : human.getParents()) { | ||
for (Human child : parent.getChildren()) { | ||
if (!child.equals(human)) { | ||
res.add(child); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
public List<Human> getByName(String name) { | ||
List<Human> res = new ArrayList<>(); | ||
for (Human human : humans) { | ||
if (human.getName().equals(name)) { | ||
res.add(human); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
public boolean setWedding(long humanId1, long humanId2) { | ||
if (checkId(humanId1) && checkId(humanId2)) { | ||
Human human1 = getById(humanId1); | ||
Human human2 = getById(humanId2); | ||
return setWedding(humanId1, humanId2); | ||
} | ||
return false; | ||
} | ||
|
||
public boolean setWedding(Human human1, Human human2) { | ||
if (human1.getSpouse() == null && human2.getSpouse() == null) { | ||
human1.setSpouse(human2); | ||
human2.setSpouse(human1); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean setDivorse(long humanId1, long humanId2) { | ||
if (checkId(humanId1) && checkId(humanId2)) { | ||
Human human1 = getById(humanId1); | ||
Human human2 = getById(humanId2); | ||
return setDivorse(human1, human2); | ||
} | ||
return false; | ||
} | ||
|
||
public boolean setDivorse(Human human1, Human human2) { | ||
if (human1.getSpouse() != null && human2.getSpouse() != null) { | ||
human1.setSpouse(null); | ||
human2.setSpouse(null); | ||
return setDivorse(human1, human2); | ||
} | ||
return false; | ||
} | ||
|
||
public boolean remove(long humansId) { | ||
if (checkId(humansId)) { | ||
Human human = getById(humansId); | ||
return humans.remove(human); | ||
} | ||
return false; | ||
|
||
} | ||
|
||
public boolean checkId(long id) { | ||
return id < humansId && id >= 0; | ||
} | ||
|
||
public Human getById(long id) { | ||
for (Human human : humans) { | ||
if (human.getId() == id) { | ||
return human; | ||
} | ||
} | ||
return null; | ||
} | ||
private void writeObject(ObjectOutputStream stream) throws IOException { | ||
stream.defaultWriteObject(); | ||
|
||
} | ||
|
||
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { | ||
stream.defaultReadObject(); | ||
|
||
|
||
} | ||
Comment on lines
+140
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. непонятные методы. дерево должно работать со списком людей и никакие больше потоки деревом обрабатываться не должны |
||
|
||
public void sortByAge(){ | ||
humans.sort(new SortByAgeComporator()); | ||
} | ||
|
||
public void sortByName(){ | ||
Collections.sort(humans); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return info(); | ||
} | ||
|
||
public String info() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Древо: "); | ||
sb.append(humans.size()); | ||
sb.append(" людей: \n"); | ||
for (Human human : humans) { | ||
sb.append(human); | ||
sb.append("\n"); | ||
} | ||
return sb.toString(); | ||
|
||
} | ||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ru.famally_tree.Filehandler; | ||
|
||
import java.io.*; | ||
|
||
public class FileHandler implements Writer { | ||
|
||
private String filePath = "C://work/oop2/ru/famally_tree/treeFile.out"; | ||
|
||
|
||
public void setPath(String filePath) { //для изменения пути в будущем | ||
this.filePath = filePath; | ||
} | ||
|
||
@Override | ||
public void save(Serializable serializable) { | ||
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath))){ | ||
objectOutputStream.writeObject(serializable); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
public Object read(){ | ||
try(ObjectInputStream objectInputStream = new ObjectInputStream((new FileInputStream(filePath)))){ | ||
Object obj = objectInputStream.readObject(); | ||
return obj; | ||
} catch (Exception e){ | ||
return null; | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.famally_tree.Filehandler; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface Writer { | ||
void save(Serializable serializable); | ||
Object read(); | ||
void setPath(String path); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ru.famally_tree.Human; | ||
|
||
public enum Gender { | ||
Male , Famale | ||
} |
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.
по заданию здесь должен был быть реализован интерфейс Iterable