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

Home work6 #1184

Open
wants to merge 13 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
11 changes: 11 additions & 0 deletions hw_java_oop.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 added out/production/hw_java_oop/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/hw_java_oop/model/human/Gender.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/hw_java_oop/model/writer/Writer.class
Binary file not shown.
Binary file added out/production/hw_java_oop/model/writer/tree.txt
Binary file not shown.
Binary file not shown.
Binary file added out/production/hw_java_oop/view/ConsoleUI.class
Binary file not shown.
Binary file added out/production/hw_java_oop/view/MainMenu.class
Binary file not shown.
Binary file added out/production/hw_java_oop/view/View.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import view.ConsoleUI;

public class Main {

public static void main(String[] args) {
ConsoleUI consoleUI = new ConsoleUI();
consoleUI.start();
}
}
2 changes: 0 additions & 2 deletions src/Test.java

This file was deleted.

95 changes: 95 additions & 0 deletions src/model/family_tree/FamilyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package model.family_tree;

import model.human.*;
import model.human.comparators.HumanComparatorByAge;
import model.human.comparators.HumanComparatorByBirthDate;
import model.human.comparators.HumanComparatorByName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class FamilyTree<E extends FamilyTreeItem<E>> implements Serializable, Iterable<E>{
private List<E> familyTree;

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

public List<E> getHumans(){
return familyTree;
}
public void add(E human) {
this.familyTree.add(human);
}
public boolean setWedding(E human1, E human2) {
if (human1.getSpouse() == null && human2.getSpouse() == null) {
human1.setSpouse(human2);
human2.setSpouse(human1);
return true;
} else {
return false;
}
}
public boolean setDivorce(E human1, E human2) {
if (human1.getSpouse() != null && human2.getSpouse() != null) {
human1.setSpouse(null);
human2.setSpouse(null);
return true;
} else {
return false;
}
}
public E findByName(String name) {
for (E human : familyTree) {
if (human.getName().equals(name)) {
return human;
}
}
return null;
}

public E findById(int id){
for(E human : familyTree){
if (human.getId() == id){
return human;
}
}
return null;
}

public boolean remove(E human) {
if (human != null) {
familyTree.remove(human);
return true;
}
return false;
}
@Override
public String toString() {
return getInfo();
}
private String getInfo() {
StringBuilder sb = new StringBuilder();
sb.append("В вашем древе ").append(familyTree.size()).append(" объектов\n");
for (int i = 0; i < familyTree.size(); i++) {
sb.append(i + 1 + ". " + familyTree.get(i) + "\n");
}
return sb.toString();
}
public void sortByName(){
Collections.sort(familyTree, new HumanComparatorByName<>());
}
public void sortByAge(){
Collections.sort(familyTree, new HumanComparatorByAge<>());
}
public void sortByBirthDate(){
Collections.sort(familyTree, new HumanComparatorByBirthDate<>());
}
@Override
public Iterator<E> iterator() {
return new HumanIterator<>(this);
}
}
15 changes: 15 additions & 0 deletions src/model/family_tree/FamilyTreeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model.family_tree;

import java.io.Serializable;
import java.time.LocalDate;

public interface FamilyTreeItem<E> extends Serializable {
int getAge();
String getName();
int getId();
LocalDate getBirthDate();
E getSpouse();
void setSpouse(E human);
boolean addChild(E child);
boolean addParent(E parent);
}
87 changes: 87 additions & 0 deletions src/model/family_tree/Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package model.family_tree;

import model.human.Human;
import model.human.Gender;
import model.human.HumanBuilder;
import model.writer.FileHandler;

import java.time.LocalDate;

public class Service {
private FamilyTree familyTree;
Copy link
Owner

Choose a reason for hiding this comment

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

надо указать тип дерева

private HumanBuilder humanBuilder;
FileHandler fileHandler;
Copy link
Owner

Choose a reason for hiding this comment

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

нарушает пятый принцип солид


public Service() {
familyTree = new FamilyTree<>();
humanBuilder = new HumanBuilder();
fileHandler = new FileHandler();
}

public void addHuman(String name, Gender gender, LocalDate birthDate) {
Human human = humanBuilder.setName(name).setGender(gender).setBirthDate(birthDate).build();
familyTree.add(human);
}

public boolean setWedding(String name1, String name2) {
return familyTree.setWedding(familyTree.findByName(name1), familyTree.findByName(name2));
}

public boolean setWedding(int id1, int id2) {
return familyTree.setWedding(familyTree.findById(id1), familyTree.findById(id2));
}

public boolean setDivorce(String name1, String name2) {
return familyTree.setDivorce(familyTree.findByName(name1), familyTree.findByName(name2));
}

public boolean setDivorce(int id1, int id2) {
return familyTree.setDivorce(familyTree.findById(id1), familyTree.findById(id2));
}

public boolean addChild(String human, String child) {
return familyTree.findByName(human).addChild(familyTree.findByName(child));
}

public boolean addChild(int idHuman, int idChild) {
return familyTree.findById(idHuman).addChild(familyTree.findById(idChild));
}

public boolean addParent(String child, String parent) {
return familyTree.findByName(child).addParent(familyTree.findByName(parent));
}

public boolean addParent(int child, int parent) {
return familyTree.findById(child).addParent(familyTree.findById(parent));
}

public FamilyTree getFamilyTree() {
return familyTree;
}

public void sortByName() {
familyTree.sortByName();
}

public void sortByAge() {
familyTree.sortByAge();
}

public void sortByBirthDate() {
familyTree.sortByBirthDate();
}

public boolean saveToFile(String filename) {
fileHandler.setPath(filename);
return fileHandler.save(familyTree);
}

public boolean loadFromFile(String filePath) {
fileHandler.setPath(filePath);
familyTree = (FamilyTree) fileHandler.read();
if (familyTree.equals(null))
return false;
else
return true;
}
}
5 changes: 5 additions & 0 deletions src/model/human/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model.human;

public enum Gender {
Male, Female;
}
Loading