Skip to content

Commit b815239

Browse files
Add Gender in IPerson
1 parent 6256b9d commit b815239

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

TDD/Families/Families.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IPerson
99
{
1010
string Id { get; }
1111
string Name { get; set; }
12+
Gender Gender { get; }
1213
void setFather(IPerson father);
1314
void setMother(IPerson mother);
1415
void addBrother(IPerson brother);
@@ -29,22 +30,18 @@ public class Person : IPerson
2930
public string Id => _id;
3031

3132
private string _name;
32-
33-
private IPerson _father;
34-
35-
private HashSet<IPerson> _daughters = new HashSet<IPerson>();
33+
private IPerson? _father;
34+
private IPerson? _mother;
35+
private Gender _gender;
3636

3737
private HashSet<IPerson> _children = new HashSet<IPerson>();
38-
3938
private HashSet<IPerson> _brothers = new HashSet<IPerson>();
40-
4139
private HashSet<IPerson> _sisters = new HashSet<IPerson>();
4240

43-
4441
public Person(string name, Gender gender)
4542
{
4643
this._name = name;
47-
44+
this._gender = gender;
4845
}
4946

5047
public string Name
@@ -53,6 +50,12 @@ public string Name
5350
set => _name = value;
5451
}
5552

53+
public Gender Gender
54+
{
55+
get => _gender;
56+
set => _gender = value;
57+
}
58+
5659
public void addBrother(IPerson brother)
5760
{
5861
if (!this.getBrothers().Contains(brother))
@@ -64,23 +67,27 @@ public void addBrother(IPerson brother)
6467

6568
public void addDaughter(IPerson daughter)
6669
{
67-
this._daughters.Add(daughter);
68-
this._children.Add(daughter);
69-
70+
if (!this._children.Contains(daughter))
71+
{
72+
this._children.Add(daughter);
73+
}
7074
}
7175

7276
public void addSister(IPerson sister)
7377
{
7478
if (!this.getSisters().Contains(sister))
7579
this._sisters.Add(sister);
7680

77-
if (sister.getBrothers().Contains(this))
81+
if (!sister.getBrothers().Contains(this))
7882
sister.addBrother(this);
7983
}
8084

8185
public void addSon(IPerson son)
8286
{
83-
throw new NotImplementedException();
87+
if (!this._children.Contains(son))
88+
{
89+
this._children.Add(son);
90+
}
8491
}
8592

8693
public IEnumerable<IPerson> getBrothers()
@@ -95,7 +102,7 @@ public IEnumerable<IPerson> getChildren()
95102

96103
public IEnumerable<IPerson> getDaughters()
97104
{
98-
throw new NotImplementedException();
105+
return _children.Where(c => c.Gender == Gender.Female);
99106
}
100107

101108
public IPerson? getFather()
@@ -105,7 +112,7 @@ public IEnumerable<IPerson> getDaughters()
105112

106113
public IPerson? getMother()
107114
{
108-
throw new NotImplementedException();
115+
return this._mother;
109116
}
110117

111118
public IEnumerable<IPerson> getSisters()
@@ -115,18 +122,33 @@ public IEnumerable<IPerson> getSisters()
115122

116123
public IEnumerable<IPerson> getSons()
117124
{
118-
throw new NotImplementedException();
125+
return _children.Where(c => c.Gender == Gender.Male);
119126
}
120127

121128
public void setFather(IPerson father)
122129
{
123130
this._father = father;
124-
father.addDaughter(this);
131+
if (this._gender == Gender.Male)
132+
{
133+
father.addSon(this);
134+
}
135+
else
136+
{
137+
father.addDaughter(this);
138+
}
125139
}
126140

127141
public void setMother(IPerson mother)
128142
{
129-
throw new NotImplementedException();
143+
this._mother = mother;
144+
if (this._gender == Gender.Male)
145+
{
146+
mother.addSon(this);
147+
}
148+
else
149+
{
150+
mother.addDaughter(this);
151+
}
130152
}
131153
}
132154
}

0 commit comments

Comments
 (0)