From 6256b9df5409b30b776b0d4173baee64163da707 Mon Sep 17 00:00:00 2001 From: Ameneh-KM Date: Sun, 22 Jun 2025 20:24:01 +0100 Subject: [PATCH 1/5] Enhance Person class for family relationship management --- TDD/Families/Families.cs | 48 +++++++++++++++++++++++++++++++--------- TDD/Tests/SimpleTests.cs | 8 +++---- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/TDD/Families/Families.cs b/TDD/Families/Families.cs index 1f2d6a4..bda75ad 100644 --- a/TDD/Families/Families.cs +++ b/TDD/Families/Families.cs @@ -28,29 +28,54 @@ public class Person : IPerson readonly string _id = Guid.NewGuid().ToString(); public string Id => _id; + private string _name; + + private IPerson _father; + + private HashSet _daughters = new HashSet(); + + private HashSet _children = new HashSet(); + + private HashSet _brothers = new HashSet(); + + private HashSet _sisters = new HashSet(); + + public Person(string name, Gender gender) { + this._name = name; } - public string Name { - get => throw new NotImplementedException(); - set => throw new NotImplementedException(); + public string Name + { + get => _name; + set => _name = value; } public void addBrother(IPerson brother) { - throw new NotImplementedException(); + if (!this.getBrothers().Contains(brother)) + this._brothers.Add(brother); + + if (!brother.getSisters().Contains(this)) + brother.addSister(this); } public void addDaughter(IPerson daughter) { - throw new NotImplementedException(); + this._daughters.Add(daughter); + this._children.Add(daughter); + } public void addSister(IPerson sister) { - throw new NotImplementedException(); + if (!this.getSisters().Contains(sister)) + this._sisters.Add(sister); + + if (sister.getBrothers().Contains(this)) + sister.addBrother(this); } public void addSon(IPerson son) @@ -60,12 +85,12 @@ public void addSon(IPerson son) public IEnumerable getBrothers() { - throw new NotImplementedException(); + return _brothers; } public IEnumerable getChildren() { - throw new NotImplementedException(); + return _children; } public IEnumerable getDaughters() @@ -75,7 +100,7 @@ public IEnumerable getDaughters() public IPerson? getFather() { - throw new NotImplementedException(); + return this._father; } public IPerson? getMother() @@ -85,7 +110,7 @@ public IEnumerable getDaughters() public IEnumerable getSisters() { - throw new NotImplementedException(); + return _sisters; } public IEnumerable getSons() @@ -95,7 +120,8 @@ public IEnumerable getSons() public void setFather(IPerson father) { - throw new NotImplementedException(); + this._father = father; + father.addDaughter(this); } public void setMother(IPerson mother) diff --git a/TDD/Tests/SimpleTests.cs b/TDD/Tests/SimpleTests.cs index b081855..fc00ecd 100644 --- a/TDD/Tests/SimpleTests.cs +++ b/TDD/Tests/SimpleTests.cs @@ -43,10 +43,10 @@ public void TestBrotherAndSister() alice.addBrother(chuck); // Then - Assert.Contains(alice, chuck.getBrothers()); - Assert.DoesNotContain(alice, chuck.getSisters()); - Assert.Single(chuck.getBrothers()); - Assert.Empty(chuck.getSisters()); + Assert.Contains(chuck, alice.getBrothers()); + Assert.Contains(alice, chuck.getSisters()); + Assert.Single(alice.getBrothers()); + Assert.Single(chuck.getSisters()); } [Fact] From b81523990e8fba72e3fef61cef8048e5b05f3008 Mon Sep 17 00:00:00 2001 From: Ameneh-KM Date: Sun, 22 Jun 2025 22:40:51 +0100 Subject: [PATCH 2/5] Add Gender in IPerson --- TDD/Families/Families.cs | 58 +++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/TDD/Families/Families.cs b/TDD/Families/Families.cs index bda75ad..44fd5ea 100644 --- a/TDD/Families/Families.cs +++ b/TDD/Families/Families.cs @@ -9,6 +9,7 @@ public interface IPerson { string Id { get; } string Name { get; set; } + Gender Gender { get; } void setFather(IPerson father); void setMother(IPerson mother); void addBrother(IPerson brother); @@ -29,22 +30,18 @@ public class Person : IPerson public string Id => _id; private string _name; - - private IPerson _father; - - private HashSet _daughters = new HashSet(); + private IPerson? _father; + private IPerson? _mother; + private Gender _gender; private HashSet _children = new HashSet(); - private HashSet _brothers = new HashSet(); - private HashSet _sisters = new HashSet(); - public Person(string name, Gender gender) { this._name = name; - + this._gender = gender; } public string Name @@ -53,6 +50,12 @@ public string Name set => _name = value; } + public Gender Gender + { + get => _gender; + set => _gender = value; + } + public void addBrother(IPerson brother) { if (!this.getBrothers().Contains(brother)) @@ -64,9 +67,10 @@ public void addBrother(IPerson brother) public void addDaughter(IPerson daughter) { - this._daughters.Add(daughter); - this._children.Add(daughter); - + if (!this._children.Contains(daughter)) + { + this._children.Add(daughter); + } } public void addSister(IPerson sister) @@ -74,13 +78,16 @@ public void addSister(IPerson sister) if (!this.getSisters().Contains(sister)) this._sisters.Add(sister); - if (sister.getBrothers().Contains(this)) + if (!sister.getBrothers().Contains(this)) sister.addBrother(this); } public void addSon(IPerson son) { - throw new NotImplementedException(); + if (!this._children.Contains(son)) + { + this._children.Add(son); + } } public IEnumerable getBrothers() @@ -95,7 +102,7 @@ public IEnumerable getChildren() public IEnumerable getDaughters() { - throw new NotImplementedException(); + return _children.Where(c => c.Gender == Gender.Female); } public IPerson? getFather() @@ -105,7 +112,7 @@ public IEnumerable getDaughters() public IPerson? getMother() { - throw new NotImplementedException(); + return this._mother; } public IEnumerable getSisters() @@ -115,18 +122,33 @@ public IEnumerable getSisters() public IEnumerable getSons() { - throw new NotImplementedException(); + return _children.Where(c => c.Gender == Gender.Male); } public void setFather(IPerson father) { this._father = father; - father.addDaughter(this); + if (this._gender == Gender.Male) + { + father.addSon(this); + } + else + { + father.addDaughter(this); + } } public void setMother(IPerson mother) { - throw new NotImplementedException(); + this._mother = mother; + if (this._gender == Gender.Male) + { + mother.addSon(this); + } + else + { + mother.addDaughter(this); + } } } } From 5d22a281e21dc15208a28ee6d6acd6a078beed07 Mon Sep 17 00:00:00 2001 From: Ameneh-KM Date: Mon, 23 Jun 2025 19:06:00 +0100 Subject: [PATCH 3/5] remove gender from IPerson --- TDD/Families/Families.cs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/TDD/Families/Families.cs b/TDD/Families/Families.cs index 44fd5ea..e5050e2 100644 --- a/TDD/Families/Families.cs +++ b/TDD/Families/Families.cs @@ -9,7 +9,6 @@ public interface IPerson { string Id { get; } string Name { get; set; } - Gender Gender { get; } void setFather(IPerson father); void setMother(IPerson mother); void addBrother(IPerson brother); @@ -30,13 +29,17 @@ public class Person : IPerson public string Id => _id; private string _name; + private Gender _gender; private IPerson? _father; private IPerson? _mother; - private Gender _gender; private HashSet _children = new HashSet(); private HashSet _brothers = new HashSet(); private HashSet _sisters = new HashSet(); + private HashSet _daughters = new HashSet(); + private HashSet _sons = new HashSet(); + + public Person(string name, Gender gender) { @@ -50,12 +53,6 @@ public string Name set => _name = value; } - public Gender Gender - { - get => _gender; - set => _gender = value; - } - public void addBrother(IPerson brother) { if (!this.getBrothers().Contains(brother)) @@ -67,9 +64,16 @@ public void addBrother(IPerson brother) public void addDaughter(IPerson daughter) { + if (!this._daughters.Contains(daughter)) + { + this._daughters.Add(daughter); + } + if (!this._children.Contains(daughter)) { this._children.Add(daughter); + + } } @@ -84,6 +88,11 @@ public void addSister(IPerson sister) public void addSon(IPerson son) { + if (!this._sons.Contains(son)) + { + this._sons.Add(son); + } + if (!this._children.Contains(son)) { this._children.Add(son); @@ -102,7 +111,7 @@ public IEnumerable getChildren() public IEnumerable getDaughters() { - return _children.Where(c => c.Gender == Gender.Female); + return _daughters; } public IPerson? getFather() @@ -122,7 +131,7 @@ public IEnumerable getSisters() public IEnumerable getSons() { - return _children.Where(c => c.Gender == Gender.Male); + return _sons; } public void setFather(IPerson father) From 111f21110602134dccc7a09f197d98041569441f Mon Sep 17 00:00:00 2001 From: Ameneh-KM Date: Sun, 29 Jun 2025 11:59:22 +0100 Subject: [PATCH 4/5] Complete family relationship management in Families.cs --- TDD/Families/Families.cs | 111 +++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 28 deletions(-) diff --git a/TDD/Families/Families.cs b/TDD/Families/Families.cs index e5050e2..c11bd9b 100644 --- a/TDD/Families/Families.cs +++ b/TDD/Families/Families.cs @@ -29,18 +29,15 @@ public class Person : IPerson public string Id => _id; private string _name; - private Gender _gender; + public Gender _gender; private IPerson? _father; private IPerson? _mother; - private HashSet _children = new HashSet(); private HashSet _brothers = new HashSet(); private HashSet _sisters = new HashSet(); private HashSet _daughters = new HashSet(); private HashSet _sons = new HashSet(); - - public Person(string name, Gender gender) { this._name = name; @@ -55,47 +52,99 @@ public string Name public void addBrother(IPerson brother) { - if (!this.getBrothers().Contains(brother)) - this._brothers.Add(brother); + if (!_brothers.Any(b => b.Id == brother.Id)) + _brothers.Add(brother); - if (!brother.getSisters().Contains(this)) - brother.addSister(this); + if (this._gender == Gender.Male) + { + if (!brother.getBrothers().Any(b => b.Id == this.Id)) + { + brother.addBrother(this); + } + } + else + { + if (!brother.getSisters().Any(s => s.Id == this.Id)) + { + brother.addSister(this); + } + } } public void addDaughter(IPerson daughter) { - if (!this._daughters.Contains(daughter)) + if (!_daughters.Any(d => d.Id == daughter.Id)) + _daughters.Add(daughter); + + if (this._gender == Gender.Male) { - this._daughters.Add(daughter); + daughter.setFather(this); } - - if (!this._children.Contains(daughter)) + else { - this._children.Add(daughter); + daughter.setMother(this); + } + foreach (var son in _sons) + { + daughter.addBrother(son); + son.addSister(daughter); + } + foreach (var sister in _daughters) + { + if (sister.Id != daughter.Id) + { + daughter.addSister(sister); + sister.addSister(daughter); + } } } public void addSister(IPerson sister) { - if (!this.getSisters().Contains(sister)) - this._sisters.Add(sister); + if (!_sisters.Any(s => s.Id == sister.Id)) + _sisters.Add(sister); - if (!sister.getBrothers().Contains(this)) - sister.addBrother(this); + if (this._gender == Gender.Male) + { + if (!sister.getBrothers().Any(b => b.Id == this.Id)) + { + sister.addBrother(this); + } + } + else + { + if (!sister.getSisters().Any(s => s.Id == this.Id)) + { + sister.addSister(this); + } + } } public void addSon(IPerson son) { - if (!this._sons.Contains(son)) + if (!_sons.Any(s => s.Id == son.Id)) + _sons.Add(son); + + if (this._gender == Gender.Male) { - this._sons.Add(son); + son.setFather(this); + } + else + { + son.setMother(this); + } + + foreach (var bro in _sons) + { + if (bro.Id != son.Id) + son.addBrother(bro); } - if (!this._children.Contains(son)) + foreach (var sister in _daughters) { - this._children.Add(son); + son.addSister(sister); } } @@ -106,7 +155,7 @@ public IEnumerable getBrothers() public IEnumerable getChildren() { - return _children; + return _sons.Concat(_daughters); } public IEnumerable getDaughters() @@ -136,27 +185,33 @@ public IEnumerable getSons() public void setFather(IPerson father) { - this._father = father; + _father = father; + if (this._gender == Gender.Male) { - father.addSon(this); + if (!father.getSons().Any(s => s.Id == this.Id)) + father.addSon(this); } else { - father.addDaughter(this); + if (!father.getDaughters().Any(d => d.Id == this.Id)) + father.addDaughter(this); } } public void setMother(IPerson mother) { - this._mother = mother; + _mother = mother; + if (this._gender == Gender.Male) { - mother.addSon(this); + if (!mother.getSons().Any(s => s.Id == this.Id)) + mother.addSon(this); } else { - mother.addDaughter(this); + if (!mother.getDaughters().Any(d => d.Id == this.Id)) + mother.addDaughter(this); } } } From 6dc80cc60ed87de7e9c2f76ebaa04b047a355133 Mon Sep 17 00:00:00 2001 From: Ameneh-KM Date: Sun, 29 Jun 2025 14:37:27 +0100 Subject: [PATCH 5/5] Implement Relationship methods and remove unused directives --- TDD/Families/Relationship.cs | 132 +++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 15 deletions(-) diff --git a/TDD/Families/Relationship.cs b/TDD/Families/Relationship.cs index 30b06c2..c1d5232 100644 --- a/TDD/Families/Relationship.cs +++ b/TDD/Families/Relationship.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Families +namespace Families { public enum RelationshipType { @@ -26,29 +20,137 @@ public static class Relationship { public static IPerson? GetFather(IPerson person) { - throw new NotImplementedException("Method not implemented yet."); + return person.getFather(); } public static IPerson? GetMother(IPerson person) { - throw new NotImplementedException("Method not implemented yet."); + return person.getMother(); } public static IEnumerable GetSiblings(IPerson person) { - throw new NotImplementedException("Method not implemented yet."); + return person.getBrothers().Concat(person.getSisters()); } public static RelationshipType GetRelationship(IPerson person, IPerson relative) { - throw new NotImplementedException("Method not implemented yet."); - } + // Unrelated + if (person == null || relative == null) + return RelationshipType.Unrelated; + + // OtherRelationship + if (person.Id == relative.Id) + return RelationshipType.OtherRelationship; + + // Parent + if (IsParent(person, relative)) + return RelationshipType.Parent; + + // Child + if (IsParent(relative, person)) + return RelationshipType.Child; + + // Brother or Sister + if (IsSibling(person, relative)) + { + var personImpl = person as Person; + if (personImpl != null && personImpl._gender == Gender.Male) + return RelationshipType.Brother; + else + return RelationshipType.Sister; + } + + // Grandparent, Grandchild + var father = person.getFather(); + var mother = person.getMother(); + + if (father != null && (IsParent(father, relative))) + return RelationshipType.Grandparent; + + if (mother != null && (IsParent(mother, relative))) + return RelationshipType.Grandparent; + + if (IsParent(relative, father) || IsParent(relative, mother)) + return RelationshipType.Grandchild; + // Uncle or Aunt + if (father != null) + { + var siblingsOfFather = GetSiblings(father); + if (siblingsOfFather.Contains(relative)) + { + var personImpl = relative as Person; + if (personImpl != null && personImpl._gender == Gender.Male) + return RelationshipType.Uncle; + else + return RelationshipType.Aunt; + } + } + + if (mother != null) + { + var siblingsOfMother = GetSiblings(mother); + if (siblingsOfMother.Contains(relative)) + { + var personImpl = relative as Person; + if (personImpl != null && personImpl._gender == Gender.Male) + return RelationshipType.Uncle; + else + return RelationshipType.Aunt; + } + } + + // Nephew or Niece + var fatherOfRelative = relative.getFather(); + var motherOfRelative = relative.getMother(); + var siblings = GetSiblings(person); + + if (fatherOfRelative != null && motherOfRelative != null) + { + if (siblings.Any(s => s.Id == fatherOfRelative.Id) || siblings.Any(s => s.Id == motherOfRelative.Id)) + { + var personImpl = relative as Person; + if (personImpl != null && personImpl._gender == Gender.Male) + return RelationshipType.Nephew; + else + return RelationshipType.Niece; + + } + } + + // Cousin + var fatherOfPerson = GetFather(person); + var fatherOfPersonSiblings = false; + var motherOfPersonSiblings = false; + if (fatherOfPerson != null) + { + fatherOfPersonSiblings = GetSiblings(fatherOfPerson) + .Any(s => s.Id == fatherOfRelative.Id); + } + var motherOfPerson = GetMother(person); + if (motherOfPerson != null) + { + motherOfPersonSiblings = GetSiblings(motherOfPerson) + .Any(s => s.Id == motherOfRelative.Id); + } + + if (fatherOfPersonSiblings || motherOfPersonSiblings) + return RelationshipType.Cousin; + + + return RelationshipType.OtherRelationship; + } public static bool IsSibling(IPerson person1, IPerson person2) { - throw new NotImplementedException("Method not implemented yet."); + if (person1.getBrothers().Contains(person2) || + person1.getSisters().Contains(person2)) + return true; + return false; } - public static bool IsParent(IPerson person, IPerson child) { - throw new NotImplementedException("Method not implemented yet."); + if (person.getSons().Contains(child) || + person.getDaughters().Contains(child)) + return true; + return false; } } }