diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000..bbb9b9fbd0 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 723ef36f4e..22bb2c0591 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea \ No newline at end of file +.idea +.iml +out diff --git a/Homework3_OOP/.DS_Store b/Homework3_OOP/.DS_Store new file mode 100644 index 0000000000..b9006b8afe Binary files /dev/null and b/Homework3_OOP/.DS_Store differ diff --git a/Homework3_OOP/Handler/FileHandler.java b/Homework3_OOP/Handler/FileHandler.java new file mode 100644 index 0000000000..edc48dff04 --- /dev/null +++ b/Homework3_OOP/Handler/FileHandler.java @@ -0,0 +1,30 @@ +package Homework2_OOP; + +import java.io.*; + +public class FileHandler implements Writer { + + @Override + public boolean save(Serializable serializable){ + + try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("save.txt"))) { + objectOutputStream.writeObject(serializable); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + @Override + public Object read() { + try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("save.txt"))){ + return objectInputStream.readObject(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } +} diff --git a/Homework3_OOP/Human/Gender.java b/Homework3_OOP/Human/Gender.java new file mode 100644 index 0000000000..9bfb724a3a --- /dev/null +++ b/Homework3_OOP/Human/Gender.java @@ -0,0 +1,5 @@ +package Homework2_OOP; + +public enum Gender { + Male, Female; +} diff --git a/Homework3_OOP/Human/Human.java b/Homework3_OOP/Human/Human.java new file mode 100644 index 0000000000..46a4af6dcd --- /dev/null +++ b/Homework3_OOP/Human/Human.java @@ -0,0 +1,234 @@ +package Homework2_OOP; + +import java.io.Serializable; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.time.Period; + +public class Human implements Serializable, Comparable { + private long id; + private Gender gender; + private String name; + private LocalDate birthDate, deathDate; + private Human mother; + private Human father; + private List children; + private Human spouse; + + public Human(String name, Gender gender, LocalDate birthDate, LocalDate deathDate, Human father, Human mother) { + + id = -1; + this.name = name; + this.gender = gender; + this.birthDate = birthDate; + this.deathDate = deathDate; + this.father = father; + this.mother = mother; + children = new ArrayList<>(); + + } + + public Human(String name, Gender gender, LocalDate birthDate) { //Делаем перегрузку + this(name, gender, birthDate, null, null, null); + } + + public Human(String name, Gender gender, LocalDate birthDate, Human father, Human mother) { + this(name, gender, birthDate, null, father, mother); + } + + public boolean addChild(Human child){ + if (!children.contains(child)){ + children.add(child); + return true; + } + return false; + } + + public boolean addParent(Human parent){ + if (parent.getGender().equals(Gender.Male)){ + setFather(parent); + } else if (parent.getGender().equals(Gender.Female)) { + setMother(parent); + } + return true; + } + + + public void setGender(Gender gender) { + this.gender = gender; + } + + public String getName(){ + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Human getMother() { + return mother; + } + + public void setMother(Human mother) { + this.mother = mother; + } + + public Human getFather() { + return father; + } + + public void setFather(Human father) { + this.father = father; + } + + public List getParents(){ + List list = new ArrayList<>(2); + if(father != null){ + list.add(father); + } + if(mother != null){ + list.add(mother); + } + return list; + } + + 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 void setSpouse(Human spouse) { + this.spouse = spouse; + } + + public Human getSpouse() { + return spouse; + } + + public long getId(){ + return id; + } + + public void setId(long id){ + this.id = id; + } + + public List getChildren() { + return children; + } + + public void setBirthDate(LocalDate birthDate) { + this.birthDate = birthDate; + } + + public void setDeathDate(LocalDate deathDate) { + this.deathDate = deathDate; + } + + public Gender getGender() { + return gender; + } + + @Override + public String toString() { + return getInfo(); + } + + public String getInfo(){ + StringBuilder sb = new StringBuilder(); + sb.append("id: "); + sb.append(id); + sb.append(", имя: "); + sb.append(name); + sb.append(", пол: "); + sb.append(getGender()); + sb.append(", возраст: "); + sb.append(getAge()); + sb.append(", "); + sb.append(getSpouseInfo()); + sb.append(", "); + sb.append(getMotherInfo()); + sb.append(", "); + sb.append(getFatherInfo()); + sb.append(", "); + sb.append(getChildrenInfo()); + return sb.toString(); + } + + public String getSpouseInfo(){ + String res = "супруг(-а): "; + if (spouse == null){ + res += "нет"; + } else { + res += spouse.getName(); + } + return res; + } + + public String getMotherInfo(){ + String res = "мать: "; + Human mother = getMother(); + if (mother != null){ + res += mother.getName(); + } else { + res += "неизвестна"; + } + return res; + } + + public String getFatherInfo(){ + String res = "отец: "; + Human father = getFather(); + if (father != null){ + res += father.getName(); + } else { + res += "неизвестен"; + } + return res; + } + + public String getChildrenInfo(){ + StringBuilder res = new StringBuilder(); + res.append("дети: "); + if(children.size() != 0){ + res.append(children.get(0).getName()); + for(int i=1; i{ + private long humansId; + private List humanList; + + public FamilyTree(){ + this(new ArrayList<>()); + } + + public FamilyTree(List humanList) { + this.humanList = humanList; + } + + public boolean add(Human 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(Human human){ + for (Human parent: human.getParents()){ + parent.addChild(human); + } + } + + private void addToChildren(Human human){ + for (Human child: human.getChildren()){ + child.addParent(human); + } + } + + public List getSiblings(int id){ + Human human = getById(id); + if (human == null){ + return null; + } + List res = new ArrayList<>(); + for (Human parent: human.getParents()){ + for (Human child: parent.getChildren()){ + if (!child.equals(human)){ + res.add(child); + } + } + } + return res; + } + + public List getByName(String name){ + List res = new ArrayList<>(); + for (Human human: humanList){ + 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(human1, human2); + } + 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 setDivorce(Human human1, Human human2){ + if (human1.getSpouse() != null && human2.getSpouse() != null){ + human1.setSpouse(null); + human2.setSpouse(null); + return true; + } else { + return false; + } + } + + public boolean remove(long humansId){ + if (checkId(humansId)){ + Human human = getById(humansId); + return humanList.remove(human); + } + return false; + } + + private boolean checkId(long id) { + return id < humansId && id >=0; + } + + public Human getById(long id) { + for (Human human: humanList){ + if (human.getId() == id){ + return human; + } + } + return null; + } + + @Override + public String toString() { + return getInfo(); + } + + public String getInfo(){ + StringBuilder sb = new StringBuilder(); + sb.append("В древе: "); + sb.append(humanList.size()); + sb.append(" объектов(-а): \n"); + for (Human human : humanList){ + sb.append(human); + sb.append("\n"); + } + return sb.toString(); + } + + public void sortByName(){ + Collections.sort(humanList); + } + + public void sortById(){ + Collections.sort(humanList); + } + + @Override + public Iterator iterator(){ + return new HumanIterator(); + } + + class HumanIterator implements Iterator{ + + private int index; + private List humanList; + + public StudentIterator(List humanList) { + this.humanList = humanList; + } + + @Override + public boolean hasNext() { + return index < humanList.size(); + } + + @Override + public Student next() { + return humanList.get(index++); + } + } + +} + diff --git a/Homework3_OOP/Writer/Writer.java b/Homework3_OOP/Writer/Writer.java new file mode 100644 index 0000000000..12929d58c4 --- /dev/null +++ b/Homework3_OOP/Writer/Writer.java @@ -0,0 +1,8 @@ +package Homework2_OOP; + +import java.io.Serializable; + +public interface Writer { + public boolean save(Serializable serializable); + public Object read(); +} diff --git a/Homework3_OOP/save.txt b/Homework3_OOP/save.txt new file mode 100644 index 0000000000..fafc4f3b04 Binary files /dev/null and b/Homework3_OOP/save.txt differ