-
Notifications
You must be signed in to change notification settings - Fork 3
Implement Relationships #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Amy
Are you sure you want to change the base?
Changes from all commits
6256b9d
b815239
5d22a28
111f211
6dc80cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| { | ||
|
|
@@ -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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.