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

HW 3 семинар #34

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 180 additions & 0 deletions src/famally_tree/FamilyTree/FamilyTree.java
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 {
Copy link
Owner

Choose a reason for hiding this comment

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

по заданию здесь должен был быть реализован интерфейс Iterable


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
Copy link
Owner

Choose a reason for hiding this comment

The 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();

}



}
35 changes: 35 additions & 0 deletions src/famally_tree/Filehandler/FileHandler.java
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;
}

}


}
10 changes: 10 additions & 0 deletions src/famally_tree/Filehandler/Writer.java
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);
}

5 changes: 5 additions & 0 deletions src/famally_tree/Human/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.famally_tree.Human;

public enum Gender {
Male , Famale
}
Loading