diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5a596b9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GBLesson1.iml b/GBLesson1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/GBLesson1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/GBLesson1/ru/gb/family_tree/Main.class b/out/production/GBLesson1/ru/gb/family_tree/Main.class new file mode 100644 index 0000000..96bf3cd Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/Main.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/FamilyTreeManager.class b/out/production/GBLesson1/ru/gb/family_tree/model/FamilyTreeManager.class new file mode 100644 index 0000000..cf0d74d Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/FamilyTreeManager.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/HumanFactory.class b/out/production/GBLesson1/ru/gb/family_tree/model/HumanFactory.class new file mode 100644 index 0000000..4415a65 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/HumanFactory.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/PersonFactory.class b/out/production/GBLesson1/ru/gb/family_tree/model/PersonFactory.class new file mode 100644 index 0000000..6fb1bcc Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/PersonFactory.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/Service.class b/out/production/GBLesson1/ru/gb/family_tree/model/Service.class new file mode 100644 index 0000000..fa41479 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/Service.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.class b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.class new file mode 100644 index 0000000..07bec28 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamilyTree.class b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamilyTree.class new file mode 100644 index 0000000..ec4bab7 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/FamilyTree.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.class b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.class new file mode 100644 index 0000000..9555f69 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByName.class b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByName.class new file mode 100644 index 0000000..a823018 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/HumanComparatorByName.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/TreeNode.class b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/TreeNode.class new file mode 100644 index 0000000..ef4d3cb Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/family_tree/TreeNode.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/human/Gender.class b/out/production/GBLesson1/ru/gb/family_tree/model/human/Gender.class new file mode 100644 index 0000000..523d099 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/human/Gender.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/human/Human.class b/out/production/GBLesson1/ru/gb/family_tree/model/human/Human.class new file mode 100644 index 0000000..be626b4 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/human/Human.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/save/FileHandler.class b/out/production/GBLesson1/ru/gb/family_tree/model/save/FileHandler.class new file mode 100644 index 0000000..7d98089 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/save/FileHandler.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/save/Writable.class b/out/production/GBLesson1/ru/gb/family_tree/model/save/Writable.class new file mode 100644 index 0000000..eb45446 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/save/Writable.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/model/save/tree.txt b/out/production/GBLesson1/ru/gb/family_tree/model/save/tree.txt new file mode 100644 index 0000000..63ff6a5 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/model/save/tree.txt differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/presenter/Presenter.class b/out/production/GBLesson1/ru/gb/family_tree/presenter/Presenter.class new file mode 100644 index 0000000..2cf3d93 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/presenter/Presenter.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/ConsoleUI.class b/out/production/GBLesson1/ru/gb/family_tree/view/ConsoleUI.class new file mode 100644 index 0000000..c8c6a1b Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/ConsoleUI.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/MainMenu.class b/out/production/GBLesson1/ru/gb/family_tree/view/MainMenu.class new file mode 100644 index 0000000..e9d658c Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/MainMenu.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/View.class b/out/production/GBLesson1/ru/gb/family_tree/view/View.class new file mode 100644 index 0000000..068d836 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/View.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddChildern.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddChildern.class new file mode 100644 index 0000000..2ef9c25 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddChildern.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddHuman.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddHuman.class new file mode 100644 index 0000000..d0c6796 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/AddHuman.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/Command.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/Command.class new file mode 100644 index 0000000..c81c14f Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/Command.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/Finish.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/Finish.class new file mode 100644 index 0000000..998d834 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/Finish.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/GetInfo.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/GetInfo.class new file mode 100644 index 0000000..610c6a7 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/GetInfo.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/LoadTree.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/LoadTree.class new file mode 100644 index 0000000..181772e Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/LoadTree.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/SaveTree.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SaveTree.class new file mode 100644 index 0000000..f528466 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SaveTree.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/SetWedding.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SetWedding.class new file mode 100644 index 0000000..78c7e5e Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SetWedding.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByBirthDate.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByBirthDate.class new file mode 100644 index 0000000..c279e0c Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByBirthDate.class differ diff --git a/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByName.class b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByName.class new file mode 100644 index 0000000..8f5a1d4 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/family_tree/view/commands/SortByName.class differ diff --git a/out/production/GBLesson1/ru/gb/vending_machine/Main.class b/out/production/GBLesson1/ru/gb/vending_machine/Main.class new file mode 100644 index 0000000..714d51d Binary files /dev/null and b/out/production/GBLesson1/ru/gb/vending_machine/Main.class differ diff --git a/out/production/GBLesson1/ru/gb/vending_machine/products/Bottle.class b/out/production/GBLesson1/ru/gb/vending_machine/products/Bottle.class new file mode 100644 index 0000000..e304bf9 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/vending_machine/products/Bottle.class differ diff --git a/out/production/GBLesson1/ru/gb/vending_machine/products/Product.class b/out/production/GBLesson1/ru/gb/vending_machine/products/Product.class new file mode 100644 index 0000000..f9aa4fe Binary files /dev/null and b/out/production/GBLesson1/ru/gb/vending_machine/products/Product.class differ diff --git a/out/production/GBLesson1/ru/gb/vending_machine/vending/VendingMachine.class b/out/production/GBLesson1/ru/gb/vending_machine/vending/VendingMachine.class new file mode 100644 index 0000000..01a3899 Binary files /dev/null and b/out/production/GBLesson1/ru/gb/vending_machine/vending/VendingMachine.class differ diff --git a/src/ru/gb/family_tree/Main.java b/src/ru/gb/family_tree/Main.java new file mode 100644 index 0000000..275ca64 --- /dev/null +++ b/src/ru/gb/family_tree/Main.java @@ -0,0 +1,19 @@ +package ru.gb.family_tree; + +import ru.gb.family_tree.model.family_tree.FamilyTree; +import ru.gb.family_tree.model.human.Gender; +import ru.gb.family_tree.model.human.Human; +import ru.gb.family_tree.model.save.FileHandler; +import ru.gb.family_tree.model.save.Writable; +import ru.gb.family_tree.view.ConsoleUI; +import ru.gb.family_tree.view.View; + +import java.time.LocalDate; + +public class Main { + public static void main(String[] args) { + View view = new ConsoleUI(); + view.start(); + + } +} diff --git a/src/ru/gb/family_tree/model/FamilyTreeManager.java b/src/ru/gb/family_tree/model/FamilyTreeManager.java new file mode 100644 index 0000000..1541046 --- /dev/null +++ b/src/ru/gb/family_tree/model/FamilyTreeManager.java @@ -0,0 +1,25 @@ +package ru.gb.family_tree.model; + +import ru.gb.family_tree.model.family_tree.FamilyTree; +import ru.gb.family_tree.model.save.Writable; + +public class FamilyTreeManager { + private final Writable writable; + + + // Внедрение зависимости через конструктор + public FamilyTreeManager(Writable writable) { + this.writable = writable; + } + + // Метод для загрузки FamilyTree из файла + public FamilyTree load(String filePath) { + return (FamilyTree) writable.read(filePath); + } + + // Метод для сохранения FamilyTree в файл + public void save(FamilyTree familyTree, String filePath) { + writable.save(familyTree, filePath); + } +} + diff --git a/src/ru/gb/family_tree/model/HumanFactory.java b/src/ru/gb/family_tree/model/HumanFactory.java new file mode 100644 index 0000000..ed12e20 --- /dev/null +++ b/src/ru/gb/family_tree/model/HumanFactory.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.model; + +import ru.gb.family_tree.model.human.Gender; +import ru.gb.family_tree.model.human.Human; + +import java.time.LocalDate; + +public class HumanFactory implements PersonFactory { + @Override + public Human createPerson(String name, Gender gender, LocalDate birthDate) { + return new Human(name, gender, birthDate); + } +} + diff --git a/src/ru/gb/family_tree/model/PersonFactory.java b/src/ru/gb/family_tree/model/PersonFactory.java new file mode 100644 index 0000000..bfe1dc9 --- /dev/null +++ b/src/ru/gb/family_tree/model/PersonFactory.java @@ -0,0 +1,10 @@ +package ru.gb.family_tree.model; + +import ru.gb.family_tree.model.family_tree.TreeNode; +import ru.gb.family_tree.model.human.Gender; + +import java.time.LocalDate; + +public interface PersonFactory > { + T createPerson(String name, Gender gender, LocalDate birthDate); +} \ No newline at end of file diff --git a/src/ru/gb/family_tree/model/Service.java b/src/ru/gb/family_tree/model/Service.java new file mode 100644 index 0000000..28b279b --- /dev/null +++ b/src/ru/gb/family_tree/model/Service.java @@ -0,0 +1,73 @@ +package ru.gb.family_tree.model; + +import ru.gb.family_tree.model.family_tree.FamilyTree; +import ru.gb.family_tree.model.family_tree.TreeNode; +import ru.gb.family_tree.model.human.Gender; +import ru.gb.family_tree.model.save.FileHandler; +import ru.gb.family_tree.model.save.Writable; + +import java.time.LocalDate; + +public class Service> { + private FamilyTree familyTree; + private PersonFactory factory; + + Writable fileHandler = new FileHandler(); + FamilyTreeManager manager = new FamilyTreeManager(fileHandler); + private String filePath = "src/ru/gb/family_tree/model/save/tree.txt"; + + public Service(PersonFactory factory) { + this.factory = factory; + familyTree = new FamilyTree<>(); + } + + public void addHuman(String name, String gender, LocalDate date) { + T human = factory.createPerson(name, getGenderFromString(gender), date); + familyTree.addHuman(human); + } + + public void addChildren(int idParent, int idChild){ + familyTree.getById(idParent).addChild(familyTree.getById(idChild)); + familyTree.getById(idParent).getSpouse().addChild(familyTree.getById(idChild)); + familyTree.getById(idChild).addParent(familyTree.getById(idParent).getSpouse()); + familyTree.getById(idChild).addParent(familyTree.getById(idParent)); + } + + public String getHumanInfo(){ + String answer = familyTree.getInfo(); + return answer; + } + + public void sotrByName() { + familyTree.sortByName(); + } + + public void sortByBirthDate() { + familyTree.sortByBirthDate(); + } + + public void saveFemaleTree() { + manager.save(familyTree, filePath); + } + + public void loadFemaleTree() { + familyTree = manager.load(filePath); + } + + public void setWedding(int idMale, int idFemale) { + familyTree.setWedding(idMale, idFemale); + } + + public static Gender getGenderFromString(String input) { + if (input == null) { + return null; + } + try { + return Gender.valueOf(input); + } catch (IllegalArgumentException e) { + // Если введенная строка не соответствует ни одной из констант + return null; + } + } + +} diff --git a/src/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.java b/src/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.java new file mode 100644 index 0000000..8172f8a --- /dev/null +++ b/src/ru/gb/family_tree/model/family_tree/FamiliTreeIterator.java @@ -0,0 +1,24 @@ +package ru.gb.family_tree.model.family_tree; + +import java.util.Iterator; +import java.util.List; + +public class FamiliTreeIterator> implements Iterator { + private List list; + private int index; + + public FamiliTreeIterator(List list) { + this.list = list; + index = 0; + } + + @Override + public boolean hasNext() { + return index < list.size(); + } + + @Override + public T next() { + return list.get(index++); + } +} diff --git a/src/ru/gb/family_tree/model/family_tree/FamilyTree.java b/src/ru/gb/family_tree/model/family_tree/FamilyTree.java new file mode 100644 index 0000000..d82646c --- /dev/null +++ b/src/ru/gb/family_tree/model/family_tree/FamilyTree.java @@ -0,0 +1,152 @@ +package ru.gb.family_tree.model.family_tree; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class FamilyTree> implements Serializable, Iterable { + private long humansId; + private List humanList; + + public FamilyTree(){ + this(new ArrayList<>()); + } + + public FamilyTree(List humanList) { + this.humanList = humanList; + } + + public boolean addHuman(E human){ + if (human == null){ + return false; + } + if (!humanList.contains(human)){ + humanList.add(human); + human.setId(humansId++); + + addToParents(human); + addToChildern(human); + + return true; + } + return false; + } + + private void addToParents(E human){ + for (E parent: human.getParents()){ + parent.addChild(human); + } + } + + public void addToChildern(E human){ + for (E child: human.getChildren()){ + child.addParent(human); + } + } + + public List getSiblings(int id){ + E human = getById(id); + if (human == null){ + return null; + } + List res = new ArrayList<>(); + for (E parent: human.getParents()){ + for(E child: parent.getChildren()){ + if(!child.equals(human)){ + res.add(child); + } + } + } + return res; + } + + public List findByName(String name){ + List res = new ArrayList<>(); + for (E human: humanList){ + if (human.getName().equals(name)){ + res.add(human); + } + } + return res; + } + + public boolean setWedding(long humansId1, long humansId2){ + if(checkId(humansId1) && checkId(humansId2)){ + E human1 = getById(humansId1); + E human2 = getById(humansId2); + if (human1.getSpouse() == null && human2.getSpouse() == null){ + human1.setSpouse(human2); + human2.setSpouse(human1); + } else { + return false; + } + } + return false; + } + + public boolean setDivorse(long humansId1, long humansId2){ + if(checkId(humansId1) && checkId(humansId2)){ + E human1 = getById(humansId1); + E human2 = getById(humansId2); + if (human1.getSpouse() != null && human2.getSpouse() != null){ + human1.setSpouse(null); + human2.setSpouse(null); + } else { + return false; + } + } + return false; + } + + public boolean remove(long humansId){ + if(checkId(humansId)){ + E human = getById(humansId); + return humanList.remove(human); + } + return false; + } + + private boolean checkId(long id){ + return id < humansId && id >= 0; + } + + public E getById(long id){ + for (E human: humanList){ + if(human.getId() == id){ + return human; + } + } + return null; + } + + @Override + public String toString(){ + return getInfo(); + } + + public String getInfo() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("В дереве "); + stringBuilder.append(humanList.size()); + stringBuilder.append(" объектов: \n"); + for (E human : humanList) { + stringBuilder.append(human); + stringBuilder.append("\n"); + } + return stringBuilder.toString(); + } + + public void sortByName(){ + humanList.sort(new HumanComparatorByName<>()); + } + + public void sortByBirthDate(){ + humanList.sort(new HumanComparatorByBirthDate<>()); + } + + @Override + public Iterator iterator(){ + return new FamiliTreeIterator<>(humanList); + } +} diff --git a/src/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.java b/src/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.java new file mode 100644 index 0000000..138d8c6 --- /dev/null +++ b/src/ru/gb/family_tree/model/family_tree/HumanComparatorByBirthDate.java @@ -0,0 +1,11 @@ +package ru.gb.family_tree.model.family_tree; + +import java.util.Comparator; + +public class HumanComparatorByBirthDate> implements Comparator { + + @Override + public int compare(T o1, T o2){ + return o1.getBirthDate().compareTo(o2.getBirthDate()); + } +} diff --git a/src/ru/gb/family_tree/model/family_tree/HumanComparatorByName.java b/src/ru/gb/family_tree/model/family_tree/HumanComparatorByName.java new file mode 100644 index 0000000..c8fc237 --- /dev/null +++ b/src/ru/gb/family_tree/model/family_tree/HumanComparatorByName.java @@ -0,0 +1,11 @@ +package ru.gb.family_tree.model.family_tree; + +import java.util.Comparator; + +public class HumanComparatorByName> implements Comparator { + + @Override + public int compare(T o1, T o2){ + return o1.getName().compareTo(o2.getName()); + } +} \ No newline at end of file diff --git a/src/ru/gb/family_tree/model/family_tree/TreeNode.java b/src/ru/gb/family_tree/model/family_tree/TreeNode.java new file mode 100644 index 0000000..9f202c6 --- /dev/null +++ b/src/ru/gb/family_tree/model/family_tree/TreeNode.java @@ -0,0 +1,21 @@ +package ru.gb.family_tree.model.family_tree; + +import java.io.Serializable; +import java.time.LocalDate; +import java.util.List; + +public interface TreeNode extends Serializable { + void setId(long id); + long getId(); + T getFather(); + T getMather(); + boolean addChild(T human); + boolean addParent(T human); + String getName(); + LocalDate getDeathDate(); + LocalDate getBirthDate(); + List getParents(); + List getChildren(); + T getSpouse(); + void setSpouse(T human); +} diff --git a/src/ru/gb/family_tree/model/human/Gender.java b/src/ru/gb/family_tree/model/human/Gender.java new file mode 100644 index 0000000..e278515 --- /dev/null +++ b/src/ru/gb/family_tree/model/human/Gender.java @@ -0,0 +1,5 @@ +package ru.gb.family_tree.model.human; + +public enum Gender { + Male, Female +} diff --git a/src/ru/gb/family_tree/model/human/Human.java b/src/ru/gb/family_tree/model/human/Human.java new file mode 100644 index 0000000..04f11eb --- /dev/null +++ b/src/ru/gb/family_tree/model/human/Human.java @@ -0,0 +1,226 @@ +package ru.gb.family_tree.model.human; + +import ru.gb.family_tree.model.family_tree.TreeNode; + +import java.time.LocalDate; +import java.time.Period; +import java.util.ArrayList; +import java.util.List; + +public class Human implements TreeNode{ + private long id; + private String name; + private LocalDate birthDate, deathDate; + private List parents; + private List children; + private Gender gender; + 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; + parents = new ArrayList<>(); + if (father != null){ + parents.add(father); + } + if (mother != null){ + parents.add(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 mather) { + this(name, gender, birthDate, null, father, mather); + } + + public boolean addChild(Human child){ + if (!children.contains(child)){ + children.add(child); + return true; + } + return false; + } + + public boolean addParent(Human parent){ + if (!parents.contains(parent)){ + parents.add(parent); + return true; + } + return false; + } + + public Human getFather(){ + for (Human parent: parents){ + if(parent.getGender() == Gender.Male){ + return parent; + } + } + return null; + } + + public Human getMather(){ + for (Human parent: parents){ + if(parent.getGender() == Gender.Female){ + return parent; + } + } + return null; + } + + 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 String getName(){ + return name; + } + + public long getId(){ + return id; + } + + public void setId(long id){ + this.id = id; + } + + public LocalDate getBirthDate() { + return birthDate; + } + + public LocalDate getDeathDate() { + return deathDate; + } + + public void setBirthDate(LocalDate birthDate) { + this.birthDate = birthDate; + } + + public void setDeathDate(LocalDate deathDate) { + this.deathDate = deathDate; + } + + public List getParents(){ + return parents; + } + + public List getChildren(){ + return children; + } + + 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(", name: "); + sb.append(name); + sb.append(", пол: "); + sb.append(getGender()); + sb.append(", возраст: "); + sb.append(getAge()); + sb.append(", "); + sb.append(getSpouseInfo()); + sb.append(", "); + sb.append(getMatherInfo()); + 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 getMatherInfo(){ + String res = "мать: "; + Human mather = getMather(); + if (mather != null){ + res += mather.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(); + } +} diff --git a/src/ru/gb/family_tree/model/save/FileHandler.java b/src/ru/gb/family_tree/model/save/FileHandler.java new file mode 100644 index 0000000..dedf852 --- /dev/null +++ b/src/ru/gb/family_tree/model/save/FileHandler.java @@ -0,0 +1,27 @@ +package ru.gb.family_tree.model.save; + +import java.io.*; + +public class FileHandler implements Writable { + @Override + public boolean save(Serializable serializable, String filePath){ + try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath))) { + objectOutputStream.writeObject(serializable); + return true; + } catch (Exception e){ + e.printStackTrace(); + return false; + } + } + + @Override + public Object read(String filePath){ + try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath))) { + return objectInputStream.readObject(); + } catch (Exception e){ + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/ru/gb/family_tree/model/save/Writable.java b/src/ru/gb/family_tree/model/save/Writable.java new file mode 100644 index 0000000..3537de0 --- /dev/null +++ b/src/ru/gb/family_tree/model/save/Writable.java @@ -0,0 +1,8 @@ +package ru.gb.family_tree.model.save; + +import java.io.Serializable; + +public interface Writable { + boolean save(Serializable serializable, String filePath); + Object read(String filePath); +} diff --git a/src/ru/gb/family_tree/model/save/tree.txt b/src/ru/gb/family_tree/model/save/tree.txt new file mode 100644 index 0000000..63ff6a5 Binary files /dev/null and b/src/ru/gb/family_tree/model/save/tree.txt differ diff --git a/src/ru/gb/family_tree/presenter/Presenter.java b/src/ru/gb/family_tree/presenter/Presenter.java new file mode 100644 index 0000000..73a5f5f --- /dev/null +++ b/src/ru/gb/family_tree/presenter/Presenter.java @@ -0,0 +1,60 @@ +package ru.gb.family_tree.presenter; + +import ru.gb.family_tree.model.HumanFactory; +import ru.gb.family_tree.model.PersonFactory; +import ru.gb.family_tree.model.Service; +import ru.gb.family_tree.model.family_tree.FamilyTree; +import ru.gb.family_tree.model.human.Human; +import ru.gb.family_tree.model.save.FileHandler; +import ru.gb.family_tree.model.save.Writable; +import ru.gb.family_tree.view.View; + +import java.time.LocalDate; + +public class Presenter { + private View view; + private Service service; + private PersonFactory factory; + + public Presenter(View view) { + this.view = view; + factory = new HumanFactory(); + service = new Service<>(factory); + } + + + public void addHuman(String name, String gender, LocalDate date){ + service.addHuman(name, gender, date); + getHumanInfo(); + } + public void getHumanInfo(){ + view.printAnswer(service.getHumanInfo()); + } + + public void sortByName() { + service.sotrByName(); + getHumanInfo(); + } + + public void saveFemaleTree() { + service.saveFemaleTree(); + } + + public void loadFemaleTree() { + service.loadFemaleTree(); + } + + public void setWedding(int idMale, int idFemale) { + service.setWedding(idMale, idFemale); + getHumanInfo(); + } + + public void addChildren(int idParent, int idChild) { + service.addChildren(idParent, idChild); + } + + public void sortByBirthDate() { + service.sortByBirthDate(); + getHumanInfo(); + } +} diff --git a/src/ru/gb/family_tree/view/ConsoleUI.java b/src/ru/gb/family_tree/view/ConsoleUI.java new file mode 100644 index 0000000..3ec7892 --- /dev/null +++ b/src/ru/gb/family_tree/view/ConsoleUI.java @@ -0,0 +1,108 @@ +package ru.gb.family_tree.view; + +import ru.gb.family_tree.model.human.Gender; +import ru.gb.family_tree.model.human.Human; +import ru.gb.family_tree.presenter.Presenter; + +import java.time.LocalDate; +import java.util.Scanner; + +public class ConsoleUI implements View { + private Scanner scanner; + private Presenter presenter; + private boolean work; + private MainMenu mainMenu; + + public ConsoleUI() { + scanner = new Scanner(System.in); + presenter = new Presenter(this); + work = true; + mainMenu = new MainMenu(this); + } + + @Override + public void start() { + System.out.println("Создай свое семейное древо!"); + while (work) { + System.out.println(mainMenu.menu()); + String choiceStr = scanner.nextLine(); + //метод проверки на валидность + int choice = Integer.parseInt(choiceStr); + mainMenu.execute(choice); + } + + } + + public void setWedding(){ + System.out.println("Введите Id мужчины: "); + String idMaleStr = scanner.nextLine(); + int idMale = Integer.parseInt(idMaleStr); + System.out.println("Введите Id женщины: "); + String idFimaleStr = scanner.nextLine(); + int idFimale = Integer.parseInt(idFimaleStr); + presenter.setWedding(idMale, idFimale); + } + + public void loadTree() { + presenter.loadFemaleTree(); + } + + public void saveTree() { + presenter.saveFemaleTree(); + } + + public void sortByName() { + presenter.sortByName(); + } + + public void sortByBirthDate(){ + presenter.sortByBirthDate(); + } + + public void getInfo() { + presenter.getHumanInfo(); + } + + public void finish() { + work = false; + } + + + public void addHuman(){ + + System.out.println("Укажите имя"); + String name = scanner.nextLine(); + + System.out.println("Укажите пол Male или Female:"); + String genderStr = scanner.nextLine(); + + System.out.println("Укажите возраст:"); + System.out.println("Введите год рождения:"); + String yearStr = scanner.nextLine(); + int year = Integer.parseInt(yearStr); + System.out.println("Введите месяц:"); + String monthStr = scanner.nextLine(); + int month = Integer.parseInt(monthStr); + System.out.println("Введите день:"); + String dayStr = scanner.nextLine(); + int day = Integer.parseInt(dayStr); + + LocalDate date = LocalDate.of(year, month, day); + presenter.addHuman(name, genderStr, date); + } + + @Override + public void printAnswer(String answer) { + System.out.println(answer); + } + + public void addChildren() { + System.out.println("Укажите id родителя: "); + String idParentStr = scanner.nextLine(); + int idParent = Integer.parseInt(idParentStr); + System.out.println("Укажите id ребенка: "); + String idChildStr = scanner.nextLine(); + int idChild = Integer.parseInt(idChildStr); + presenter.addChildren(idParent, idChild); + } +} diff --git a/src/ru/gb/family_tree/view/MainMenu.java b/src/ru/gb/family_tree/view/MainMenu.java new file mode 100644 index 0000000..0eb40f6 --- /dev/null +++ b/src/ru/gb/family_tree/view/MainMenu.java @@ -0,0 +1,45 @@ +package ru.gb.family_tree.view; + +import ru.gb.family_tree.view.commands.*; + +import java.util.ArrayList; +import java.util.List; + +public class MainMenu { + private List commands; + + public MainMenu(ConsoleUI consoleUI) { + commands = new ArrayList<>(); + commands.add(new AddHuman(consoleUI)); + commands.add(new AddChildern(consoleUI)); + commands.add(new GetInfo(consoleUI)); + commands.add(new SetWedding(consoleUI)); + commands.add(new SortByName(consoleUI)); + commands.add(new SortByBirthDate(consoleUI)); + commands.add(new SaveTree(consoleUI)); + commands.add(new LoadTree(consoleUI)); + commands.add(new Finish(consoleUI)); + + } + + public String menu(){ + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Меню:\n"); + for (int i = 0; i < commands.size(); i++) { + stringBuilder.append(i+1); + stringBuilder.append(". "); + stringBuilder.append(commands.get(i).getDescription()); + stringBuilder.append("\n"); + } + return stringBuilder.toString(); + } + + public void execute(int choice){ + Command command = commands.get(choice - 1); + command.execute(); + } + + public int size(){ + return commands.size(); + } +} diff --git a/src/ru/gb/family_tree/view/View.java b/src/ru/gb/family_tree/view/View.java new file mode 100644 index 0000000..fcdaaae --- /dev/null +++ b/src/ru/gb/family_tree/view/View.java @@ -0,0 +1,6 @@ +package ru.gb.family_tree.view; + +public interface View { + void start(); + void printAnswer(String answer); +} diff --git a/src/ru/gb/family_tree/view/commands/AddChildern.java b/src/ru/gb/family_tree/view/commands/AddChildern.java new file mode 100644 index 0000000..e33a3c5 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/AddChildern.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class AddChildern extends Command { + public AddChildern(ConsoleUI consoleUI) { + super("Добавить ребенка: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().addChildren(); + }; + +} diff --git a/src/ru/gb/family_tree/view/commands/AddHuman.java b/src/ru/gb/family_tree/view/commands/AddHuman.java new file mode 100644 index 0000000..9fd174b --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/AddHuman.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class AddHuman extends Command { + public AddHuman(ConsoleUI consoleUI) { + super("Добавить человека: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().addHuman(); + }; + +} diff --git a/src/ru/gb/family_tree/view/commands/Command.java b/src/ru/gb/family_tree/view/commands/Command.java new file mode 100644 index 0000000..3cca3d6 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/Command.java @@ -0,0 +1,24 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public abstract class Command { + private String description; + private ConsoleUI consoleUI; + + public Command(String description, ConsoleUI consoleUI) { + this.description = description; + this.consoleUI = consoleUI; + } + + public String getDescription(){ + return description; + } + + public abstract void execute(); + + ConsoleUI getConsoleUI(){ + return consoleUI; + } +} + diff --git a/src/ru/gb/family_tree/view/commands/Finish.java b/src/ru/gb/family_tree/view/commands/Finish.java new file mode 100644 index 0000000..243597b --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/Finish.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class Finish extends Command{ + public Finish(ConsoleUI consoleUI) { + super("Закончить работу: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().finish(); + }; +} + diff --git a/src/ru/gb/family_tree/view/commands/GetInfo.java b/src/ru/gb/family_tree/view/commands/GetInfo.java new file mode 100644 index 0000000..083e128 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/GetInfo.java @@ -0,0 +1,15 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class GetInfo extends Command{ + + public GetInfo(ConsoleUI consoleUI) { + super("Получить список людей: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().getInfo(); + }; + +} \ No newline at end of file diff --git a/src/ru/gb/family_tree/view/commands/LoadTree.java b/src/ru/gb/family_tree/view/commands/LoadTree.java new file mode 100644 index 0000000..81fbbfb --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/LoadTree.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class LoadTree extends Command { + public LoadTree(ConsoleUI consoleUI) { + super("Выгрузить данный из файла: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().loadTree(); + }; + +} diff --git a/src/ru/gb/family_tree/view/commands/SaveTree.java b/src/ru/gb/family_tree/view/commands/SaveTree.java new file mode 100644 index 0000000..85991b5 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/SaveTree.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class SaveTree extends Command { + public SaveTree(ConsoleUI consoleUI) { + super("Записть дерево в файл: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().saveTree(); + }; + +} diff --git a/src/ru/gb/family_tree/view/commands/SetWedding.java b/src/ru/gb/family_tree/view/commands/SetWedding.java new file mode 100644 index 0000000..efa96db --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/SetWedding.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class SetWedding extends Command { + public SetWedding(ConsoleUI consoleUI) { + super("Сыграть свадьбу: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().setWedding(); + }; + +} diff --git a/src/ru/gb/family_tree/view/commands/SortByBirthDate.java b/src/ru/gb/family_tree/view/commands/SortByBirthDate.java new file mode 100644 index 0000000..4e2dff9 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/SortByBirthDate.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class SortByBirthDate extends Command{ + public SortByBirthDate(ConsoleUI consoleUI) { + super("Отсортировать людей по дате рождения: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().sortByBirthDate(); + }; + +} \ No newline at end of file diff --git a/src/ru/gb/family_tree/view/commands/SortByName.java b/src/ru/gb/family_tree/view/commands/SortByName.java new file mode 100644 index 0000000..ae95982 --- /dev/null +++ b/src/ru/gb/family_tree/view/commands/SortByName.java @@ -0,0 +1,14 @@ +package ru.gb.family_tree.view.commands; + +import ru.gb.family_tree.view.ConsoleUI; + +public class SortByName extends Command{ + public SortByName(ConsoleUI consoleUI) { + super("Отсортировать людей по имени: ", consoleUI); + } + @Override + public void execute(){ + getConsoleUI().sortByName(); + }; + +} diff --git a/src/ru/gb/vending_machine/Main.java b/src/ru/gb/vending_machine/Main.java index abfd7ac..d69fdfc 100644 --- a/src/ru/gb/vending_machine/Main.java +++ b/src/ru/gb/vending_machine/Main.java @@ -10,9 +10,11 @@ public static void main(String[] args) { Product product1 = new Bottle("coca-cola", 150, 1.5); Product product2 = new Product("milko", 100); + Product product3 = new Product("twix", 65); vendingMachine.addProduct(product1); vendingMachine.addProduct(product2); + vendingMachine.addProduct(product3); System.out.println(vendingMachine.getProductsInfo()); }