-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuppy.java
More file actions
397 lines (335 loc) · 10.6 KB
/
Copy pathGuppy.java
File metadata and controls
397 lines (335 loc) · 10.6 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
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
import java.util.ArrayList;
import java.util.Random;
/**
* This class is use to create Guppy object.
* @version JDK 13
* @author Zichang Liu
*/
public class Guppy {
//SYMBOLIC CONSTANTS HERE
/**
* Setting age for young guppy.
*/
public static final int YOUNG_FISH_AGE_IN_WEEKS = 10;
/**
* Setting age for mature guppy.
*/
public static final int MATURE_FISH_AGE_IN_WEEKS = 30;
/**
* Setting MAX age for guppy.
*/
public static final int MAXIMUM_AGE_IN_WEEKS = 50;
/**
* Setting default genus name.
*/
public static final String DEFAULT_GENUS = "Poecillia";
/**
* Setting default species name.
*/
public static final String DEFAULT_SPECIES = "reticulata";
/**
* Setting minimum water volume for guppy.
*/
public static final double MINIMUM_WATER_VOLUME_ML = 250.0;
/**
* Setting default health coefficient for guppy.
*/
public static final double DEFAULT_HEALTH_COEFFICIENT = 0.5;
/**
* Setting MINIMUM_HEALTH_COEFFICIENT for guppy.
*/
public static final double MINIMUM_HEALTH_COEFFICIENT = 0.0;
/**
* Setting value for MAXIMUM_HEALTH_COEFFICIENT for guppy.
*/
public static final double MAXIMUM_HEALTH_COEFFICIENT = 1.0;
private static int numberOfGuppiesBorn;
//INSTANCE VARIABLES
private String genus;
private String species;
private int ageInWeeks;
private int generationNumber;
private int idendificaNumber;
private double healthCoefficient;
private boolean isFemale;
private boolean isAlive;
//Incremented by 1 each time a guppy is constructed
//assign this number to idendificaNumber once new guppy is constructed
//TWO CONSTRUCTORS
//
//FIRST CONSTRUCTOR(DEFAULT), NO PARAMETER.
//*
// * Create the Guppy default constructor
public Guppy() {
numberOfGuppiesBorn++;
ageInWeeks = 0;
generationNumber = 0;
genus = DEFAULT_GENUS;
species = DEFAULT_SPECIES;
isFemale = true;
isAlive = true;
healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
this.idendificaNumber = numberOfGuppiesBorn;
}
//SECOND CONSTRUCTOR(MODIFY), SIX PARAMETERS
/**
* Modifying the Guppy Object.
* @param newGenus String represent genus
* @param newSpecies String represent species
* @param newAgeinWeeks int represent age
* @param newGenerationNumber int represent Generation number
* @param newHealthCoefficient double represent health coefficient
* @param newIsFemale boolean represent female status
*/
public Guppy(String newGenus, String newSpecies, int newAgeinWeeks, int newGenerationNumber,
double newHealthCoefficient, boolean newIsFemale) {
numberOfGuppiesBorn += 1;
//Titlecasing newGenus
if (newGenus == null) {
throw new IllegalArgumentException("Null detetced ");
} else if (newGenus.isBlank()) {
throw new IllegalArgumentException("No Genus Specified");
} else {
genus = newGenus.trim().substring(0, 1).toUpperCase()
+ newGenus.trim().substring(1).toLowerCase();
}
//Lowercasing newSpecies
if (newSpecies == null) {
throw new IllegalArgumentException("Null detected in species");
} else if (newSpecies.isBlank()) {
throw new IllegalArgumentException("blank species");
} else {
species = newSpecies.trim().toLowerCase();
}
//Checking newAgeInWeeks
if (newAgeinWeeks < 0) {
throw new IllegalArgumentException("No Neg age.");
} else {
ageInWeeks = newAgeinWeeks;
}
//assigning isFemale
isFemale = newIsFemale;
//Assigning generationNumber
if (newGenerationNumber < 0) {
generationNumber = 1;
} else {
generationNumber = newGenerationNumber;
}
//Assigning newHealthCoefficient
if (newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
throw new IllegalArgumentException("Over Max Health Coefficient");
} else if (newHealthCoefficient < MINIMUM_HEALTH_COEFFICIENT) {
throw new IllegalArgumentException("Below MIN health coefficient");
} else {
healthCoefficient = newHealthCoefficient;
}
isAlive = true;
if (newAgeinWeeks > MAXIMUM_AGE_IN_WEEKS) {
throw new IllegalArgumentException("Over Max age.");
}
}
public ArrayList<Guppy> spawn() {
// check if Guppy is female
//if female, can have babie
//Female must be 8 or older to spawn
Random funtime = new Random();
final int spawnHighBound = 100;
final int spawnAgeBound = 8;
final double spawnDecider = 0.5;
int spawnNumberOfGuppy = funtime.nextInt(spawnHighBound);
ArrayList<Guppy> babyGuppies = new ArrayList<>();
if (isFemale && getAgeInWeeks() >= spawnAgeBound) {
for (int i = 0; i < spawnNumberOfGuppy; i++) {
double shouldWeHaveSomeBabies = funtime.nextDouble();
if (shouldWeHaveSomeBabies <= spawnDecider) {
Guppy babyGuppy = new Guppy();
babyGuppies.add(babyGuppy);
int babyAmount = funtime.nextInt(spawnHighBound);
}
}
}
return babyGuppies;
}
/**
* define incrementAge().
* Increment of Guppy
*/
public void incrementAge() {
int ageCurrent = getAgeInWeeks();
ageCurrent += 1.0;
if (ageCurrent > MAXIMUM_AGE_IN_WEEKS) {
isAlive = false;
} else {
setAgeInWeeks(ageCurrent);
}
}
//getter and setter
/**
* set ageInWeeks.
* @param ageInWeeks int represent new age in weeks
*/
public void setAgeInWeeks(int ageInWeeks) {
if (ageInWeeks >= 0 && ageInWeeks <= MAXIMUM_AGE_IN_WEEKS) {
this.ageInWeeks = ageInWeeks;
}
}
/**
* set the new healthCoefficient.
* @param healthCoefficient int represent new health coefficient
*/
public void setHealthCoefficient(double healthCoefficient) {
if (healthCoefficient >= MINIMUM_HEALTH_COEFFICIENT
&& healthCoefficient <= MAXIMUM_HEALTH_COEFFICIENT) {
this.healthCoefficient = healthCoefficient;
}
}
/**
* set alive status.
* @param alive new alive boolean
*/
public void setAlive(boolean alive) {
isAlive = alive;
}
/**
* get genus.
* @return genus
*/
public String getGenus() {
return this.genus;
}
/**
* return species.
* @return species
*/
public String getSpecies() {
return species;
}
/**
* return ageInWeeks.
* @return ageInweeks
*/
public int getAgeInWeeks() {
return ageInWeeks;
}
/**
* return Generation number.
* @return generation number
*/
public int getGenerationNumber() {
return generationNumber;
}
/**
* return idendificaNumber.
* @return idendificaNumber
*/
public int getidendificaNumber() {
return idendificaNumber;
}
/**
* get healthCoefficient.
* @return healthCoefficient
*/
public double getHealthCoefficient() {
return healthCoefficient;
}
/**
* get isFemale.
* @return isFemale
*/
public boolean isFemale() {
return isFemale;
}
/**
* get isAlive.
* @return isAlive
*/
public boolean isAlive() {
return isAlive;
}
/**
* get Number of guppy created.
* @return numberOfGuppiesBorn
*/
public static int getNumberOfGuppiesBorn() {
return numberOfGuppiesBorn;
}
/**
define getVolumeNeed
* get how much volume is needed.
* @return int represent volume needed
*/
public double getVolumeNeed() {
int guppyAge = getAgeInWeeks();
//Less than 10
final double matureGuppyWaterConverter = 1.5;
if (guppyAge < YOUNG_FISH_AGE_IN_WEEKS) {
return MINIMUM_WATER_VOLUME_ML;
} else if (guppyAge >= YOUNG_FISH_AGE_IN_WEEKS && guppyAge <= MATURE_FISH_AGE_IN_WEEKS) {
return MINIMUM_WATER_VOLUME_ML * guppyAge / YOUNG_FISH_AGE_IN_WEEKS;
} else if (guppyAge > MATURE_FISH_AGE_IN_WEEKS && guppyAge <= MAXIMUM_AGE_IN_WEEKS) {
return MINIMUM_WATER_VOLUME_ML * matureGuppyWaterConverter;
} else {
return 0.0;
}
}
/**
* define changeHealthCoefficient.
* Change health coefficient
* @param delta int represent change
*/
public void changeHealthCoefficient(double delta) {
double currentHealthCoefficent = getHealthCoefficient();
currentHealthCoefficent += delta;
if (currentHealthCoefficent <= MINIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(0.0);
setAlive(false);
} else if (currentHealthCoefficent >= MAXIMUM_HEALTH_COEFFICIENT) {
setHealthCoefficient(MAXIMUM_HEALTH_COEFFICIENT);
} else {
setHealthCoefficient(currentHealthCoefficent);
}
}
//define own equals()
@Override
/*
Check if obj created from Guppy.
*/
public boolean equals(Object obj) {
//null check
if (obj == null) {
return false;
} else if (!(obj instanceof Guppy)) {
return false;
} else if (this.getAgeInWeeks() != ((Guppy) obj).getAgeInWeeks()
&& this.getGenerationNumber() != ((Guppy) obj).getGenerationNumber()
&& !(this.getGenus().equals(((Guppy) obj).getGenus()))
&& this.getHealthCoefficient() != ((Guppy) obj).getHealthCoefficient()
&& !(this.getSpecies().equals(((Guppy) obj).getSpecies()))
&& this.getVolumeNeed() != ((Guppy) obj).getVolumeNeed()
&& this.isFemale() != ((Guppy) obj).isFemale()
&& this.isAlive() != ((Guppy) obj).isAlive()
) {
return false;
} else {
return this == obj;
}
}
/**
* define own toString.
* String representation of Guppy
* @return Guppy in a String
*/
@Override
public String toString() {
return "Guppy{"
+ "genus='" + genus + '\''
+ ", species='" + species + '\''
+ ", ageInWeeks=" + ageInWeeks
+ ", generationNumber=" + generationNumber
+ ", idendificaNumber=" + idendificaNumber
+ ", healthCoefficient=" + healthCoefficient
+ ", isFemale=" + isFemale
+ ", isAlive=" + isAlive
+ '}';
}
}