Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 129 additions & 17 deletions TDD/Families/Families.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,79 +28,191 @@ public class Person : IPerson
readonly string _id = Guid.NewGuid().ToString();
public string Id => _id;

private string _name;
public Gender _gender;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this exercise, I won't allow this to become public. It's possible to pass all the tests without accessing this, or casting IPerson -> Person in the other code. Use the getSisters() and getBrothers() methods to achieve this.

private IPerson? _father;
private IPerson? _mother;

private HashSet<IPerson> _brothers = new HashSet<IPerson>();
private HashSet<IPerson> _sisters = new HashSet<IPerson>();
private HashSet<IPerson> _daughters = new HashSet<IPerson>();
private HashSet<IPerson> _sons = new HashSet<IPerson>();

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<IPerson> getBrothers()
{
throw new NotImplementedException();
return _brothers;
}

public IEnumerable<IPerson> getChildren()
{
throw new NotImplementedException();
return _sons.Concat(_daughters);
}

public IEnumerable<IPerson> 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<IPerson> getSisters()
{
throw new NotImplementedException();
return _sisters;
}

public IEnumerable<IPerson> 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);
}
}
}
}
132 changes: 117 additions & 15 deletions TDD/Families/Relationship.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<IPerson> 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to allow this as we are pretending that we can only access through the IPerson interface from the Relationships class

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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;
}
}
}
8 changes: 4 additions & 4 deletions TDD/Tests/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down