From 53e2c9d69a4625f6b70455ee963795ced36ae4f7 Mon Sep 17 00:00:00 2001 From: Alena Date: Sat, 7 Sep 2024 14:14:53 +0700 Subject: [PATCH 1/4] first seminar create Main.java --- src/Test.java | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 src/Test.java diff --git a/src/Test.java b/src/Test.java deleted file mode 100644 index a06540728..000000000 --- a/src/Test.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Test { -} From a48fb4ebd43649f86dbfdd791371d993a910ef13 Mon Sep 17 00:00:00 2001 From: Alena Date: Sat, 7 Sep 2024 14:18:14 +0700 Subject: [PATCH 2/4] create Main.java --- homeWorkFamilyTree.iml | 11 +++ .../homeWorkFamilyTree/FamilyTree.class | Bin 0 -> 1991 bytes .../homeWorkFamilyTree/Gender.class | Bin 0 -> 950 bytes out/production/homeWorkFamilyTree/Human.class | Bin 0 -> 3512 bytes out/production/homeWorkFamilyTree/Main.class | Bin 0 -> 1436 bytes src/FamilyTree.java | 48 +++++++++ src/Gender.java | 3 + src/Human.java | 93 ++++++++++++++++++ src/Main.java | 28 ++++++ 9 files changed, 183 insertions(+) create mode 100644 homeWorkFamilyTree.iml create mode 100644 out/production/homeWorkFamilyTree/FamilyTree.class create mode 100644 out/production/homeWorkFamilyTree/Gender.class create mode 100644 out/production/homeWorkFamilyTree/Human.class create mode 100644 out/production/homeWorkFamilyTree/Main.class create mode 100644 src/FamilyTree.java create mode 100644 src/Gender.java create mode 100644 src/Human.java create mode 100644 src/Main.java diff --git a/homeWorkFamilyTree.iml b/homeWorkFamilyTree.iml new file mode 100644 index 000000000..c90834f2d --- /dev/null +++ b/homeWorkFamilyTree.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/homeWorkFamilyTree/FamilyTree.class b/out/production/homeWorkFamilyTree/FamilyTree.class new file mode 100644 index 0000000000000000000000000000000000000000..bb34b060c18b2ef851cf7a45a7b9bacfd373a76d GIT binary patch literal 1991 zcma)6O;;OL7=CUN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UB}@--+4Xix1HdvZ1WI{|2W5*PDvow9CW1Dl7Y+RF-G-epP;#m+61z%obYha zeIo;^37vpiQo}=+jBdL zmR{j4Y}IH6dAcmh)-qoWfGk}DAY0(wQzl9NI@YM}HhHUL`7y>YK4ak=nUCZN+@WtO z%2A)R=DSEl^^eIi!pMp887`eeaDC#h`VvLVNfj)V$=jeeNw5!GN^6A@k=4j5l+wx@ z#ZxSQqTl~9X}HHKA|^dSa_FDl`w^s)HF)cEBulcO(id1veuLOo#!&2ibqvkEs4R^t xDlzP73|sk1sN8ks6gN(wM9xh$auKV literal 0 HcmV?d00001 diff --git a/out/production/homeWorkFamilyTree/Human.class b/out/production/homeWorkFamilyTree/Human.class new file mode 100644 index 0000000000000000000000000000000000000000..fb27752d05162b07619413158cf41d2bf2205073 GIT binary patch literal 3512 zcmbVP*-{%v6g`cAk;GuI3vfRNhjRf5?xdvYqZ3EeOSxCVn=TGuD$<-VH-T zAcQ998k%+NK#M@bL}Asi1%jz%E0fNd?6xM$d*!x-+_Fi9po>#p@m{>}S13C`kVT!Ce?y?lol1dr1zgNd0wYX@=#U}KjU&DZo z7zPD)Y}Jr8sGlvu&dPnj}B0Bd*A&P2x$GK|DrMlliETvDVBmp1@%ZM|2#; zv7+_2szqtn@ysO9*4JNglyAHso)l=FcA{y+HKL17!A?tMiF;C@qwIB0e562!nYl0$ z2uSr#>o|k6JjHU&oZ&h&$nN4PDh?+7`9u>&F{a_1j&Yo)8_UKhiqMwJMt<6Sg|Wqu zx+Zr_>UbI#1nO-yy;TF>Sdz??j%m!$Ek-s=j|mJ_TGdx|yY{|1i6AZsh_FPO)iEat zYF7T-YSvv3;)=jd+NT(+MH?k>IE<^frs2Ad8+dL@jucI{;IKJjyiVAaOW*|nZ3Mg*lu3nMkSc9-FB>hGqc?$31-WzwPYJ^A;)a# z@=%mBWn!N6Vlb@GZr!`*JXNMF>;n1 z`;+zVvPBbw@}@iG=U}+cCneG3!nn#mfhP7xOEsoJxllmo>{v0S1Uh{!su)TveTi7* z+SAP8Y^lC@%OmB&(ou~3DkM*^;>5(yUW9s)JZ_{|Ce{VM zt_&Vu&^vI#u+tec-;;Ell|nZ0z%bsjac(_p))2p5flwp!e27salbK~1V_fT#wr%D{ zGe$md<~4jK(7R2iT2wWBA+YbkqO4J#UO2!qTgP+{^2=+Ey*hb^sg{?PYFYnP%WFxs zyn9s3Yeu!aP9dm4s4IDxHw`G6~9_RIZ++`OxsGk8nlGw^mUv^Vy;HcKUoe)w#O2$3IuV z!c8CV6iq8nZIrr4%P8`Z19bD`y$WHsCsktJC@*_r2&~k`s<4oUwU1c){{c%nO=4wA zSYtjfec*e9V|VdL9p<9_FvW*p{kx z`0BM2)|*@7YzTGDzeCgb1`v1|uhgL4T}?evllppzy3}7%-$S-h8eOtK?z4|>;>33L zr^)mT^YJV?`D&Lgk?fl**zfV#cUH4+EwO*KCV`$R0zEYejFZ555||)?$^T2>wGx4Q zWGTIJ;XV?qM^7bbiSS+UU&hqnU5p6aLX@2efuGPc9ivN6Y$7=yi(Hh=?Z1AFZQ}AR zCR5}YHNT1ZvcnIu)&fa>r`D^2^;|Dy;rkl+@JP%Yy$JL7H i*y|q|z?(#?!&`WpZII~i@%Iis;V8`Br}!LSqU&$N^2td6 literal 0 HcmV?d00001 diff --git a/out/production/homeWorkFamilyTree/Main.class b/out/production/homeWorkFamilyTree/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..b2aed6d1f0bcea61b936ccd20b2e4e9ca74cc15e GIT binary patch literal 1436 zcmZ`(O>-MX5PfT}v@3at9XWPT0+@sVQWQs!1PoCKU^{Y5)*oOaFyU(~jiq%-yVQD} zs$4j6=f;5x7fw`hAO$i-syK4sC-7sayxz59I~b{Ix94?FcfU71`}e)2Qr3b!nrDJ?E8 zmc3&FYB`+7(+-LXXKQ#kn zc#S36hfAC)?TdJrc^)%(!@-*h*HIfB!_kyv_mvdrvdxKO#49(1)>{fU@wNrUnzMcs zchVC)Jt+Ewlv4LyDLVT^(saZqgCw0-xFtj+>AcZtQgJLO+!hgY6tPa+M1sDja7V&( z3_n1Z6qX0*Qkcd%SXFpmGPt_cN|cweT~k<>P&W?K$#Br%G%abL-0iur#i1JY{J<>l z_BKtlt~X`O3dzJabmYr=AGG6+zr%y~WHXiJNJ^5)(%t_KTUmsl%ZKF1f#>EdT* f(3EQm-(eF?sz1X#En35@Vfb&trhkjkLufw&FQP*9 literal 0 HcmV?d00001 diff --git a/src/FamilyTree.java b/src/FamilyTree.java new file mode 100644 index 000000000..75a5557bf --- /dev/null +++ b/src/FamilyTree.java @@ -0,0 +1,48 @@ +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class FamilyTree { + private List familyTree; + + public FamilyTree() { + this.familyTree = new ArrayList<>(); + } + + public void addHuman(Human human) { + familyTree.add(human); + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + for(Human human:familyTree) { + stringBuilder.append(human.toString()); + stringBuilder.append("\n"); + } + return stringBuilder.toString(); + } + + public String findByName(String name) { + String humanToFind = "Поиск не дал результата"; + for(Human human:familyTree) { + if(human.getFirstName().equalsIgnoreCase(name)) { + humanToFind = human.toString(); + } + if(human.getSecondName().equalsIgnoreCase(name)) { + humanToFind = human.toString(); + } + } + return humanToFind; + } + + public String findByBirthDate(LocalDate birthDate) { + String humanToFind = "Поиск не дал результата"; + for(Human human:familyTree) { + if(human.getBirthDate().equals(birthDate)) { + humanToFind = human.toString(); + } + } + return humanToFind; + } +} diff --git a/src/Gender.java b/src/Gender.java new file mode 100644 index 000000000..07c453d6c --- /dev/null +++ b/src/Gender.java @@ -0,0 +1,3 @@ +public enum Gender { + male, female; +} diff --git a/src/Human.java b/src/Human.java new file mode 100644 index 000000000..4b4031308 --- /dev/null +++ b/src/Human.java @@ -0,0 +1,93 @@ +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class Human { + private String firstName; + private String secondName; + private LocalDate birthDate; + private LocalDate deathDate; + private Gender gender; + private Human mother; + private Human father; + private List children = new ArrayList<>(); + + public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) { + this.firstName = firstName; + this.secondName = secondName; + this.birthDate = birthDate; + this.gender = gender; + this.mother = new Human(); + this.father = new Human(); + } + +public Human() {} + + public void setMother(Human mother) { + this.mother = mother; + } + public void setFather(Human father) { + this.father = father; + } + public void setChildren(Human child) { + children.add(child); + } + public void setDeathDate(LocalDate deathDate) { + this.deathDate = deathDate; + } + + public String getFirstName() { + return firstName; + } + public String getSecondName() { + return secondName; + } + public LocalDate getBirthDate() { + return birthDate; + } +// public LocalDate getDeathDate() { +// return deathDate; +// } + public String getDeathDate() { + String string; + if(deathDate == null) + string = "Is alive"; + else string = deathDate.toString(); + return string; + } + public Gender getGender() { + return gender; + } + public String getMother() { + String string; + if (mother.firstName == null) + string = "No data found"; + else string = mother.toString(); + return string; + } + public String getFather() { + String string; + if (father.firstName == null) + string = "No data found"; + else string = father.toString(); + return string; + } + public String getChildren() { + StringBuilder stringBuilder = new StringBuilder(); + for (Human child : children) { + stringBuilder.append(child.toString()); + stringBuilder.append("\n"); + } + if (stringBuilder.isEmpty()) { + stringBuilder.append("No children found"); + } + return stringBuilder.toString(); + } + + @Override + public String toString() { + return firstName + " " + secondName + + ", birth date - " + birthDate + + ", " + gender; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 000000000..9ba03ea6b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,28 @@ +import java.time.LocalDate; + +public class Main { + public static void main(String[] args) { + FamilyTree familyTree = new FamilyTree(); + Human human1 = new Human("Petr", "Petrov", LocalDate.of(1990, 1, 1), Gender.male); + Human human2 = new Human("Elena", "Petrova", LocalDate.of(1990, 2, 25), Gender.female); + Human human3 = new Human("Polina", "Petrova", LocalDate.of(2010, 3, 15), Gender.female); + + human2.setChildren(human3); + System.out.println(human1.getChildren()); + System.out.println(human2.getChildren()); + + System.out.println(human2.getBirthDate()); + System.out.println(human2.getDeathDate()); + + human3.setFather(human1); + System.out.println(human3.getFather()); + System.out.println(human3.getMother()); + + familyTree.addHuman(human1); + familyTree.addHuman(human2); + familyTree.addHuman(human3); + + System.out.println(familyTree.toString()); + + } +} From 4181110e2da27b9efac51d7aeb3b48f2aaa74da4 Mon Sep 17 00:00:00 2001 From: Alena Date: Wed, 18 Sep 2024 13:55:02 +0700 Subject: [PATCH 3/4] improoved --- .../homeWorkFamilyTree/FamilyTree.class | Bin 1991 -> 2960 bytes out/production/homeWorkFamilyTree/Human.class | Bin 3512 -> 6541 bytes out/production/homeWorkFamilyTree/Main.class | Bin 1436 -> 1395 bytes src/FamilyTree.java | 55 ++++- src/Human.java | 198 ++++++++++++++---- src/Main.java | 28 ++- 6 files changed, 209 insertions(+), 72 deletions(-) diff --git a/out/production/homeWorkFamilyTree/FamilyTree.class b/out/production/homeWorkFamilyTree/FamilyTree.class index bb34b060c18b2ef851cf7a45a7b9bacfd373a76d..2613a200ae1de0914bf009b0d9830681ea44c22e 100644 GIT binary patch literal 2960 zcma)8Yf}?v6n-`dhJ=M+6i|y4R4gPQ-cV`)DGRYDjRivC{k1-tV{3 zul>MurXOlAIM$g?Kee6qA9SYQ`x_iBea`OYX8q7)vit74=Y7s|o^#Io=J$Vo{0YDi zzVO4NV8dDMg4UC_jC9Wt)6|yZM$QUCfloyh)+t!mZ)6N>P=U9j^HdPk2>20{cQp!v zquQd8UY<5}UBRY~_+6l4vZxk5Kk5|J+XiVZGv6~ccUDhY0piuGSdTEl&Xj@GmthH` zSPeF!!H-52P1vNMuH;HyE2vIpGnQs#ataz;B4vJJowEY4Ma4a`+N-5f;(Du!7Tn7i zEk-bwQlN|n(S~h)Y*(=Z?Fy=n=NGjM?>XHP17F9uqr!bEI?$=0hEUU4M+e5x5w{oE zQMVzgq6^&$0`s~xk+l|dljDv~*G63RsMuYA8D22bDO1nL{9YCN3M)r7St+xqX418# zB|XEOc9++SYv?L!$5P=kR*gqlCqY3Ge&{OBV7{Ui3XJRZ^LZ_u8=KE$O?_C)v8O6Zxj=1Jd>AUuijP|QN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Uoe)w#O2$3IuV z!c8CV6iq8nZIrr4%P8`Z19bD`y$WHsCsktJC@*_r2&~k`s<4oUwU1c){{c%nO=4wA zSYtjfec*e9V|VdL9p<9_FvW*p{kx z`0BM2)|*@7YzTGDzeCgb1`v1|uhgL4T}?evllppzy3}7%-$S-h8eOtK?z4|>;>33L zr^)mT^YJV?`D&Lgk?fl**zfV#cUH4+EwO*KCV`$R0zEYejFZ555||)?$^T2>wGx4Q zWGTIJ;XV?qM^7bbiSS+UU&hqnU5p6aLX@2efuGPc9ivN6Y$7=yi(Hh=?Z1AFZQ}AR zCR5}YHNT1ZvcnIu)&fa>r`D^2^;|Dy;rkl+@JP%Yy$JL7H i*y|q|z?(#?!&`WpZII~i@%Iis;V8`Br}!LSqU&$N^2td6 diff --git a/out/production/homeWorkFamilyTree/Main.class b/out/production/homeWorkFamilyTree/Main.class index b2aed6d1f0bcea61b936ccd20b2e4e9ca74cc15e..f0c7220aa261374866b26b2a005b66b1a6d7a7e4 100644 GIT binary patch delta 681 zcmYk3&rTCj6vltIop##k0MkkcO-L{pW1+28LD8aBsR}4c6{WVKrrHVCU`uKx#!X$h zav@G)Vq#2p?liDyjV3;TkD!m?))>zeh;F{~?>pz7@7#BxUxxAh&zEn&G zOF3k6m?Mf%sa(!JsH~Rjc2!}-=4YQgDphw#6DCJpQt+|e$d!cAFwmJaInD`1Kw7)( z|NDwAW6Il_eld_~a~VnPx1k|L|LVpRu;s z*I_8FeZ?q5#v;98{k>&aJyH>h6|f>*&OnDm+Lw%KZBmIH^tBm}C)ym#$CF)j^Abd* zASNNnbbgCWyv@0P+h*v4rl@k)8FoKKyqv7skmaAz!*hChfyGP0ye7h1_RylA4-D{; zXjf)tTMCO@7fAuB-C#+C4)cmUH|1`VJf$F>S7zU(D4r%SOmRy*A3?>kGYuW6g>|Hh8bQhRcS-4X{sCO6tD$FL90@vwFN;@{Qj82$VU*8aoVM9+#55A z3uDxUD-#!ODm7jB1N;g831hl9-p8ccyyU+7&OP_Md*0hq-}%)ae|EnE$fCKaCYe+= zEYr@i69E+}A{;Z&82rx-%l^%RBMPF3$%u2D!koR~N~BdJIFdL`q=sQFHd~##X>=Km zh9iZxzM;oEdbbg;SZ1r+$f!u+tc-IUX=LneSKMB8E62r67dRGhk%5y)wq;t)oNgHm z6XDnI$NQq~vgpY&1j7epOKWYTW@RE(A(7*_BIXbhIYW0OL}7_zS-4QhMV@fO6zB@a zs-P=G|Bn_quI;16p2O8-lsT>o37=lCFRwq-yDDTWWsq=o+UnEcZl82yI87EbP9GA%_z z(g5Kt%+3xlKfoEvq6f3{16*>jMXH7*qUGr zbRT()(J&tPX#Oc0Ek#+GKBWpjo{*0=4#C1GUJ&mk4&xP$;4OmqfN^}n1U@5#Z5$iA sPCCZ$2#-nAhcBd{lkF+I!5V7xj49HoQ|6{S3|b>KNJMx_bQc familyTree; public FamilyTree() { - this.familyTree = new ArrayList<>(); + this(new ArrayList<>()); } - public void addHuman(Human human) { - familyTree.add(human); + public FamilyTree(ArrayList humans) { + this.familyTree = humans; + } + + public boolean addHuman(Human human) { + boolean res = false; + if (!familyTree.contains(human)) { + familyTree.add(human); + this.treeId += 1; + human.setId(treeId); + addToFamily(human); + res = true; + } + return res; + } + + private void addToFamily(Human human) { + if (human.getMother() != null) + human.getMother().addChildren(human); + if (human.getFather() != null) + human.getFather().addChildren(human); + for (Human child : human.getChildren()) { + child.setParent(human); + } + } + + public boolean setWedding(Human human1, Human human2) { + boolean res = false; + if (human1.getSpouse() == null && human2.getSpouse() == null) { + human1.setSpouse(human2); + human2.setSpouse(human1); + res = true; + } + return res; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); - for(Human human:familyTree) { + for (Human human : familyTree) { stringBuilder.append(human.toString()); stringBuilder.append("\n"); } @@ -24,12 +57,12 @@ public String toString() { } public String findByName(String name) { - String humanToFind = "Поиск не дал результата"; - for(Human human:familyTree) { - if(human.getFirstName().equalsIgnoreCase(name)) { + String humanToFind = "no data found"; + for (Human human : familyTree) { + if (human.getFirstName().equalsIgnoreCase(name)) { humanToFind = human.toString(); } - if(human.getSecondName().equalsIgnoreCase(name)) { + if (human.getSecondName().equalsIgnoreCase(name)) { humanToFind = human.toString(); } } @@ -37,9 +70,9 @@ public String findByName(String name) { } public String findByBirthDate(LocalDate birthDate) { - String humanToFind = "Поиск не дал результата"; - for(Human human:familyTree) { - if(human.getBirthDate().equals(birthDate)) { + String humanToFind = "no data found"; + for (Human human : familyTree) { + if (human.getBirthDate().equals(birthDate)) { humanToFind = human.toString(); } } diff --git a/src/Human.java b/src/Human.java index 4b4031308..ac2dbe2af 100644 --- a/src/Human.java +++ b/src/Human.java @@ -1,93 +1,199 @@ import java.time.LocalDate; +import java.time.Period; import java.util.ArrayList; import java.util.List; public class Human { + private long id; private String firstName; private String secondName; private LocalDate birthDate; private LocalDate deathDate; private Gender gender; + private Human spouse; private Human mother; private Human father; private List children = new ArrayList<>(); - public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) { + public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender, + Human spouse, Human mother, Human father) { + this.id = -1; this.firstName = firstName; this.secondName = secondName; this.birthDate = birthDate; + this.deathDate = deathDate; this.gender = gender; - this.mother = new Human(); - this.father = new Human(); + this.spouse = spouse; + this.mother = mother; + this.father = father; } -public Human() {} + public Human(String firstName, String secondName, LocalDate birthDate, Gender gender, Human mother, Human father) { + this(firstName, secondName, birthDate, null, gender, null, mother, father); + } - public void setMother(Human mother) { - this.mother = mother; + public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) { + this(firstName, secondName, birthDate, null, gender, null, null, null); } - public void setFather(Human father) { - this.father = father; + + public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender) { + this(firstName, secondName, birthDate, deathDate, gender, null, null, null); + } + + public void setId(long id) { + this.id = id; + } + + public long getId() { + return id; } - public void setChildren(Human child) { - children.add(child); + + public void setParent(Human parent) { + if (parent.gender == Gender.female) { + this.mother = parent; + } else { + this.father = parent; + } } + + public void setParents(Human parent1, Human parent2) { + this.setParent(parent1); + this.setParent(parent2); + } + + public Human getMother() { + return mother; + } + + public Human getFather() { + return father; + } + + public String infoParents() { + String string1 = "mother: "; + String string2 = "father: "; + if (mother == null) + string1 = string1 + "no data found"; + else string1 = string1 + mother.getName(); + if (father == null) + string2 = string2 + "no data found"; + else string2 = string2 + father.getName(); + return string1 + "\n" + string2; + } + + public void setSpouse(Human spouse) { + this.spouse = spouse; + } + + public Human getSpouse() { + return spouse; + } + + public String infoSpouse() { + String res = "spouse: "; + if (spouse == null) + res = res + "single"; + else res = res + spouse.getName(); + return res; + } + + public void addChildren(Human child) { + if (!children.contains(child)) { + children.add(child); + } + } + + public List getChildren() { + return children; + } + + public String infoChildren() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("children: "); + if (!children.isEmpty()) { + for (Human child : children) { + stringBuilder.append(child.getName()).append(", "); + } + } else stringBuilder.append("no children found"); + return stringBuilder.toString(); + } + public void setDeathDate(LocalDate deathDate) { this.deathDate = deathDate; } + public LocalDate getBirthDate() { + return birthDate; + } + + public LocalDate getDeathDate() { + return deathDate; + } + + public String infoDeathDate() { + String res; + if (deathDate == null) + res = "is alive"; + else res = "death date: " + deathDate.toString(); + return res; + } + public String getFirstName() { return firstName; } + public String getSecondName() { return secondName; } - public LocalDate getBirthDate() { - return birthDate; + + public String getName() { + return firstName + " " + secondName; } -// public LocalDate getDeathDate() { -// return deathDate; -// } - public String getDeathDate() { - String string; - if(deathDate == null) - string = "Is alive"; - else string = deathDate.toString(); - return string; + + public String infoBirthDate() { + return "birth date: " + birthDate.toString(); } + + public String infoAge() { + Integer age; + String res; + if (deathDate == null) { + age = Period.between(birthDate, LocalDate.now()).getYears(); + res = age.toString() + " years"; + } else { + age = Period.between(birthDate, deathDate).getYears(); + res = "died at " + age.toString() + " years"; + } + return res; + } + public Gender getGender() { return gender; } - public String getMother() { - String string; - if (mother.firstName == null) - string = "No data found"; - else string = mother.toString(); - return string; - } - public String getFather() { - String string; - if (father.firstName == null) - string = "No data found"; - else string = father.toString(); - return string; - } - public String getChildren() { - StringBuilder stringBuilder = new StringBuilder(); - for (Human child : children) { - stringBuilder.append(child.toString()); - stringBuilder.append("\n"); + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; } - if (stringBuilder.isEmpty()) { - stringBuilder.append("No children found"); + if (!(obj instanceof Human)) { + return false; } - return stringBuilder.toString(); + Human human = (Human) obj; + return human.getId() == getId(); } @Override public String toString() { - return firstName + " " + secondName + - ", birth date - " + birthDate + - ", " + gender; + StringBuilder info = new StringBuilder(); + info.append("======\n").append("id ").append(getId()).append(",\n"); + info.append(getName()).append(", \n"); + info.append(infoBirthDate()).append(", \n"); + if (deathDate != null) info.append(infoDeathDate()).append(", \n"); + info.append(infoParents()).append(", \n"); + info.append(infoSpouse()).append(", \n"); + info.append(infoChildren()); + + return info.toString(); } } diff --git a/src/Main.java b/src/Main.java index 9ba03ea6b..a85aeb385 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,26 +3,24 @@ public class Main { public static void main(String[] args) { FamilyTree familyTree = new FamilyTree(); - Human human1 = new Human("Petr", "Petrov", LocalDate.of(1990, 1, 1), Gender.male); - Human human2 = new Human("Elena", "Petrova", LocalDate.of(1990, 2, 25), Gender.female); - Human human3 = new Human("Polina", "Petrova", LocalDate.of(2010, 3, 15), Gender.female); + Human petr = new Human("Petr", "Petrov", LocalDate.of(1990, 1, 1), Gender.male); + Human elena = new Human("Elena", "Ivanova", LocalDate.of(1990, 2, 25), Gender.female); + Human polina = new Human("Polina", "Petrova", LocalDate.of(2010, 3, 15), Gender.female); + Human igor = new Human("Igor", "Petrov", LocalDate.of(1950, 10, 13), LocalDate.of(2010, 8, 18), Gender.male); - human2.setChildren(human3); - System.out.println(human1.getChildren()); - System.out.println(human2.getChildren()); - System.out.println(human2.getBirthDate()); - System.out.println(human2.getDeathDate()); + elena.addChildren(polina); + petr.addChildren(polina); + petr.setParent(igor); - human3.setFather(human1); - System.out.println(human3.getFather()); - System.out.println(human3.getMother()); + familyTree.addHuman(petr); + familyTree.addHuman(elena); + familyTree.addHuman(polina); + familyTree.addHuman(igor); + familyTree.setWedding(petr, elena); - familyTree.addHuman(human1); - familyTree.addHuman(human2); - familyTree.addHuman(human3); + System.out.println(familyTree); - System.out.println(familyTree.toString()); } } From 548798aa040926b7a64d0988ad644084be3a5652 Mon Sep 17 00:00:00 2001 From: Alena Date: Fri, 20 Sep 2024 18:09:42 +0700 Subject: [PATCH 4/4] improoved, created Writable and FileHandler --- fam_tree.txt | Bin 0 -> 927 bytes .../homeWorkFamilyTree/FamilyTree.class | Bin 2960 -> 3191 bytes out/production/homeWorkFamilyTree/Human.class | Bin 6541 -> 7279 bytes out/production/homeWorkFamilyTree/Main.class | Bin 1395 -> 2409 bytes src/FamilyTree.java | 26 +- src/FileHandler.java | 24 ++ src/Human.java | 313 ++++++++++-------- src/Main.java | 24 +- src/Writable.java | 6 + 9 files changed, 241 insertions(+), 152 deletions(-) create mode 100644 fam_tree.txt create mode 100644 src/FileHandler.java create mode 100644 src/Writable.java diff --git a/fam_tree.txt b/fam_tree.txt new file mode 100644 index 0000000000000000000000000000000000000000..b168c8dc0ee62d5ee0c32c0338edcac603188f9f GIT binary patch literal 927 zcma)4J#Q015S{zB58@yLks@RT(a_mHz!g#wVsu1Ep`}=Sd(Oh$-m$lKE)hZNpQn0O#T!XpX2>+HiS>W7I6gamqO{oZKk<~yS1Gb86QOXn56^{BWR5e zMH(wE-@u$|Y{#6{90L3Z`~+jJGz5{cK~6gns?_@|L)~tzn=&~W9B3uv1QYxTmoaf# z(POhCEXzrUR0AAE$;Y{jNrR9Uo|i>FElm`d{;)Z)u)8%g5-PlZz}1Hr?=J2=*gL-o zP!&gk0}xzVR#lP;2H=>fI41T;0>3)CQD^^f))(1evQdfXp$7k1o(jq43jDh8S^%Jn zb_zPC6za9VBd6K&VS(g?XDsC_*0zZn>q~ocVcir;#+?<4w@mqWwNKgWQe<<|=xT7u zo%S2l2-hl~6)VdAe=3v47A?I-%`<96RbZFiPRb>tgF6@nqZyO=%)~gqptfaZo|}|( zAD=b-ukONKuHh_PTMWc9cVSU-Sff`r*6LPh<&Gw~c|JX(zC{q(5ut%-yK%jAX!nqo Grjoy3s^&WY literal 0 HcmV?d00001 diff --git a/out/production/homeWorkFamilyTree/FamilyTree.class b/out/production/homeWorkFamilyTree/FamilyTree.class index 2613a200ae1de0914bf009b0d9830681ea44c22e..5eebc05e0fe631655a59ea6b7d1d0f662d41ae9a 100644 GIT binary patch delta 1466 zcmaJ>U2{`a6kR7tbCYnBK#L`1N^LAfnuNAf1tV#zt*BK4<)bY+1Inchn3^V#CP={= z75o;dUKOi?4?c8!lR?W=9mhw8H(vb#{sm?_MO^#bnDW4whjaGbXP>>+UVG=vZMYo? z-2CU)KL8Bk`>TNgcQk+~UN-RxMiq?XnbIC>DieT(qXx!JOdw;QcMqnIn|M_sOh!DD zElyX%qL?)Cnj8dqa3C{Y$WKTDAExNa7g{H*lGS>wP|i;XPQkH)hdI z*T~F?%!n8YEoptHT*~IU28u;%CY_xw^`?i)Q&v9JpHhf!{r`xLv1>OxS5$|>>u|w` z7CcC$Tv&&CD9mX44E(g~;N^POzTn+dd4v7{qW}|Y5X7vulwRLNg}cPBVZ>S5FLIaI zKa7Fw*3XC$#GvO^4ulTJPdL zt^)TplS$GZcVFyhgyTzSaN!$lu6{JRVDB|r#s&Q$Rt&5s-UH;&h>h4lUj!X!LMOkv z(X1J@Itgoxdhk9zAe@U4AL1kKA2Sd2SwXjWw=#ohb;}r?T*YaH!HEbHp%HCVdNWdJ z$CKECr|@tMCq?BxVRk8DR}FDj6;W`4@PtDv@Ehn`CP{@0_*BEq`MPv#Hj)YuBat{| zO|ZCajO%1E-5MrFg+K!T3EYB%!ctg*7W;j9^rdc>4#&NS40eF#OVW z>`RNhOR)R>kKV&!I|bgsA|E4q65H`O*PXRe+sRIZlv4YuYOv?&ZWH4*f(1&M0?s;DLVlD!Qiw=;4dg%N`El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qW}N^ diff --git a/out/production/homeWorkFamilyTree/Human.class b/out/production/homeWorkFamilyTree/Human.class index 0ec2eb4f4a09888be58c865c2c554e633c2c42ae..94732c81201c549c0367435ce8cdf7dbec75f7f0 100644 GIT binary patch literal 7279 zcmbtZd0-UP8UMXxliBP}4wfqmqCr5DO(3GQVuDc!2GI?I(IAS)Bs(Mv$!^@;pmM_q~~&%qH9ZV-jZG`Q5+odvAF5 z-93*0SfLIIp#-G{0w&5(u28et+G;fqSgF3|Q+hVry*Y*QWyw@B*QQWf-_UI!s4!J8 z8qOsLnwMuX*0y*un+w4}X$8VC4OE(_LbXEKiNk|dN+HD39_a#jnC1tt!| z;j~4col4jlsuW+PTY67}i5MC=n;l9IXGz|OYt1>i5Q_{nn>a%6cJ?R)2CV@t+mR-Y z(mMy!xqdq%tlnqh{d#B<`P-Qghn9+Sj!{U}PgeYYjZ9FjsJ?j}Txknm4c#JrtBGY= z^i8(NEYh@@I1b0to>@D$)}mJ#BcXbCb3=EKQH7wRPfJ}8E7@~YX{n1a<~mHQ!ifqq z2dyo3dpgx?<<=*2{cL4(Rw|cOIIM`84U_d0Sxz!>GU7CNpY6uRwE7}TBT!f)d{0qW zT7>#!;zfy+V2y!ODf&1_obX#Q%=i{xtwU)Lr%MPXdj=Sn*_JvPTyNqGY+!&`LqiN3 zg@r|gn9R~8+Zx1K3YDpJUBb#)b(_+|se~whwuy6au0oi~w|fYSNSjFn3>u4feNUhR&zhS7(914$E`v4sE}r;uMlAM{(1M@lz0&v*(~xqOlYmL41hr$|tCnIw5+Mi4TjK zMlyTC;81Q`5Fb&PLi^{JIX(#D8nIor&KgK=B{Z(Xr3OBhPmhHFj39uH2w}V?f^u}o z;u}regqx{KDt!TCDsTA0eYDRf@hJnhnz#+y^Z8k?S!-vKX~vS#W9KfgCAqe_W}85w zDE8fG^HI{QK4ao`e3oi4rZ(7CMoeAbusVc0aHoO0One?+pe@D$usW5q`y?Y8Tdjd% z`;<)*L#zFZBm;_Keb%6!U~ zn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4WqNV%r#|&4h;hp^IpcRAa*&{{26}kqT#n@OQ}Kb z_n}bbq3{c)ZzsRr%se3|oDz%QhpNEd_>-vJjjGDMai)P@maE+{{@uD8!N48_D)(}$ zU(*Mv&qLJq5r+0YhVMAr+x?oQ1U$*QEM@dd{EAGcv32;huG$jt1_Hs5PPw&biB0*R zI9IUed0ueYSxjLcm4tLBA0pB??vC86^AW#vs@hZd`lRgBUt+PRrSgtruQw@QpPBIW z@Pw~1ewAc7r}#d~EcX~Q$r1LkkF$e%g5~x}R+pzR%#LR}dz@l@+36}CVlMoRm*3cV zAHxRbH!Alcm8-yS@jJd3vGTl1#ec7#f7H)E>*rtf^Y8lkPyPJ2e*Q;4Ush7*!ceMO Jg;W^P{{lTlUg7`% diff --git a/out/production/homeWorkFamilyTree/Main.class b/out/production/homeWorkFamilyTree/Main.class index f0c7220aa261374866b26b2a005b66b1a6d7a7e4..c6d73c3b15bad1a66756ba4f6881a8c1914ea5ce 100644 GIT binary patch literal 2409 zcmbtW+fy4=82@c?Xt4Kx-?3azaNvVo=9EN&KB>ivFu zQ2znPXP>YSp%rx;$8j8A#1|i&@y+qYH)nj*@pm=}q=Mr^VP?-c-}x@T@B4n|oP7WH zXI}z1jQ7Iu!LK5qBZ!cK$edUd$)YIDBu`Dv8Pl$U&;he#x(5~bWASknnu72#VVT9H zF~=~%P^EdZg5Ze-OO!OUCB0#Q@2_GE)qnVp0OPXuV2C&P%v=|@U|*ZBj$SvWLR7(o0g1tzl`{Rg44GUu~y%V z*XUGjkmY%ZlnqD z0^KYX>``-yte7b)*cPi1ypfeM=(voS` z>YG`v;b{ejh6a;o%7#-;u6?k!`s0UdU#u-B(|JQ=9a|pva&p$TjGXOU+BoCMg0L>K zG{%5?g?+k+XH*CsQ;o9(?To$pwB4>p=t01}= z6%C8FtnBVB$l8kx1^1o4mB3(mA5d&&bXD>5MOChQ?oe0*ep00m4r*c10e_fQ;+bbIo>l#h7webHFE`g17Kwk{tGcpJUa&qtAP zq=h?FiD;2%-Dlrm(9=muops}`{=OC5HyHA^TB8qc-ctL!0~I{nSNjGl7)W zAJ~Pz(4#c*>1)AWrGpQi?9usZA8;90xY86IOSsA|_25rDhv(VV{q*w!XMV>11}}0J zAcBwa5@$i;cmpqU79yHCyuz7E|0-U^YeXH!>)fvr<|KVdB6X}H&z=`DiQXHS@!>0| z%M@4XBY(#LF`C9bcwJQ!&mG}0`8V+v9fsw-Rdaln(EPND)*KJ_^(Jn>@*}rV%2rRK kpB@HStfc3Achw4SGhz7`^`h{g(7TM{qyKj(_u<`t0IZcn0RR91 delta 437 zcmYL_yDvjw7{-6+wC9{U4y8m=k#w+WrKn5Y>VCh~wJt?E7%Uy=AQmQ*L41?MV6zd1 zBQbR7=pXPmNNjfT_K;Yf_jzy6`+VO==*za>zF&*LD0@%#yJp+iv^liXp>lepsnem0 z4vmWAgF-rYbdWFDSoAm~=~cISliBYuz@RGX&6Pt8TZ}j&HKsoGMCAmN7E=z>%%~P$ z(zck>@Gs;J^D1bR)V$f4okf@12x=IZg7cEaC2A6lmKU@que~5xZiA0SLFadXCDM{r zu*`~7WLX;{Ad~Q@N@n4#HSBkZ$Ntc8Nkn5V-I-{shvm8?VQEN+NwF{UOe*R!{(m<` zQ1lE>H&TARu(aGPh>KIIIH#HmYPh79YeL*m#~tuiYS i2v=m-lppBfge_q{5#MK9m|srxvm?xq$*k<@?EU~L6hv(R diff --git a/src/FamilyTree.java b/src/FamilyTree.java index d2dec1108..0da835e9d 100644 --- a/src/FamilyTree.java +++ b/src/FamilyTree.java @@ -1,16 +1,18 @@ +import java.io.Serializable; +import java.lang.foreign.SegmentAllocator; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; -public class FamilyTree { +public class FamilyTree implements Serializable { static long treeId = 0; private List familyTree; public FamilyTree() { - this(new ArrayList<>()); + this(new ArrayList()); } - public FamilyTree(ArrayList humans) { + public FamilyTree(ArrayList humans) { this.familyTree = humans; } @@ -18,7 +20,7 @@ public boolean addHuman(Human human) { boolean res = false; if (!familyTree.contains(human)) { familyTree.add(human); - this.treeId += 1; + treeId += 1; human.setId(treeId); addToFamily(human); res = true; @@ -57,16 +59,20 @@ public String toString() { } public String findByName(String name) { - String humanToFind = "no data found"; + StringBuilder humanToFind = new StringBuilder(); +// String humanToFind = "no data found"; for (Human human : familyTree) { - if (human.getFirstName().equalsIgnoreCase(name)) { - humanToFind = human.toString(); + if (human.getName().equalsIgnoreCase(name)) { + humanToFind.append(human.toString()); } - if (human.getSecondName().equalsIgnoreCase(name)) { - humanToFind = human.toString(); + if (human.getFirstName().equalsIgnoreCase(name)) { + humanToFind.append(human.toString()); } + else if (human.getSecondName().equalsIgnoreCase(name)) { + humanToFind.append(human.toString()); + } else humanToFind.append("no data found"); } - return humanToFind; + return humanToFind.toString(); } public String findByBirthDate(LocalDate birthDate) { diff --git a/src/FileHandler.java b/src/FileHandler.java new file mode 100644 index 000000000..7b8a957f0 --- /dev/null +++ b/src/FileHandler.java @@ -0,0 +1,24 @@ +import java.io.*; + +public class FileHandler implements Writable{ + + @Override + public boolean save(Serializable serializable, String filePath) { + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath))) { + out.writeObject(serializable); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + @Override + public Object read(String filePath) { + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath))) { + return in.readObject(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/Human.java b/src/Human.java index ac2dbe2af..4fbbf17cd 100644 --- a/src/Human.java +++ b/src/Human.java @@ -1,9 +1,10 @@ +import java.io.Serializable; import java.time.LocalDate; import java.time.Period; import java.util.ArrayList; import java.util.List; -public class Human { +public class Human implements Serializable { private long id; private String firstName; private String secondName; @@ -16,7 +17,7 @@ public class Human { private List children = new ArrayList<>(); public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender, - Human spouse, Human mother, Human father) { + Human spouse, Human parent1, Human parent2) { this.id = -1; this.firstName = firstName; this.secondName = secondName; @@ -24,176 +25,206 @@ public Human(String firstName, String secondName, LocalDate birthDate, LocalDate this.deathDate = deathDate; this.gender = gender; this.spouse = spouse; - this.mother = mother; - this.father = father; + if (parent1 != null && parent1.gender == Gender.male) { + this.mother = parent2; + this.father = parent1; + } else { + this.mother = parent1; + this.father = parent2; + } } - public Human(String firstName, String secondName, LocalDate birthDate, Gender gender, Human mother, Human father) { - this(firstName, secondName, birthDate, null, gender, null, mother, father); - } +public Human(String firstName, String secondName, LocalDate birthDate, Gender gender, Human parent1, Human parent2) { + this(firstName, secondName, birthDate, null, gender, null, parent1, parent2); +} - public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) { - this(firstName, secondName, birthDate, null, gender, null, null, null); - } +public Human(String firstName, String secondName, LocalDate birthDate, Gender gender) { + this(firstName, secondName, birthDate, null, gender, null, null, null); +} - public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender) { - this(firstName, secondName, birthDate, deathDate, gender, null, null, null); - } +public Human(String firstName, String secondName, LocalDate birthDate, LocalDate deathDate, Gender gender) { + this(firstName, secondName, birthDate, deathDate, gender, null, null, null); +} - public void setId(long id) { - this.id = id; - } +public void setId(long id) { + this.id = id; +} - public long getId() { - return id; - } +public long getId() { + return id; +} - public void setParent(Human parent) { - if (parent.gender == Gender.female) { - this.mother = parent; - } else { - this.father = parent; - } +public void setParent(Human parent) { + if (parent.gender == Gender.female) { + this.mother = parent; + } else { + this.father = parent; } +} - public void setParents(Human parent1, Human parent2) { - this.setParent(parent1); - this.setParent(parent2); - } +public void setParents(Human parent1, Human parent2) { + this.setParent(parent1); + this.setParent(parent2); +} - public Human getMother() { - return mother; - } +public Human getMother() { + return mother; +} - public Human getFather() { - return father; - } +public Human getFather() { + return father; +} - public String infoParents() { - String string1 = "mother: "; - String string2 = "father: "; - if (mother == null) - string1 = string1 + "no data found"; - else string1 = string1 + mother.getName(); - if (father == null) - string2 = string2 + "no data found"; - else string2 = string2 + father.getName(); - return string1 + "\n" + string2; - } +public String infoParents() { + String string1 = "mother: "; + String string2 = "father: "; + if (mother == null) + string1 = string1 + "no data found"; + else string1 = string1 + mother.getName(); + if (father == null) + string2 = string2 + "no data found"; + else string2 = string2 + father.getName(); + return string1 + "\n" + string2; +} - public void setSpouse(Human spouse) { - this.spouse = spouse; + public String infoSiblings() { + List siblings = new ArrayList<>(); + StringBuilder res = new StringBuilder(); + res.append("siblings: "); + if (mother == null && father == null) { + res.append("no data found"); + } else if (mother != null || father != null) { + if (mother != null) { + for (Human child : mother.getChildren()) + if (!child.equals(this)) { + siblings.add(child); + } + } + if (father != null) { + for (Human child : father.getChildren()) + if (!child.equals(this) && !siblings.contains(child)) { + siblings.add(child); + } + } + for (Human sibling: siblings) { + res.append(sibling.getName()).append(", "); + } + } return res.toString(); } - public Human getSpouse() { - return spouse; - } +public void setSpouse(Human spouse) { + this.spouse = spouse; +} - public String infoSpouse() { - String res = "spouse: "; - if (spouse == null) - res = res + "single"; - else res = res + spouse.getName(); - return res; - } +public Human getSpouse() { + return spouse; +} - public void addChildren(Human child) { - if (!children.contains(child)) { - children.add(child); - } - } +public String infoSpouse() { + String res = "spouse: "; + if (spouse == null) + res = res + "single"; + else res = res + spouse.getName(); + return res; +} - public List getChildren() { - return children; +public void addChildren(Human child) { + if (!children.contains(child)) { + children.add(child); } +} - public String infoChildren() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("children: "); - if (!children.isEmpty()) { - for (Human child : children) { - stringBuilder.append(child.getName()).append(", "); - } - } else stringBuilder.append("no children found"); - return stringBuilder.toString(); - } +public List getChildren() { + return children; +} - public void setDeathDate(LocalDate deathDate) { - this.deathDate = deathDate; - } +public String infoChildren() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("children: "); + if (!children.isEmpty()) { + for (Human child : children) { + stringBuilder.append(child.getName()).append(", "); + } + } else stringBuilder.append("no children found"); + return stringBuilder.toString(); +} - public LocalDate getBirthDate() { - return birthDate; - } +public void setDeathDate(LocalDate deathDate) { + this.deathDate = deathDate; +} - public LocalDate getDeathDate() { - return deathDate; - } +public LocalDate getBirthDate() { + return birthDate; +} - public String infoDeathDate() { - String res; - if (deathDate == null) - res = "is alive"; - else res = "death date: " + deathDate.toString(); - return res; - } +public LocalDate getDeathDate() { + return deathDate; +} - public String getFirstName() { - return firstName; - } +public String infoDeathDate() { + String res; + if (deathDate == null) + res = "is alive"; + else res = "death date: " + deathDate.toString(); + return res; +} - public String getSecondName() { - return secondName; - } +public String getFirstName() { + return firstName; +} - public String getName() { - return firstName + " " + secondName; - } +public String getSecondName() { + return secondName; +} - public String infoBirthDate() { - return "birth date: " + birthDate.toString(); - } +public String getName() { + return firstName + " " + secondName; +} - public String infoAge() { - Integer age; - String res; - if (deathDate == null) { - age = Period.between(birthDate, LocalDate.now()).getYears(); - res = age.toString() + " years"; - } else { - age = Period.between(birthDate, deathDate).getYears(); - res = "died at " + age.toString() + " years"; - } - return res; - } +public String infoBirthDate() { + return "birth date: " + birthDate.toString(); +} - public Gender getGender() { - return gender; - } +public String infoAge() { + Integer age; + String res; + if (deathDate == null) { + age = Period.between(birthDate, LocalDate.now()).getYears(); + res = age.toString() + " years"; + } else { + age = Period.between(birthDate, deathDate).getYears(); + res = "died at " + age.toString() + " years"; + } + return res; +} - @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(); - } +public Gender getGender() { + return gender; +} - @Override - public String toString() { - StringBuilder info = new StringBuilder(); - info.append("======\n").append("id ").append(getId()).append(",\n"); - info.append(getName()).append(", \n"); - info.append(infoBirthDate()).append(", \n"); - if (deathDate != null) info.append(infoDeathDate()).append(", \n"); - info.append(infoParents()).append(", \n"); - info.append(infoSpouse()).append(", \n"); - info.append(infoChildren()); - - return info.toString(); +@Override +public boolean equals(Object obj) { + if (this == obj) { + return true; } + if (!(obj instanceof Human)) { + return false; + } + Human human = (Human) obj; + return human.getId() == getId(); +} + +@Override +public String toString() { + StringBuilder info = new StringBuilder(); + info.append("======\n").append("id ").append(getId()).append(",\n"); + info.append(getName()).append(", \n"); + info.append(infoBirthDate()).append(", \n"); + if (deathDate != null) info.append(infoDeathDate()).append(", \n"); + info.append(infoParents()).append(", \n"); + info.append(infoSpouse()).append(", \n"); + info.append(infoChildren()); + + return info.toString(); +} } diff --git a/src/Main.java b/src/Main.java index a85aeb385..125d36317 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,25 +2,47 @@ public class Main { public static void main(String[] args) { + + FamilyTree familyTree = new FamilyTree(); Human petr = new Human("Petr", "Petrov", LocalDate.of(1990, 1, 1), Gender.male); Human elena = new Human("Elena", "Ivanova", LocalDate.of(1990, 2, 25), Gender.female); Human polina = new Human("Polina", "Petrova", LocalDate.of(2010, 3, 15), Gender.female); Human igor = new Human("Igor", "Petrov", LocalDate.of(1950, 10, 13), LocalDate.of(2010, 8, 18), Gender.male); - + Human maksim = new Human("Maksim", "Petrov", LocalDate.of(2010, 3, 15), Gender.male, elena, petr); + Human petrov = new Human("Petrov", "Petro", LocalDate.of(2010, 3, 15), Gender.male, elena, petr); elena.addChildren(polina); petr.addChildren(polina); petr.setParent(igor); + familyTree.addHuman(petr); familyTree.addHuman(elena); familyTree.addHuman(polina); familyTree.addHuman(igor); + familyTree.addHuman(maksim); + familyTree.addHuman(petrov); familyTree.setWedding(petr, elena); System.out.println(familyTree); + System.out.println(polina.infoSiblings()); + + System.out.println(familyTree.findByName("Petrov")); + String filePath = "C:/Users/Алёна/IdeaProjects/homeWorkFamilyTree/fam_tree.txt"; + save(familyTree, filePath); + + } + + private static FamilyTree load(String filePath) { + Writable writable = new FileHandler(); + return (FamilyTree) writable.read(filePath); + } + + private static void save(FamilyTree tree, String filePath) { + Writable writable = new FileHandler(); + writable.save(tree, filePath); } } diff --git a/src/Writable.java b/src/Writable.java new file mode 100644 index 000000000..c830c22bc --- /dev/null +++ b/src/Writable.java @@ -0,0 +1,6 @@ +import java.io.Serializable; + +public interface Writable { + boolean save(Serializable serializable, String filePath); + Object read(String filePath); +}