-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFamily.java
More file actions
266 lines (202 loc) · 7.1 KB
/
Family.java
File metadata and controls
266 lines (202 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Family {
List <Persons> person = new ArrayList<Persons>();
// This function will set the person in question to Male
// However if they are set already to a gender it will return False!
// This function is almost exactly the same as the female function.
public boolean male(String name) {
if(ishere(name)) {
Persons checkPerson = getPerson(name);
if(checkPerson.gender =="") {
checkPerson.gender = "Male";
return true;
}
else if(checkSpouse(name).equals("Male"))
{
return false;
}
else {
return false;
}
} else {
person.add(new Persons(name));
Persons newPerson = getPerson(name);
newPerson.gender = "Male";
return true;
}
}
//Refer to male() method for comments
public boolean female(String name) {
if(ishere(name)) {
Persons checkPerson = getPerson(name);
if(checkPerson.gender == "") {
checkPerson.gender = "Female";
return true;
} else return false;
} else {
person.add(new Persons(name));
Persons newPerson = getPerson(name);
newPerson.gender = "Male";
return true;
}
}
/* Check if the person in question is a male.
* Almost exactly same code for isFemale. */
public boolean isMale(String name) {
if(ishere(name)) { //Check if person is there
Persons person2 = getPerson(name); //Get the person and store in person2.
if(person2.gender.equals("Male")) { //If their gender is male, return true;
return true;
}
else { return false; } //If not return false; Will return if no gender assigned.
}
else { //If no person is there, create one and return false anyway.
person.add(new Persons(name));
return false;
}
}
//Refer to isMale() method for comments
public boolean isFemale(String name) {
Persons person2 = getPerson(name);
if(person2.gender.equals("Female")) {
return true;
}
else { return false; }
}
public boolean setParentOf(String childName, String parentName) {
if(ishere(parentName)) {
Persons parent = getPerson(parentName);
String gender = parent.gender;
if(ishere(childName)) {
Persons child = getPerson(childName);
if(isChild(parentName, childName)) {
return false;
}
else {
if(child.parentsList.size() == 0 ) { //If parents = 0, add the next parent.
child.parentsList.add(parent);
parent.childList.add(child);
return true;
}
else if (child.parentsList.size() == 1) { //If parents = 1, check the gender of the first parent.
if(child.parentsList.get(0).gender.equals("")){ //If gender == null, add new parent
child.parentsList.add(parent);
parent.childList.add(child);
child.parentsList.get(0).spouse.add(parent);
parent.spouse.add(child.parentsList.get(0));
return true;
} else if(!child.parentsList.get(0).gender.equals(parent.gender)){ // ELSE If Parent1 gender != new parent gender , add parent
child.parentsList.add(parent);
parent.childList.add(child);
child.parentsList.get(0).spouse.add(parent);
parent.spouse.add(child.parentsList.get(0));
return true;
}
else { //if genders are the same, return false;
return false;
}
}
else return false;
}
}
else { //Else add the child, and set their parent to the parentName + gender.
person.add(new Persons(childName));
Persons child = getPerson(childName);
parent.childList.add(child);
child.parentsList.add(parent);
return true;
}
}
else { //If there is no person by the parentName, Return false as we cannot assume their gender.
person.add(new Persons(parentName));
Persons parent = getPerson(parentName);
if(ishere(childName)) { //if there is a person by the name of the child, get them and store in 'child'.
Persons child = getPerson(childName);
if(child.parentsList.size() == 0 ) { //If parents = 0, add the next parent.
child.parentsList.add(parent);
parent.childList.add(child);
return true;
}
else if (child.parentsList.size() == 1) { //If parents = 1, check the gender of the first parent.
if(child.parentsList.get(0).gender.equals("")){ //If gender == null, add new parent
child.parentsList.add(parent);
parent.childList.add(child);
child.parentsList.get(0).spouse.add(parent);
parent.spouse.add(child.parentsList.get(0));
return true;
} else if(!child.parentsList.get(0).gender.equals(parent.gender)){ // ELSE If Parent1 gender != new parent gender , add parent
child.parentsList.add(parent);
parent.childList.add(child);
child.parentsList.get(0).spouse.add(parent);
parent.spouse.add(child.parentsList.get(0));
return true;
}
else { //if genders are the same, return false;
return false;
}
}
else return false;
}
else { //Else add the child, and set their parent to the parentName + gender.
person.add(new Persons(childName));
Persons child = getPerson(childName);
child.parentsList.add(parent);
parent.childList.add(child);
return true;
}
}
}
public String[] getParentsOf(String name) {
String[] parents = new String[2];
if(ishere(name)) {
Persons child = getPerson(name);
List <Persons> parentList = child.parentsList;
List<String> names = parentList.stream().map(x -> x.name).sorted().collect(Collectors.toList());
parents = names.parallelStream().toArray(String[]::new);
return parents;
} else {
person.add(new Persons(name));
return parents;
}
}
public String[] getChildrenOf(String name) {
if(ishere(name)) {
Persons parent = getPerson(name);
String[] children = new String[parent.childList.size()];
List <String> names = parent.childList.stream().map(x -> x.name).sorted().collect(Collectors.toList());
children = names.stream().toArray(String[]::new);
return children;
}
else {
Persons parent = getPerson(name);
String[] children = new String[parent.childList.size()];
return children;
}
}
// Uses name to find the person within the personList and returns it to be assigned.
public Persons getPerson(String name) {
List<Persons> person2 = person.stream().filter(x -> x.name.equals(name)).collect(Collectors.toList());
return person2.get(0);
}
// This function will be used to determine whether the person has already been created!
// Return false if not
public Boolean ishere(String name) {
boolean ishe = person.stream().anyMatch(x -> x.name.equals(name));
return ishe;
}
public Boolean isChild(String childName, String parentName) {
Persons parent = getPerson(parentName);
boolean ishe = parent.childList.stream().anyMatch(x-> x.name.equals(childName));
System.out.println(ishe + "HIIIIII");
return ishe;
}
public String checkSpouse(String name) {
Persons checkP = getPerson(name);
String gender = checkP.spouse.get(0).gender;
return gender;
}
}