-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPool.java
443 lines (408 loc) · 13 KB
/
Pool.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import java.util.*;
/**
* This class is use to create Pool object.
* @author Zichang Liu
* @version JDK13
*/
public class Pool {
/**
* Setting default pool name.
*/
public static final String DEFAULT_POOL_NAME = "Unnamed";
/**
* Setting default pool temp.
*/
public static final double DEFAULT_POOL_TEMP_CELSIUS = 40.0;
/**
* Setting minimum pool temp.
*/
public static final double MINIMUM_POOL_TEMP_CELSIUS = 0.0;
/**
* Setting max pool temp.
*/
public static final double MAXIMUM_POOL_TEMP_CELSIUS = 100.0;
/**
* Setting neutral ph value.
*/
public static final double NEUTRAL_PH = 7.0;
/**
* Setting max ph value.
*/
public static final double MAX_PH = 14.0;
/**
* Setting min ph value.
*/
public static final double MIN_PH = 0.0;
/**
* Setting default nutrient coefficent.
*/
public static final double DEFAULT_NUTRIENT_COEFFICIENT = 0.50;
/**
* Setting minimum nutrient coefficient.
*/
public static final double MINIMUM_NUTRIENT_COEFFICIENT = 0.0;
/**
* Setting maximum nutriet coefficient.
*/
public static final double MAXIMUM_NUTRIENT_COEFFICIENT = 1.0;
/**
* Setting minimum volume for pool.
*/
public static final double MINIMUM_POOL_VOLUME = 0.0;
private static int numberOfPools = 0;
private static int poolCounter = 0;
private String name;
private double volumeLitres;
private double temperatureCelsius;
private double pH;
private double nutrientCoeffcient;
private int identificationNumber;
private ArrayList<Guppy> guppiesInPool;
private Random randomNumberGenerator;
/**
* Create a Default Pool object.
*/
public Pool() {
volumeLitres = 0.0;
name = DEFAULT_POOL_NAME;
temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
pH = NEUTRAL_PH;
nutrientCoeffcient = DEFAULT_NUTRIENT_COEFFICIENT;
guppiesInPool = new ArrayList<>();
randomNumberGenerator = new Random();
}
/**
* Create a modified Pool object.
* @param newName name for Pool
* @param newVolumeLitres volume for pool
* @param newTemperature temperature for pool
* @param newPH ph for pool
* @param newNutrientCoe nutrientCoefficient for pool
* @throws IllegalArgumentException if no name throws IAE.
*/
public Pool(final String newName, double newVolumeLitres, double newTemperature,
double newPH, double newNutrientCoe) {
if (newName.strip().equals("") || newName.equals(null)) {
throw new IllegalArgumentException("NEIN");
}
if (newVolumeLitres < MINIMUM_POOL_VOLUME) {
this.volumeLitres = MINIMUM_POOL_VOLUME;
}
if (newTemperature < MINIMUM_POOL_TEMP_CELSIUS
|| newTemperature > MAXIMUM_POOL_TEMP_CELSIUS) {
this.temperatureCelsius = DEFAULT_POOL_TEMP_CELSIUS;
}
if (newPH > MAX_PH || newPH < MIN_PH) {
this.pH = NEUTRAL_PH;
}
if (newNutrientCoe < 0.0 || newNutrientCoe > 1.0) {
this.nutrientCoeffcient = DEFAULT_NUTRIENT_COEFFICIENT;
}
this.name = newName;
this.volumeLitres = newVolumeLitres;
this.temperatureCelsius = newTemperature;
this.pH = newPH;
this.nutrientCoeffcient = newNutrientCoe;
this.guppiesInPool = new ArrayList<>();
this.randomNumberGenerator = new Random();
this.identificationNumber = poolCounter + 1;
}
/**
* Return the name of the pool.
* @return name
*/
public String getName() {
return name;
}
/**
* Return the volume of the pool.
* @return volumeLitres
*/
public double getVolumeLitres() {
return volumeLitres;
}
/**
* Return the temp for the pool.
* @return temperatureCelsius
*/
public double getTemperatureCelsius() {
return temperatureCelsius;
}
/**
* Return the ph of the pool.
* @return pH
*/
public double getpH() {
return pH;
}
/**
* Return the nutrient coefficient of the pool.
* @return nutrientCoeffcient
*/
public double getNutrientCoeffcient() {
return nutrientCoeffcient;
}
/**
* Return the id number of the pool.
* @return identificationNumber
*/
public int getidentificationNumber() {
return identificationNumber;
}
/**
* Setting the volume for the pool.
* @param volumeLitres new volume of the pool.
*/
public void setVolumeLitres(double volumeLitres) {
if (volumeLitres > 0.0) {
this.volumeLitres = volumeLitres;
}
}
/**
* setting the new temp for the pool.
* @param temperatureCelsius new temp of the pool
*/
public void setTemperatureCelsius(double temperatureCelsius) {
if (temperatureCelsius > MINIMUM_POOL_TEMP_CELSIUS
&& temperatureCelsius < MAXIMUM_POOL_TEMP_CELSIUS) {
this.temperatureCelsius = temperatureCelsius;
}
}
/**
* setting the new ph for the pool.
* @param pH new ph of the pool
*/
public void setpH(double pH) {
if (pH > MIN_PH && pH < MAX_PH) {
this.pH = pH;
}
}
/**
* setting the new nutrient coefficient of the pool.
* @param nutrientCoeffcient new nutrient coefficient of the pool.
*/
public void setNutrientCoeffcient(double nutrientCoeffcient) {
if (nutrientCoeffcient > 0.0 && nutrientCoeffcient < 1.0) {
this.nutrientCoeffcient = nutrientCoeffcient;
}
}
/**
* Changing the pool`s nutrient coefficient.
* @param delta the value add or minus to the nutrient coefficient
*/
public void changeNutrientCoeffcient(double delta) {
double changingNuCo = getNutrientCoeffcient();
changingNuCo += delta;
if (changingNuCo < MINIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoeffcient(MINIMUM_NUTRIENT_COEFFICIENT);
} else if (changingNuCo > MAXIMUM_NUTRIENT_COEFFICIENT) {
setNutrientCoeffcient(MAXIMUM_NUTRIENT_COEFFICIENT);
} else {
setNutrientCoeffcient(changingNuCo);
}
}
/**
* Changing the temp of the pool.
* @param delta the value add or minus to the temp
*/
public void changeTempretature(double delta) {
double changingTemp = getTemperatureCelsius();
changingTemp += delta;
if (changingTemp < MINIMUM_POOL_TEMP_CELSIUS) {
setTemperatureCelsius(MINIMUM_POOL_TEMP_CELSIUS);
} else if (changingTemp > MAXIMUM_POOL_TEMP_CELSIUS) {
setTemperatureCelsius(MAXIMUM_POOL_TEMP_CELSIUS);
} else {
setTemperatureCelsius(changingTemp);
}
}
/**
*Return the number of guppy in a pool.
* @return poolCounter
*/
public static int getNumberCreated() {
return poolCounter;
}
/**
*Add guppy if a guppy object is provided.
* @param guppy Guppy to be added
* @return true/false
*/
public boolean addGuppy(Guppy guppy) {
if (guppy.equals(null)) {
return false;
} else {
guppiesInPool.add(guppy);
return true;
}
}
/**
*Return the population of the Pool.
* @return guppiesInPool.size()
*/
public int getPopulation() {
return guppiesInPool.size();
}
/**
* apply new nutrient coefficient and kill guppy base on that.
* @return casualty
*/
public int applyNuCo() {
int casualty = 0;
Random killingTime = new Random();
double mayIkillthatGuppy = 0.0;
Iterator<Guppy> timeTokill = guppiesInPool.iterator();
while (timeTokill.hasNext()) {
Guppy guppy = timeTokill.next();
mayIkillthatGuppy = killingTime.nextDouble();
if (mayIkillthatGuppy > nutrientCoeffcient) {
guppy.setAlive(false);
++casualty;
}
}
return casualty;
}
/**
*Remove the guppy which is dead.
* @return howManybodyBagsdoweNeed
*/
public int removeDeadGuppies() {
int howManybodyBagsdoweNeed = 0;
Guppy looksDead;
Iterator<Guppy> knockknockYoudead = guppiesInPool.iterator();
while (knockknockYoudead.hasNext()) {
looksDead = knockknockYoudead.next();
if (!looksDead.isAlive()) {
knockknockYoudead.remove();
++howManybodyBagsdoweNeed;
}
}
return howManybodyBagsdoweNeed;
}
/**
*Return the volume the guppies need.
* @return totalVolumeNeeded / volumeConverter
*/
public double getGuppyVolumeReqInLitres() {
double totalVolumeNeeded = 0.0;
final int volumeConverter = 1000;
for (Guppy guppy : guppiesInPool) {
if (guppy.isAlive()) {
totalVolumeNeeded += guppy.getVolumeNeed();
}
}
return totalVolumeNeeded / volumeConverter;
}
/**
*Return the average age of guppy in a week.
* @return totalAge / guppiesInPool.size()
*/
public double getAverageAgeInWeeks() {
double totalAge = 0.0;
for (Guppy guppy : guppiesInPool) {
totalAge += guppy.getAgeInWeeks();
}
return totalAge / guppiesInPool.size();
}
/**
*Return the average health coefficient in the pool.
* @return totalHealthCo / guppiesInPool.size()
*/
public double getAverageHealthCoe() {
double totalHealthCo = 0.0;
for (Guppy guppy : guppiesInPool) {
totalHealthCo += guppy.getHealthCoefficient();
}
return totalHealthCo / guppiesInPool.size();
}
/**
*Return the percentage of female guppies in the pool.
* @return femalePercent
*/
public double getFemalePercent() {
double totalFemale = 0.0;
final int percentConvert = 100;
for (Guppy guppy : guppiesInPool) {
if (guppy.isFemale()) {
totalFemale += 1.0;
}
}
double femalePercent = totalFemale / guppiesInPool.size();
femalePercent *= percentConvert;
return femalePercent;
}
/**
*return median age of guppy in a pool.
* @return guppyMedian.getAgeInWeeks()
*/
public double getMedianAge() {
Guppy guppyMedian = guppiesInPool.get(guppiesInPool.size() / 2);
return guppyMedian.getAgeInWeeks();
}
/**
*Spawn guppies to the pool.
* @return totalSpawn
*/
public int spawn() {
int totalSpawn = 0;
Guppy probablyAMom;
Iterator<Guppy> babyTime = guppiesInPool.iterator();
ArrayList<Guppy> newFry = new ArrayList<>();
while (babyTime.hasNext()) {
probablyAMom = babyTime.next();
if (probablyAMom.isFemale()) {
newFry.addAll(probablyAMom.spawn());
}
}
totalSpawn += newFry.size();
guppiesInPool.addAll(newFry);
return totalSpawn;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pool)) return false;
Pool pool = (Pool) o;
return Double.compare(pool.getVolumeLitres(), getVolumeLitres()) == 0 &&
Double.compare(pool.getTemperatureCelsius(), getTemperatureCelsius()) == 0 &&
Double.compare(pool.getpH(), getpH()) == 0 &&
Double.compare(pool.getNutrientCoeffcient(), getNutrientCoeffcient()) == 0 &&
identificationNumber == pool.identificationNumber &&
Objects.equals(getName(), pool.getName()) &&
Objects.equals(guppiesInPool, pool.guppiesInPool) &&
Objects.equals(randomNumberGenerator, pool.randomNumberGenerator);
}
@Override
public int hashCode() {
return Objects.hash(getName(), getVolumeLitres(), getTemperatureCelsius(), getpH(), getNutrientCoeffcient(), identificationNumber, guppiesInPool, randomNumberGenerator);
}
/**
* Return how many guppy has died due to oldness.
* @return yoHeisDead
*/
public int incrementAges() {
int yoHeisDead = 0;
for (Guppy guppy : guppiesInPool) {
if (guppy.getAgeInWeeks() == Guppy.MAXIMUM_AGE_IN_WEEKS) {
guppy.setAlive(false);
++yoHeisDead;
} else {
guppy.incrementAge();
}
}
return yoHeisDead;
}
/**
*Return how many has died due to crowding.
* @return crowdedCasaulty
*/
public int adjustForCrowding() {
int crowdedCasaulty = 0;
while (getGuppyVolumeReqInLitres() > getVolumeLitres()) {
guppiesInPool.sort(Comparator.comparingDouble(Guppy::getHealthCoefficient));
guppiesInPool.get(crowdedCasaulty).setAlive(false);
crowdedCasaulty++;
}
return crowdedCasaulty;
}
}