diff --git a/TDD/Families/Families.cs b/TDD/Families/Families.cs index 1f2d6a4..c11bd9b 100644 --- a/TDD/Families/Families.cs +++ b/TDD/Families/Families.cs @@ -28,79 +28,191 @@ public class Person : IPerson readonly string _id = Guid.NewGuid().ToString(); public string Id => _id; + private string _name; + public Gender _gender; + private IPerson? _father; + private IPerson? _mother; + + 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; + this._gender = gender; } - 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 (!_brothers.Any(b => b.Id == brother.Id)) + _brothers.Add(brother); + + 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) { - throw new NotImplementedException(); + if (!_daughters.Any(d => d.Id == daughter.Id)) + _daughters.Add(daughter); + + if (this._gender == Gender.Male) + { + daughter.setFather(this); + } + else + { + 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) { - throw new NotImplementedException(); + if (!_sisters.Any(s => s.Id == sister.Id)) + _sisters.Add(sister); + + 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) { - throw new NotImplementedException(); + if (!_sons.Any(s => s.Id == son.Id)) + _sons.Add(son); + + if (this._gender == Gender.Male) + { + son.setFather(this); + } + else + { + son.setMother(this); + } + + foreach (var bro in _sons) + { + if (bro.Id != son.Id) + son.addBrother(bro); + } + + foreach (var sister in _daughters) + { + son.addSister(sister); + } } public IEnumerable getBrothers() { - throw new NotImplementedException(); + return _brothers; } public IEnumerable getChildren() { - throw new NotImplementedException(); + return _sons.Concat(_daughters); } public IEnumerable getDaughters() { - throw new NotImplementedException(); + return _daughters; } public IPerson? getFather() { - throw new NotImplementedException(); + return this._father; } public IPerson? getMother() { - throw new NotImplementedException(); + return this._mother; } public IEnumerable getSisters() { - throw new NotImplementedException(); + return _sisters; } public IEnumerable getSons() { - throw new NotImplementedException(); + return _sons; } public void setFather(IPerson father) { - throw new NotImplementedException(); + _father = father; + + if (this._gender == Gender.Male) + { + if (!father.getSons().Any(s => s.Id == this.Id)) + father.addSon(this); + } + else + { + if (!father.getDaughters().Any(d => d.Id == this.Id)) + father.addDaughter(this); + } } public void setMother(IPerson mother) { - throw new NotImplementedException(); + _mother = mother; + + if (this._gender == Gender.Male) + { + if (!mother.getSons().Any(s => s.Id == this.Id)) + mother.addSon(this); + } + else + { + if (!mother.getDaughters().Any(d => d.Id == this.Id)) + mother.addDaughter(this); + } } } } 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; } } } 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]