public List <Ape> GetChildren(ApeFamilyService familyService) { ApeFamily family = familyService.GetAll().SingleOrDefault(p => p.Partners.Contains(this)); List <Ape> result = new List <Ape>(); if (family != null) { result = family.Children; } return(result); }
public List <Ape> GetChildren(GenderType gender, ApeFamilyService familyService) { ApeFamily family = familyService.GetAll().SingleOrDefault(p => p.Partners.Contains(this)); List <Ape> result = new List <Ape>(); if (family != null) { result = family.Children.Where(c => c.GetGender() == gender).ToList(); } return(result); }
public List <Ape> GetGrandChildren(ApeFamilyService familyService) { ApeFamily family = familyService.GetAll().SingleOrDefault(p => p.Partners.Contains(this)); List <Ape> result = new List <Ape>(); if (family != null) { foreach (var r in family.Children.ToList()) { result.AddRange(r.GetChildren(familyService)); } } return(result); }
public List <Ape> CalculateDownBy2LevelRelationship(RelationshipType relationshipType, ApeFamilyService familyService) { List <Ape> result = new List <Ape>(); switch (relationshipType) { case RelationshipType.Son: result = GetGrandChildren(GenderType.Male, familyService); break; case RelationshipType.Daughter: result = GetGrandChildren(GenderType.Female, familyService); break; default: result = GetGrandChildren(familyService); break; } return(result); }
public List <Ape> GetCousins(ApeFamilyAssociationService service, ApeFamilyService familyService) { List <Ape> result = new List <Ape>(); List <Ape> fatherSideCousins = new List <Ape>(); List <Ape> motherSideCousins = new List <Ape>(); List <Ape> father = GetParent(GenderType.Male, service); List <Ape> mother = GetParent(GenderType.Female, service); foreach (var fatherSideSibling in father.ElementAt(0).GetSiblings(service)) { fatherSideCousins.AddRange(fatherSideSibling.GetChildren(familyService)); } foreach (var motherSideSibling in mother.ElementAt(0).GetSiblings(service)) { motherSideCousins.AddRange(motherSideSibling.GetChildren(familyService)); } result.AddRange(fatherSideCousins); result.AddRange(motherSideCousins); return(result); }
static void Main(string[] args) { ApeService apeService = new ApeService(); ApeFamilyService apeFamilyService = new ApeFamilyService(apeService); ApeFamilyAssociationService apeFamilyAssociationService = new ApeFamilyAssociationService(apeFamilyService); int userInput = 0; do { userInput = DisplayMenu(apeService); switch (userInput) { case 1: try { Console.WriteLine("Please enter the name of the Ape you want to find relationship for:"); string apeName = Console.ReadLine(); Console.WriteLine("Enter RelationShip Type"); string relationShipType = Console.ReadLine(); Ape ape = apeService.GetElement(apeName); Models.RelationshipType relationship; if (!Enum.TryParse(relationShipType, true, out relationship)) { throw new Exception("Invalid Relationship"); } List <Ape> apes = ape.CalculateRelationship(relationship, apeFamilyAssociationService, apeFamilyService); Utility.PrintName(apes); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("We should start from all over"); } break; case 2: try { Console.WriteLine("Please enter the name of the parent you want add the family under"); string parent = Console.ReadLine(); Console.WriteLine("Enter child name:"); string child = Console.ReadLine(); Console.WriteLine("Enter child gender:"); string genderType = Console.ReadLine(); Ape parentApe = apeService.GetElement(parent); Models.GenderType gender; if (!Enum.TryParse(genderType, true, out gender)) { throw new Exception("We are not aware of such a gender!!"); } ApeFamily apeFamily = apeFamilyService.GetAll().SingleOrDefault(p => p.Partners.Contains(parentApe)); if (apeFamily == null) { throw new Exception("That would be incorrect. Apes are not Godzillas."); } Ape newBorn = apeFamily.AddNewBorn(child, parentApe.GetDepthLevel() + 1, gender); apeFamilyAssociationService.AddElement(child, apeFamily); apeService.AddElement(newBorn.GetName(), newBorn); Console.WriteLine($"New born {newBorn.GetName()} successfully added to family"); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("We should start from all over"); } break; case 3: Utility.PrintName(apeFamilyService.GetMothersWithMaximumGirlApes()); break; case 4: try { Console.WriteLine("Please enter the name of the ape1"); string ape1Name = Console.ReadLine(); Console.WriteLine("Please enter the name of the ape1"); string ape2Name = Console.ReadLine(); Ape ape1 = apeService.GetElement(ape1Name); Ape ape2 = apeService.GetElement(ape2Name); Utility.PrintName(apeFamilyAssociationService.GetRelationshipBetweenApes(ape1, ape2)); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("We should start from all over"); } break; } } while (userInput != 5); Console.WriteLine(); }
public List <Ape> CalculateRelationship(RelationshipType relationshipType, ApeFamilyAssociationService familyAssociationService, ApeFamilyService apeFamilyService) { List <Ape> result = new List <Ape>(); switch (relationshipType) { case RelationshipType.Brother: case RelationshipType.Sister: case RelationshipType.SisterInLaw: case RelationshipType.BrotherInLaw: case RelationshipType.Cousins: result = CalculateSameLevelRelationship(relationshipType, familyAssociationService, apeFamilyService); break; case RelationshipType.Mother: case RelationshipType.Father: case RelationshipType.PaternalUncle: case RelationshipType.MaternalAunt: case RelationshipType.PaternalAunt: case RelationshipType.MaternalUncle: result = CalculateUpBy1LevelRelationship(relationshipType, familyAssociationService); break; case RelationshipType.Children: case RelationshipType.Son: case RelationshipType.Daughter: result = CalculateDownBy1LevelRelationship(relationshipType, apeFamilyService); break; case RelationshipType.GrandChildren: case RelationshipType.GrandSon: case RelationshipType.GrandDaughter: result = CalculateDownBy2LevelRelationship(relationshipType, apeFamilyService); break; } return(result); }
public List <Ape> CalculateSameLevelRelationship(RelationshipType relationshipType, ApeFamilyAssociationService familyAssociationService, ApeFamilyService familyService) { List <Ape> result = new List <Ape>(); switch (relationshipType) { case RelationshipType.Brother: result = GetSiblings(GenderType.Male, familyAssociationService); break; case RelationshipType.Sister: result = GetSiblings(GenderType.Female, familyAssociationService); break; case RelationshipType.SisterInLaw: result = GetSisterInLawOrBrotherInLaw(GenderType.Female, familyAssociationService); break; case RelationshipType.BrotherInLaw: result = GetSisterInLawOrBrotherInLaw(GenderType.Male, familyAssociationService); break; case RelationshipType.Cousins: result = GetCousins(familyAssociationService, familyService); break; default: break; } return(result); }
public void Arrange() { _apeService = new ApeService(); _apeFamilyService = new ApeFamilyService(_apeService); _apeFamilyAssociationService = new ApeFamilyAssociationService(_apeFamilyService); }