diff --git a/hw_java_oop.iml b/hw_java_oop.iml new file mode 100644 index 0000000000..c90834f2d6 --- /dev/null +++ b/hw_java_oop.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/hw_java_oop/Main.class b/out/production/hw_java_oop/Main.class new file mode 100644 index 0000000000..4c4c0cde30 Binary files /dev/null and b/out/production/hw_java_oop/Main.class differ diff --git a/out/production/hw_java_oop/model/family_tree/FamilyTree.class b/out/production/hw_java_oop/model/family_tree/FamilyTree.class new file mode 100644 index 0000000000..516b8a6d02 Binary files /dev/null and b/out/production/hw_java_oop/model/family_tree/FamilyTree.class differ diff --git a/out/production/hw_java_oop/model/family_tree/FamilyTreeItem.class b/out/production/hw_java_oop/model/family_tree/FamilyTreeItem.class new file mode 100644 index 0000000000..e7aa2e8aff Binary files /dev/null and b/out/production/hw_java_oop/model/family_tree/FamilyTreeItem.class differ diff --git a/out/production/hw_java_oop/model/family_tree/Service.class b/out/production/hw_java_oop/model/family_tree/Service.class new file mode 100644 index 0000000000..71a03d1e71 Binary files /dev/null and b/out/production/hw_java_oop/model/family_tree/Service.class differ diff --git a/out/production/hw_java_oop/model/human/Gender.class b/out/production/hw_java_oop/model/human/Gender.class new file mode 100644 index 0000000000..278aa92405 Binary files /dev/null and b/out/production/hw_java_oop/model/human/Gender.class differ diff --git a/out/production/hw_java_oop/model/human/Human.class b/out/production/hw_java_oop/model/human/Human.class new file mode 100644 index 0000000000..61128f0a83 Binary files /dev/null and b/out/production/hw_java_oop/model/human/Human.class differ diff --git a/out/production/hw_java_oop/model/human/HumanBuilder.class b/out/production/hw_java_oop/model/human/HumanBuilder.class new file mode 100644 index 0000000000..a7cfa5f791 Binary files /dev/null and b/out/production/hw_java_oop/model/human/HumanBuilder.class differ diff --git a/out/production/hw_java_oop/model/human/HumanIterator.class b/out/production/hw_java_oop/model/human/HumanIterator.class new file mode 100644 index 0000000000..2336dfbbba Binary files /dev/null and b/out/production/hw_java_oop/model/human/HumanIterator.class differ diff --git a/out/production/hw_java_oop/model/human/comparators/HumanComparatorByAge.class b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByAge.class new file mode 100644 index 0000000000..e84db825c8 Binary files /dev/null and b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByAge.class differ diff --git a/out/production/hw_java_oop/model/human/comparators/HumanComparatorByBirthDate.class b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByBirthDate.class new file mode 100644 index 0000000000..d4c1ed7770 Binary files /dev/null and b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByBirthDate.class differ diff --git a/out/production/hw_java_oop/model/human/comparators/HumanComparatorByName.class b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByName.class new file mode 100644 index 0000000000..206f86fd67 Binary files /dev/null and b/out/production/hw_java_oop/model/human/comparators/HumanComparatorByName.class differ diff --git a/out/production/hw_java_oop/model/writer/FileHandler.class b/out/production/hw_java_oop/model/writer/FileHandler.class new file mode 100644 index 0000000000..888d73f8d3 Binary files /dev/null and b/out/production/hw_java_oop/model/writer/FileHandler.class differ diff --git a/out/production/hw_java_oop/model/writer/Writer.class b/out/production/hw_java_oop/model/writer/Writer.class new file mode 100644 index 0000000000..36ec911916 Binary files /dev/null and b/out/production/hw_java_oop/model/writer/Writer.class differ diff --git a/out/production/hw_java_oop/model/writer/tree.txt b/out/production/hw_java_oop/model/writer/tree.txt new file mode 100644 index 0000000000..79708534cf Binary files /dev/null and b/out/production/hw_java_oop/model/writer/tree.txt differ diff --git a/out/production/hw_java_oop/presenter/Presenter.class b/out/production/hw_java_oop/presenter/Presenter.class new file mode 100644 index 0000000000..9376b02df4 Binary files /dev/null and b/out/production/hw_java_oop/presenter/Presenter.class differ diff --git a/out/production/hw_java_oop/view/ConsoleUI.class b/out/production/hw_java_oop/view/ConsoleUI.class new file mode 100644 index 0000000000..58b3370c86 Binary files /dev/null and b/out/production/hw_java_oop/view/ConsoleUI.class differ diff --git a/out/production/hw_java_oop/view/MainMenu.class b/out/production/hw_java_oop/view/MainMenu.class new file mode 100644 index 0000000000..3de37346da Binary files /dev/null and b/out/production/hw_java_oop/view/MainMenu.class differ diff --git a/out/production/hw_java_oop/view/View.class b/out/production/hw_java_oop/view/View.class new file mode 100644 index 0000000000..06b5ff7939 Binary files /dev/null and b/out/production/hw_java_oop/view/View.class differ diff --git a/out/production/hw_java_oop/view/commands/AddChild.class b/out/production/hw_java_oop/view/commands/AddChild.class new file mode 100644 index 0000000000..f39f1dfdcf Binary files /dev/null and b/out/production/hw_java_oop/view/commands/AddChild.class differ diff --git a/out/production/hw_java_oop/view/commands/AddHuman.class b/out/production/hw_java_oop/view/commands/AddHuman.class new file mode 100644 index 0000000000..1cfed82219 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/AddHuman.class differ diff --git a/out/production/hw_java_oop/view/commands/AddParent.class b/out/production/hw_java_oop/view/commands/AddParent.class new file mode 100644 index 0000000000..aa50372044 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/AddParent.class differ diff --git a/out/production/hw_java_oop/view/commands/Command.class b/out/production/hw_java_oop/view/commands/Command.class new file mode 100644 index 0000000000..36a84d1716 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/Command.class differ diff --git a/out/production/hw_java_oop/view/commands/Finish.class b/out/production/hw_java_oop/view/commands/Finish.class new file mode 100644 index 0000000000..e9b2903501 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/Finish.class differ diff --git a/out/production/hw_java_oop/view/commands/GetFamilyTreeInfo.class b/out/production/hw_java_oop/view/commands/GetFamilyTreeInfo.class new file mode 100644 index 0000000000..e43779cbd9 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/GetFamilyTreeInfo.class differ diff --git a/out/production/hw_java_oop/view/commands/LoadFromFile.class b/out/production/hw_java_oop/view/commands/LoadFromFile.class new file mode 100644 index 0000000000..3b749470ed Binary files /dev/null and b/out/production/hw_java_oop/view/commands/LoadFromFile.class differ diff --git a/out/production/hw_java_oop/view/commands/SaveToFile.class b/out/production/hw_java_oop/view/commands/SaveToFile.class new file mode 100644 index 0000000000..0a7643f93d Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SaveToFile.class differ diff --git a/out/production/hw_java_oop/view/commands/SetDivorce.class b/out/production/hw_java_oop/view/commands/SetDivorce.class new file mode 100644 index 0000000000..908691ca27 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SetDivorce.class differ diff --git a/out/production/hw_java_oop/view/commands/SetWedding.class b/out/production/hw_java_oop/view/commands/SetWedding.class new file mode 100644 index 0000000000..6cd28ba7c7 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SetWedding.class differ diff --git a/out/production/hw_java_oop/view/commands/SortByAge.class b/out/production/hw_java_oop/view/commands/SortByAge.class new file mode 100644 index 0000000000..47a8c7f7db Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SortByAge.class differ diff --git a/out/production/hw_java_oop/view/commands/SortByBirthdate.class b/out/production/hw_java_oop/view/commands/SortByBirthdate.class new file mode 100644 index 0000000000..1e78466ce5 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SortByBirthdate.class differ diff --git a/out/production/hw_java_oop/view/commands/SortByName.class b/out/production/hw_java_oop/view/commands/SortByName.class new file mode 100644 index 0000000000..430f8c6fe2 Binary files /dev/null and b/out/production/hw_java_oop/view/commands/SortByName.class differ diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000000..6c51ea0d88 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,9 @@ +import view.ConsoleUI; + +public class Main { + + public static void main(String[] args) { + ConsoleUI consoleUI = new ConsoleUI(); + consoleUI.start(); + } +} \ No newline at end of file diff --git a/src/Test.java b/src/Test.java deleted file mode 100644 index a06540728b..0000000000 --- a/src/Test.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Test { -} diff --git a/src/model/family_tree/FamilyTree.java b/src/model/family_tree/FamilyTree.java new file mode 100644 index 0000000000..5f7f2b80b5 --- /dev/null +++ b/src/model/family_tree/FamilyTree.java @@ -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> implements Serializable, Iterable{ + private List familyTree; + + public FamilyTree() { + this.familyTree = new ArrayList<>(); + } + + public List 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 iterator() { + return new HumanIterator<>(this); + } +} diff --git a/src/model/family_tree/FamilyTreeItem.java b/src/model/family_tree/FamilyTreeItem.java new file mode 100644 index 0000000000..5574424c95 --- /dev/null +++ b/src/model/family_tree/FamilyTreeItem.java @@ -0,0 +1,15 @@ +package model.family_tree; + +import java.io.Serializable; +import java.time.LocalDate; + +public interface FamilyTreeItem extends Serializable { + int getAge(); + String getName(); + int getId(); + LocalDate getBirthDate(); + E getSpouse(); + void setSpouse(E human); + boolean addChild(E child); + boolean addParent(E parent); +} diff --git a/src/model/family_tree/Service.java b/src/model/family_tree/Service.java new file mode 100644 index 0000000000..d1e68bede7 --- /dev/null +++ b/src/model/family_tree/Service.java @@ -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; + private HumanBuilder humanBuilder; + FileHandler fileHandler; + + 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; + } +} diff --git a/src/model/human/Gender.java b/src/model/human/Gender.java new file mode 100644 index 0000000000..3f24ac0fd0 --- /dev/null +++ b/src/model/human/Gender.java @@ -0,0 +1,5 @@ +package model.human; + +public enum Gender { + Male, Female; +} diff --git a/src/model/human/Human.java b/src/model/human/Human.java new file mode 100644 index 0000000000..4c6b6b55c6 --- /dev/null +++ b/src/model/human/Human.java @@ -0,0 +1,167 @@ +package model.human; + +import model.family_tree.FamilyTreeItem; +import java.time.LocalDate; +import java.time.Period; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Human implements FamilyTreeItem { + private int id; + private String name; + private Gender gender; + private LocalDate birthDate, deathDate; + private Human spouse; + private Human mother; + private Human father; + private List children; + + public Human(String name, Gender gender, LocalDate birthDate, LocalDate deathDate, Human father, Human mother) { + this.name = name; + this.gender = gender; + this.birthDate = birthDate; + this.deathDate = deathDate; + this.father = father; + this.mother = mother; + this.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 Human() { + } + public void setId(int id){ + this.id = id; + } + public int getId(){ + return id; + } + public void setChildren(List children){ + this.children = children; + } + public boolean addChild(Human child) { + if (!children.contains(child)) { + children.add(child); + return true; + } + return false; + } + public boolean addParent(Human parent) { + if (parent.gender.equals(Gender.Female)) { + setMother(parent); + } else if (parent.gender.equals(Gender.Male)) { + setFather(parent); + } + return true; + } + public void setGender(Gender gender) { + this.gender = gender; + } + public void setBirthDate(LocalDate birthDate) { + this.birthDate = birthDate; + } + public LocalDate getBirthDate(){ + return birthDate; + } + public void setMother(Human mother) { + this.mother = mother; + } + public Human getMother() { + return mother; + } + public void setFather(Human father) { + this.father = father; + } + public Human getFather() { + return father; + } + public Human getSpouse() { + return this.spouse; + } + public void setSpouse(Human spouse) { + this.spouse = spouse; + } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Human human = (Human) o; + return Objects.equals(name, human.name) && gender == human.gender && Objects.equals(birthDate, human.birthDate) && Objects.equals(deathDate, human.deathDate) && Objects.equals(spouse, human.spouse) && Objects.equals(mother, human.mother) && Objects.equals(father, human.father) && Objects.equals(children, human.children); + } + @Override + public int hashCode() { + return Objects.hash(name, gender, birthDate, deathDate, spouse, mother, father, children); + } + public void setDeathDate(LocalDate deathDate) { + this.deathDate = deathDate; + } + @Override + public int getAge() { + LocalDate date = LocalDate.now(); + if (this.deathDate != null) { + date = this.deathDate; + } + return getPeriod(this.birthDate, date); + } + private int getPeriod(LocalDate startPeriod, LocalDate endPeriod) { + Period diff = Period.between(startPeriod, endPeriod); + return diff.getYears(); + } + public void setName(String name) { + this.name = name; + } + @Override + public String getName() { + return this.name; + } + private String getGender() { + return this.gender.toString(); + } + private String getSpauseInfo() { + if (this.spouse == null) + return "нет"; + return getSpouse().name; + } + private String getMotherInfo() { + if (this.mother == null) + return "нет информации"; + return this.mother.name; + } + private String getFatherInfo() { + if (this.father == null) + return "нет информации"; + return this.father.name; + } + private String getChildrenInfo() { + if (this.children.isEmpty()) { + return "отсутствуют"; + } else { + String[] children = new String[this.children.size()]; + for (int i = 0; i < this.children.size(); i++) { + children[i] = this.children.get(i).name; + } + return String.join(", ", children); + } + } + @Override + public String toString() { + return getInfo(); + } + private String getInfo() { + StringBuilder sb = new StringBuilder(); + sb.append("id: ").append(id); + sb.append(". Имя: ").append(getName()); + sb.append(", пол: ").append(getGender()); + sb.append(", возраст: ").append(getAge()); + sb.append(", супруг(а): ").append(getSpauseInfo()); + sb.append(", мать: ").append(getMotherInfo()); + sb.append(", отец: ").append(getFatherInfo()); + sb.append(", дети: "); + sb.append(getChildrenInfo()); + return sb.toString(); + } +} diff --git a/src/model/human/HumanBuilder.java b/src/model/human/HumanBuilder.java new file mode 100644 index 0000000000..de648a0c9e --- /dev/null +++ b/src/model/human/HumanBuilder.java @@ -0,0 +1,59 @@ +package model.human; + +import model.family_tree.FamilyTreeItem; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class HumanBuilder { + private int genId; + private Human human; + private String name; + private Gender gender; + private LocalDate birthDate; + private List children; + + public HumanBuilder(){ + createHuman(); + } + + public HumanBuilder setName(String name){ + this.name = name; + return this; + } + public HumanBuilder setGender(Gender gender){ + this.gender = gender; + return this; + } + public HumanBuilder setBirthDate(LocalDate birthdate){ + this.birthDate = birthdate; + return this; + } + private void setChildren(){ + human.setChildren(new ArrayList<>()); + } + private void createHuman(){ + human = new Human(); + } + private void nextId(){ + human.setId(genId++); + } + private void createName(){ + human.setName(name); + } + private void createGender(){ + human.setGender(gender); + } + private void createBirthDate(){ + human.setBirthDate(birthDate); + } + public Human build(){ + createHuman(); + createName(); + createGender(); + createBirthDate(); + nextId(); + setChildren(); + return human; + } +} \ No newline at end of file diff --git a/src/model/human/HumanIterator.java b/src/model/human/HumanIterator.java new file mode 100644 index 0000000000..59bb79630b --- /dev/null +++ b/src/model/human/HumanIterator.java @@ -0,0 +1,29 @@ +package model.human; + +import model.family_tree.FamilyTree; +import model.family_tree.FamilyTreeItem; +import java.util.Iterator; +import java.util.List; + +public class HumanIterator> implements Iterator { + private int curIndex; + private List humans; + + public HumanIterator(FamilyTree familyTree) { + this.humans = familyTree.getHumans(); + this.curIndex = 0; + } + + @Override + public boolean hasNext() { + return curIndex < humans.size() -1; + } + + @Override + public E next() { + if (!hasNext()){ + return null; + } + return humans.get(curIndex++); + } +} \ No newline at end of file diff --git a/src/model/human/comparators/HumanComparatorByAge.java b/src/model/human/comparators/HumanComparatorByAge.java new file mode 100644 index 0000000000..56ae6bc1ab --- /dev/null +++ b/src/model/human/comparators/HumanComparatorByAge.java @@ -0,0 +1,12 @@ +package model.human.comparators; + +import model.family_tree.FamilyTreeItem; + +import java.util.Comparator; + +public class HumanComparatorByAge> implements Comparator { + @Override + public int compare(E o1, E o2) { + return Integer.compare(o1.getAge(), o2.getAge()); + } +} \ No newline at end of file diff --git a/src/model/human/comparators/HumanComparatorByBirthDate.java b/src/model/human/comparators/HumanComparatorByBirthDate.java new file mode 100644 index 0000000000..5830d1c120 --- /dev/null +++ b/src/model/human/comparators/HumanComparatorByBirthDate.java @@ -0,0 +1,12 @@ +package model.human.comparators; + +import model.family_tree.FamilyTreeItem; + +import java.util.Comparator; + +public class HumanComparatorByBirthDate> implements Comparator { + @Override + public int compare(E o1, E o2) { + return o1.getBirthDate().compareTo(o2.getBirthDate()); + } +} diff --git a/src/model/human/comparators/HumanComparatorByName.java b/src/model/human/comparators/HumanComparatorByName.java new file mode 100644 index 0000000000..633bdc71fd --- /dev/null +++ b/src/model/human/comparators/HumanComparatorByName.java @@ -0,0 +1,12 @@ +package model.human.comparators; + +import model.family_tree.FamilyTreeItem; + +import java.util.Comparator; + +public class HumanComparatorByName> implements Comparator { + @Override + public int compare(E o1, E o2) { + return o1.getName().compareTo(o1.getName()); + } +} diff --git a/src/model/writer/FileHandler.java b/src/model/writer/FileHandler.java new file mode 100644 index 0000000000..1154955767 --- /dev/null +++ b/src/model/writer/FileHandler.java @@ -0,0 +1,33 @@ +package model.writer; + +import java.io.*; + +public class FileHandler implements Writer { + private String filePath; + + @Override + public boolean save(Serializable serializable) { + try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath))) { + objectOutputStream.writeObject(serializable); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + @Override + public Object read() { + try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath))) { + return objectInputStream.readObject(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + @Override + public void setPath(String filePath) { + this.filePath = filePath; + } +} diff --git a/src/model/writer/Writer.java b/src/model/writer/Writer.java new file mode 100644 index 0000000000..88e39d43b8 --- /dev/null +++ b/src/model/writer/Writer.java @@ -0,0 +1,11 @@ +package model.writer; + +import java.io.Serializable; + +public interface Writer { + boolean save(Serializable serializable); + + Object read(); + + void setPath(String filePath); +} diff --git a/src/model/writer/tree.txt b/src/model/writer/tree.txt new file mode 100644 index 0000000000..79708534cf Binary files /dev/null and b/src/model/writer/tree.txt differ diff --git a/src/presenter/Presenter.java b/src/presenter/Presenter.java new file mode 100644 index 0000000000..c016132d75 --- /dev/null +++ b/src/presenter/Presenter.java @@ -0,0 +1,84 @@ +package presenter; + +import model.family_tree.Service; +import model.human.Gender; +import view.View; + +import java.time.LocalDate; +import java.util.Scanner; + +public class Presenter { + private View view; + private Service service; + + public Presenter(View view) { + this.view = view; + service = new Service(); + } + + public void addHuman(String name, Gender gender, LocalDate birthDate) { + service.addHuman(name, gender, birthDate); + //какой-то ответ + getFamilyTreeInfo(); + } + + public boolean addParent(String childName, String parentName) { + return service.addParent(childName, parentName); + } + + public boolean addParent(int childId, int parentId) { + return service.addParent(childId, parentId); + } + + public boolean addChild(String parentName, String childName) { + return service.addChild(parentName, childName); + } + + public boolean addChild(int parentId, int childId) { + return service.addChild(parentId, childId); + } + + public void sortByAge() { + service.sortByAge(); + getFamilyTreeInfo(); + } + + public void sortByName() { + service.sortByName(); + getFamilyTreeInfo(); + } + + public void sortByBirthDate() { + service.sortByBirthDate(); + getFamilyTreeInfo(); + } + + public void getFamilyTreeInfo() { + String info = service.getFamilyTree().toString(); + view.printAnswer(info); + } + + public boolean setWedding(String name1, String name2) { + return service.setWedding(name1, name2); + } + + public boolean setWedding(int id1, int id2) { + return service.setWedding(id1, id2); + } + + public boolean setDivorce(String name1, String name2) { + return service.setDivorce(name1, name2); + } + + public boolean setDivorce(int id1, int id2) { + return service.setDivorce(id1, id2); + } + + public boolean saveToFile(String filename) { + return service.saveToFile(filename); + } + + public boolean loadFromFile(String filename) { + return service.loadFromFile(filename); + } +} \ No newline at end of file diff --git a/src/view/ConsoleUI.java b/src/view/ConsoleUI.java new file mode 100644 index 0000000000..03f12ad13c --- /dev/null +++ b/src/view/ConsoleUI.java @@ -0,0 +1,255 @@ +package view; + +import model.human.Gender; +import presenter.Presenter; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Scanner; + +public class ConsoleUI implements View { + private static final String INPUT_ERROR = "Вы ввели неверное значение"; + private Scanner scanner; + private Presenter presenter; + private boolean isWork; + private MainMenu menu; + + public ConsoleUI() { + scanner = new Scanner(System.in); + presenter = new Presenter(this); + isWork = true; + menu = new MainMenu(this); + } + + @Override + public void start() { + hello(); + while (isWork) { + printMenu(); + execute(); + } + } + + @Override + public void printAnswer(String answer) { + System.out.println(answer); + } + + private void hello() { + printAnswer("Доброго времени суток!"); + } + + private void execute() { + String line = scanner.nextLine(); + if (checkTextForInt(line)) { + int numCommand = Integer.parseInt(line); + if (checkCommand(numCommand)) { + menu.execute(numCommand); + } + } + } + + private boolean checkTextForInt(String text) { + if (text.matches("[0-9]+")) { + return true; + } else { + inputError(); + return false; + } + } + + private boolean checkCommand(int numCommand) { + if (numCommand <= menu.getSize()) { + return true; + } else { + inputError(); + return false; + } + } + + private void printMenu() { + printAnswer(menu.menu()); + } + + private void inputError() { + printAnswer(INPUT_ERROR); + } + + public void finish() { + printAnswer("До свидания"); + isWork = false; + } + + public void addHuman() { + String name; + Gender gender; + LocalDate birthDate; + String strFormatter = "dd.MM.yyyy"; + printAnswer("Введите имя:"); + name = scanner.nextLine(); + printAnswer("Укажите пол\n1 - мужской, 2- женский"); + String sex = scanner.nextLine(); + while (!(sex.charAt(0) == '1' || sex.charAt(0) == '2')) { + inputError(); + printAnswer("Укажите пол\n1 - мужской, 2- женский"); + sex = scanner.nextLine(); + } + if (sex.equals("1")) + gender = Gender.Male; + else + gender = Gender.Female; + + printAnswer("Введите дату рождения в формате " + strFormatter); + String strDate = scanner.nextLine(); + while (strDate.length() != 10 && strDate.split(".").length != 3) { + inputError(); + printAnswer("Введите дату рождения в формате " + strFormatter); + strDate = scanner.nextLine(); + } + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(strFormatter); + birthDate = LocalDate.parse(strDate, dateTimeFormatter); + presenter.addHuman(name, gender, birthDate); + } + + public void addParent() { + String childName, parentName; + int childId, parentId; + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + int choice = Integer.parseInt(scanner.nextLine()); + while (choice < 1 || choice > 2) { + inputError(); + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + choice = Integer.parseInt(scanner.nextLine()); + } + if (choice == 1) { + printAnswer("Введите имя ребёнка"); + childName = scanner.nextLine(); + printAnswer("Введите имя родителя"); + parentName = scanner.nextLine(); + presenter.addParent(childName, parentName); + } else { + printAnswer("Введите id ребёнка"); + childId = Integer.parseInt(scanner.nextLine()); + printAnswer("Введите id родителя"); + parentId = Integer.parseInt(scanner.nextLine()); + presenter.addParent(childId, parentId); + } + presenter.getFamilyTreeInfo(); + } + + public void addChild() { + String childName, parentName; + int childId, parentId; + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + int choice = Integer.parseInt(scanner.nextLine()); + while (choice < 1 || choice > 2) { + inputError(); + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + choice = Integer.parseInt(scanner.nextLine()); + } + if (choice == 1) { + printAnswer("Введите имя родителя"); + parentName = scanner.nextLine(); + printAnswer("Введите имя ребёнка"); + childName = scanner.nextLine(); + presenter.addChild(parentName, childName); + } else { + printAnswer("Введите id родителя"); + parentId = Integer.parseInt(scanner.nextLine()); + printAnswer("Введите id ребёнка"); + childId = Integer.parseInt(scanner.nextLine()); + presenter.addChild(parentId, childId); + } + presenter.getFamilyTreeInfo(); + } + + public void setWedding() { + String name1, name2; + int id1, id2; + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + int choice = Integer.parseInt(scanner.nextLine()); + while (choice < 1 || choice > 2) { + inputError(); + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + choice = Integer.parseInt(scanner.nextLine()); + } + if (choice == 1) { + printAnswer("Введите первое имя"); + name1 = scanner.nextLine(); + printAnswer("Введите второе имя"); + name2 = scanner.nextLine(); + presenter.setWedding(name1, name2); + } else { + printAnswer("Введите первый id"); + id1 = Integer.parseInt(scanner.nextLine()); + printAnswer("Введите второй id"); + id2 = Integer.parseInt(scanner.nextLine()); + presenter.setWedding(id1, id2); + } + } + + public void setDivorce() { + String name1, name2; + int id1, id2; + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + int choice = Integer.parseInt(scanner.nextLine()); + while (choice < 1 || choice > 2) { + inputError(); + printAnswer("Выберете способ добавления:\n1 - по имени; 2 - по id"); + choice = Integer.parseInt(scanner.nextLine()); + } + if (choice == 1) { + printAnswer("Введите первое имя"); + name1 = scanner.nextLine(); + printAnswer("Введите второе имя"); + name2 = scanner.nextLine(); + presenter.setDivorce(name1, name2); + } else { + printAnswer("Введите первый id"); + id1 = Integer.parseInt(scanner.nextLine()); + printAnswer("Введите второй id"); + id2 = Integer.parseInt(scanner.nextLine()); + presenter.setDivorce(id1, id2); + } + } + + public void getFamilyTreeinfo() { + presenter.getFamilyTreeInfo(); + } + + public void sortByName() { + presenter.sortByName(); + } + + public void sortByAge() { + presenter.sortByAge(); + } + + public void sortByBirthdate() { + presenter.sortByBirthDate(); + } + + public void saveToFile() { + String filename; + String filePath = "src/model/writer/"; + printAnswer("Укажите имя файла"); + filename = scanner.nextLine(); + filePath += filename + ".txt"; + if (presenter.saveToFile(filePath)) + printAnswer("Файл " + filename + " успешно сохранен"); + else + printAnswer("Ошибка сохранения"); + } + + public void loadFromFile() { + String filename; + String filePath = "src/model/writer/"; + printAnswer("Укажите имя файла"); + filename = scanner.nextLine(); + filePath += filename + ".txt"; + if (presenter.loadFromFile(filePath)) + printAnswer("Файл " + filename + " успешно загружен"); + else + printAnswer("Ошибка загрузки"); + } +} diff --git a/src/view/MainMenu.java b/src/view/MainMenu.java new file mode 100644 index 0000000000..554246f72f --- /dev/null +++ b/src/view/MainMenu.java @@ -0,0 +1,46 @@ +package view; + +import view.commands.*; + +import java.util.ArrayList; +import java.util.List; + +public class MainMenu { + private List commandList; + + public MainMenu(ConsoleUI consoleUI){ + commandList = new ArrayList<>(); + commandList.add(new AddHuman(consoleUI)); + commandList.add(new AddParent(consoleUI)); + commandList.add(new AddChild(consoleUI)); + commandList.add(new SetWedding(consoleUI)); + commandList.add(new SetDivorce(consoleUI)); + commandList.add(new SortByName(consoleUI)); + commandList.add(new SortByAge(consoleUI)); + commandList.add(new SortByBirthdate(consoleUI)); + commandList.add((new GetFamilyTreeInfo(consoleUI))); + commandList.add((new SaveToFile(consoleUI))); + commandList.add((new LoadFromFile(consoleUI))); + commandList.add((new Finish(consoleUI))); + } + + public String menu(){ + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < commandList.size(); i++) { + stringBuilder.append(i+1); + stringBuilder.append(". "); + stringBuilder.append(commandList.get(i).getDescription()); + stringBuilder.append("\n"); + } + return stringBuilder.toString(); + } + + public void execute(int choice){ + Command command = commandList.get(choice-1); + command.execute(); + } + + public int getSize(){ + return commandList.size(); + } +} diff --git a/src/view/View.java b/src/view/View.java new file mode 100644 index 0000000000..7c4ec8eba3 --- /dev/null +++ b/src/view/View.java @@ -0,0 +1,6 @@ +package view; + +public interface View { + void start(); + void printAnswer(String answer); +} diff --git a/src/view/commands/AddChild.java b/src/view/commands/AddChild.java new file mode 100644 index 0000000000..6d3f22ae21 --- /dev/null +++ b/src/view/commands/AddChild.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class AddChild extends Command{ + + public AddChild(ConsoleUI consoleUI){ + super(consoleUI); + description = "Добавить ребёнка"; + } + @Override + public void execute() { + consoleUI.addChild(); + } +} diff --git a/src/view/commands/AddHuman.java b/src/view/commands/AddHuman.java new file mode 100644 index 0000000000..f2086439f7 --- /dev/null +++ b/src/view/commands/AddHuman.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class AddHuman extends Command{ + + public AddHuman(ConsoleUI consoleUI){ + super(consoleUI); + description = "Добавить человека"; + } + @Override + public void execute() { + consoleUI.addHuman(); + } +} diff --git a/src/view/commands/AddParent.java b/src/view/commands/AddParent.java new file mode 100644 index 0000000000..20182e1ffe --- /dev/null +++ b/src/view/commands/AddParent.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class AddParent extends Command{ + + public AddParent(ConsoleUI consoleUI){ + super(consoleUI); + description = "Добавить родителя"; + } + @Override + public void execute() { + consoleUI.addParent(); + } +} diff --git a/src/view/commands/Command.java b/src/view/commands/Command.java new file mode 100644 index 0000000000..9c1be2f122 --- /dev/null +++ b/src/view/commands/Command.java @@ -0,0 +1,18 @@ +package view.commands; + +import view.ConsoleUI; + +public abstract class Command { + String description; + ConsoleUI consoleUI; + + public Command(ConsoleUI consoleUI) { + this.consoleUI = consoleUI; + } + + public String getDescription() { + return description; + } + + public abstract void execute(); +} diff --git a/src/view/commands/Finish.java b/src/view/commands/Finish.java new file mode 100644 index 0000000000..f987dd8818 --- /dev/null +++ b/src/view/commands/Finish.java @@ -0,0 +1,14 @@ +package view.commands; + +import view.ConsoleUI; + +public class Finish extends Command { + public Finish(ConsoleUI consoleUI) { + super(consoleUI); + description = "Закончить работу"; + } + + public void execute(){ + consoleUI.finish(); + } +} diff --git a/src/view/commands/GetFamilyTreeInfo.java b/src/view/commands/GetFamilyTreeInfo.java new file mode 100644 index 0000000000..ada7f8b8eb --- /dev/null +++ b/src/view/commands/GetFamilyTreeInfo.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class GetFamilyTreeInfo extends Command{ + + public GetFamilyTreeInfo(ConsoleUI consoleUI){ + super(consoleUI); + description = "Показать весь список"; + } + @Override + public void execute() { + consoleUI.getFamilyTreeinfo(); + } +} \ No newline at end of file diff --git a/src/view/commands/LoadFromFile.java b/src/view/commands/LoadFromFile.java new file mode 100644 index 0000000000..8885c4a548 --- /dev/null +++ b/src/view/commands/LoadFromFile.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class LoadFromFile extends Command{ + + public LoadFromFile(ConsoleUI consoleUI){ + super(consoleUI); + description = "Загрузить из файла"; + } + @Override + public void execute() { + consoleUI.loadFromFile(); + } +} \ No newline at end of file diff --git a/src/view/commands/SaveToFile.java b/src/view/commands/SaveToFile.java new file mode 100644 index 0000000000..d37eae12bb --- /dev/null +++ b/src/view/commands/SaveToFile.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SaveToFile extends Command{ + + public SaveToFile(ConsoleUI consoleUI){ + super(consoleUI); + description = "Сохранить в файл"; + } + @Override + public void execute() { + consoleUI.saveToFile(); + } +} \ No newline at end of file diff --git a/src/view/commands/SetDivorce.java b/src/view/commands/SetDivorce.java new file mode 100644 index 0000000000..dad8eaba07 --- /dev/null +++ b/src/view/commands/SetDivorce.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SetDivorce extends Command{ + + public SetDivorce(ConsoleUI consoleUI){ + super(consoleUI); + description = "Развести 2 людей"; + } + @Override + public void execute() { + consoleUI.setDivorce(); + } +} \ No newline at end of file diff --git a/src/view/commands/SetWedding.java b/src/view/commands/SetWedding.java new file mode 100644 index 0000000000..4432e80059 --- /dev/null +++ b/src/view/commands/SetWedding.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SetWedding extends Command{ + + public SetWedding(ConsoleUI consoleUI){ + super(consoleUI); + description = "Поженить 2 людей"; + } + @Override + public void execute() { + consoleUI.setWedding(); + } +} \ No newline at end of file diff --git a/src/view/commands/SortByAge.java b/src/view/commands/SortByAge.java new file mode 100644 index 0000000000..fea7a17fff --- /dev/null +++ b/src/view/commands/SortByAge.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SortByAge extends Command{ + + public SortByAge(ConsoleUI consoleUI){ + super(consoleUI); + description = "Отсортировать по возрасту"; + } + @Override + public void execute() { + consoleUI.sortByAge(); + } +} diff --git a/src/view/commands/SortByBirthdate.java b/src/view/commands/SortByBirthdate.java new file mode 100644 index 0000000000..b9e1889fbf --- /dev/null +++ b/src/view/commands/SortByBirthdate.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SortByBirthdate extends Command{ + + public SortByBirthdate(ConsoleUI consoleUI){ + super(consoleUI); + description = "Отсортровать по дате рождения"; + } + @Override + public void execute() { + consoleUI.sortByBirthdate(); + } +} diff --git a/src/view/commands/SortByName.java b/src/view/commands/SortByName.java new file mode 100644 index 0000000000..d38ee0ac3c --- /dev/null +++ b/src/view/commands/SortByName.java @@ -0,0 +1,15 @@ +package view.commands; + +import view.ConsoleUI; + +public class SortByName extends Command{ + + public SortByName(ConsoleUI consoleUI){ + super(consoleUI); + description = "Отсортировать по имени"; + } + @Override + public void execute() { + consoleUI.sortByName(); + } +}