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

Созданы классы и основные методы #1192

Open
wants to merge 4 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
Binary file added fam_tree.txt
Binary file not shown.
11 changes: 11 additions & 0 deletions homeWorkFamilyTree.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file added out/production/homeWorkFamilyTree/Gender.class
Binary file not shown.
Binary file added out/production/homeWorkFamilyTree/Human.class
Binary file not shown.
Binary file added out/production/homeWorkFamilyTree/Main.class
Binary file not shown.
87 changes: 87 additions & 0 deletions src/FamilyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.io.Serializable;
import java.lang.foreign.SegmentAllocator;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class FamilyTree implements Serializable {
static long treeId = 0;
private List<Human> familyTree;

public FamilyTree() {
this(new ArrayList<Human>());
}

public FamilyTree(ArrayList<Human> humans) {
this.familyTree = humans;
}

public boolean addHuman(Human human) {
boolean res = false;
if (!familyTree.contains(human)) {
familyTree.add(human);
treeId += 1;
human.setId(treeId);
addToFamily(human);
res = true;
}
return res;
}

private void addToFamily(Human human) {
if (human.getMother() != null)
human.getMother().addChildren(human);
if (human.getFather() != null)
human.getFather().addChildren(human);
for (Human child : human.getChildren()) {
child.setParent(human);
}
}

public boolean setWedding(Human human1, Human human2) {
boolean res = false;
if (human1.getSpouse() == null && human2.getSpouse() == null) {
human1.setSpouse(human2);
human2.setSpouse(human1);
res = true;
}
return res;
}

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (Human human : familyTree) {
stringBuilder.append(human.toString());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}

public String findByName(String name) {
StringBuilder humanToFind = new StringBuilder();
// String humanToFind = "no data found";
for (Human human : familyTree) {
if (human.getName().equalsIgnoreCase(name)) {
humanToFind.append(human.toString());
}
if (human.getFirstName().equalsIgnoreCase(name)) {
humanToFind.append(human.toString());
}
else if (human.getSecondName().equalsIgnoreCase(name)) {
humanToFind.append(human.toString());
} else humanToFind.append("no data found");
}
return humanToFind.toString();
}

public String findByBirthDate(LocalDate birthDate) {
String humanToFind = "no data found";
for (Human human : familyTree) {
if (human.getBirthDate().equals(birthDate)) {
humanToFind = human.toString();
}
}
return humanToFind;
}
}
24 changes: 24 additions & 0 deletions src/FileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.io.*;

public class FileHandler implements Writable{

@Override
public boolean save(Serializable serializable, String filePath) {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath))) {
out.writeObject(serializable);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public Object read(String filePath) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath))) {
return in.readObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
3 changes: 3 additions & 0 deletions src/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum Gender {
male, female;
}
230 changes: 230 additions & 0 deletions src/Human.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import java.io.Serializable;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;

public class Human implements Serializable {
private long id;
private String firstName;
private String secondName;
private LocalDate birthDate;
private LocalDate deathDate;
private Gender gender;
private Human spouse;
private Human mother;
private Human father;
private List<Human> children = new ArrayList<>();

public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender,
Human spouse, Human parent1, Human parent2) {
this.id = -1;
this.firstName = firstName;
this.secondName = secondName;
this.birthDate = birthDate;
this.deathDate = deathDate;
this.gender = gender;
this.spouse = spouse;
if (parent1 != null && parent1.gender == Gender.male) {
this.mother = parent2;
this.father = parent1;
} else {
this.mother = parent1;
this.father = parent2;
}
}

public Human(String firstName, String secondName, LocalDate birthDate, Gender gender, Human parent1, Human parent2) {
this(firstName, secondName, birthDate, null, gender, null, parent1, parent2);
}

public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) {
this(firstName, secondName, birthDate, null, gender, null, null, null);
}

public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender) {
this(firstName, secondName, birthDate, deathDate, gender, null, null, null);
}

public void setId(long id) {
this.id = id;
}

public long getId() {
return id;
}

public void setParent(Human parent) {
if (parent.gender == Gender.female) {
this.mother = parent;
} else {
this.father = parent;
}
}

public void setParents(Human parent1, Human parent2) {
this.setParent(parent1);
this.setParent(parent2);
}

public Human getMother() {
return mother;
}

public Human getFather() {
return father;
}

public String infoParents() {
String string1 = "mother: ";
String string2 = "father: ";
if (mother == null)
string1 = string1 + "no data found";
else string1 = string1 + mother.getName();
if (father == null)
string2 = string2 + "no data found";
else string2 = string2 + father.getName();
return string1 + "\n" + string2;
}

public String infoSiblings() {
List<Human> siblings = new ArrayList<>();
StringBuilder res = new StringBuilder();
res.append("siblings: ");
if (mother == null && father == null) {
res.append("no data found");
} else if (mother != null || father != null) {
if (mother != null) {
for (Human child : mother.getChildren())
if (!child.equals(this)) {
siblings.add(child);
}
}
if (father != null) {
for (Human child : father.getChildren())
if (!child.equals(this) && !siblings.contains(child)) {
siblings.add(child);
}
}
for (Human sibling: siblings) {
res.append(sibling.getName()).append(", ");
}
} return res.toString();
}

public void setSpouse(Human spouse) {
this.spouse = spouse;
}

public Human getSpouse() {
return spouse;
}

public String infoSpouse() {
String res = "spouse: ";
if (spouse == null)
res = res + "single";
else res = res + spouse.getName();
return res;
}

public void addChildren(Human child) {
if (!children.contains(child)) {
children.add(child);
}
}

public List<Human> getChildren() {
return children;
}

public String infoChildren() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("children: ");
if (!children.isEmpty()) {
for (Human child : children) {
stringBuilder.append(child.getName()).append(", ");
}
} else stringBuilder.append("no children found");
return stringBuilder.toString();
}

public void setDeathDate(LocalDate deathDate) {
this.deathDate = deathDate;
}

public LocalDate getBirthDate() {
return birthDate;
}

public LocalDate getDeathDate() {
return deathDate;
}

public String infoDeathDate() {
String res;
if (deathDate == null)
res = "is alive";
else res = "death date: " + deathDate.toString();
return res;
}

public String getFirstName() {
return firstName;
}

public String getSecondName() {
return secondName;
}

public String getName() {
return firstName + " " + secondName;
}

public String infoBirthDate() {
return "birth date: " + birthDate.toString();
}

public String infoAge() {
Integer age;
String res;
if (deathDate == null) {
age = Period.between(birthDate, LocalDate.now()).getYears();
res = age.toString() + " years";
} else {
age = Period.between(birthDate, deathDate).getYears();
res = "died at " + age.toString() + " years";
}
return res;
}

public Gender getGender() {
return gender;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Human)) {
return false;
}
Human human = (Human) obj;
return human.getId() == getId();
}

@Override
public String toString() {
StringBuilder info = new StringBuilder();
info.append("======\n").append("id ").append(getId()).append(",\n");
info.append(getName()).append(", \n");
info.append(infoBirthDate()).append(", \n");
if (deathDate != null) info.append(infoDeathDate()).append(", \n");
info.append(infoParents()).append(", \n");
info.append(infoSpouse()).append(", \n");
info.append(infoChildren());

return info.toString();
}
}
Loading