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

HW3 #1173

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

HW3 #1173

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 .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea
.iml
out
Binary file added Homework3_OOP/.DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions Homework3_OOP/Handler/FileHandler.java
Original file line number Diff line number Diff line change
@@ -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;
}

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

public enum Gender {
Male, Female;
}
234 changes: 234 additions & 0 deletions Homework3_OOP/Human/Human.java
Original file line number Diff line number Diff line change
@@ -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<Human> {
private long id;
private Gender gender;
private String name;
private LocalDate birthDate, deathDate;
private Human mother;
private Human father;
private List<Human> 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<Human> getParents(){
List<Human> 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<Human> 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<children.size(); i++){
res.append(", ");
res.append(children.get(i).getName());
}
} else {
res.append("отсутствуют");
}

return res.toString();
}

@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 int compareTo(Human o){
return name.compareTo(o.name);
}

}

45 changes: 45 additions & 0 deletions Homework3_OOP/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package Homework2_OOP;

import java.time.LocalDate;

public class Main{

public static void main(String[] args) {


FamilyTree tree = famTree();
System.out.println(tree);

}

private static FamilyTree load() {
FileHandler fileHandler = new FileHandler();
return (FamilyTree) fileHandler.read();
}

private static void save(FamilyTree familyTree){
FileHandler fileHandler = new FileHandler();
fileHandler.save(familyTree);
}

private static FamilyTree famTree() {
FamilyTree tree = new FamilyTree();

Human first = new Human ("Altair", Gender.Male, LocalDate.of(1975, 2, 14));
Human second = new Human ("Jasmine", Gender.Female, LocalDate.of(1976, 3, 11));

tree.add(first);
tree.add(second);
tree.setWedding(first, second);

Human child1 = new Human ("Balian", Gender.Male, LocalDate.of(2000, 1, 23), first, second);

tree.add(child1);

tree.sortById(); // Сортировка по ID

tree.sortByName(); // Сортировка по имени

return tree;
}
}
Loading