-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChickenFarm.php
359 lines (312 loc) · 8.71 KB
/
ChickenFarm.php
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
<?php
declare(strict_types=1);
class Barn
{
/**
* @var Chicken[]
*/
private array $chickens = [];
private DateTime $currentDate;
private int $totalNewBornChickens = 0;
private int $producedEggs = 0;
private int $fertilizedEggs = 0;
private ?Chicken $mostProducedEggsChicken = null;
private ?Chicken $mostFertilizedEggsChicken = null;
/**
* @param int $numberOfChickens
*/
public function __construct(int $numberOfChickens)
{
$this->currentDate = new DateTime('2023-01-01');
$this->addChickens($numberOfChickens);
}
/**
* Add a number of chickens to the barn.
*
* @param int $numberOfChickens
* @return $this
*/
private function addChickens(int $numberOfChickens): self
{
if ($numberOfChickens < 0) {
throw new \InvalidArgumentException(
'Number of chickens needs to be zero or larger.'
);
}
for ($i = 0; $i < $numberOfChickens; $i++) {
$this->chickens[] = new Chicken($this->currentDate);
}
return $this;
}
/**
* Simulate a certain amount of chicken days.
*
* @param int $days
* @return $this
*/
public function simulate(int $days): self
{
for ($i = 0; $i < $days; $i++) {
if ($i === 0) {
$this->resetStats();
}
foreach ($this->chickens as $chicken) {
$chicken->simulateDay();
$this->chickens = array_merge(
$this->chickens,
$chicken->getNewChickens()
);
$this->totalNewBornChickens += count($chicken->getNewChickens());
if ($i === $days - 1) {
$this->collectStats($chicken);
}
}
$this->currentDate->modify('+1 day');
}
return $this;
}
/**
* Set stats back to zero.
*
* @return void
*/
private function resetStats(): void
{
$this->producedEggs = 0;
$this->fertilizedEggs = 0;
$this->mostProducedEggsChicken = null;
$this->mostFertilizedEggsChicken = null;
}
/**
* Add chicken stats to totals.
*
* @param Chicken $chicken
* @return void
*/
private function collectStats(Chicken $chicken): void
{
$this->producedEggs += $chicken->getProducedEggs();
$this->fertilizedEggs += $chicken->getFertilizedEggs();
if ($this->mostProducedEggsChicken === null ||
$chicken->getProducedEggs() > $this->mostProducedEggsChicken->getProducedEggs()) {
$this->mostProducedEggsChicken = $chicken;
}
if ($this->mostFertilizedEggsChicken === null ||
$chicken->getFertilizedEggs() > $this->mostFertilizedEggsChicken->getFertilizedEggs()) {
$this->mostFertilizedEggsChicken = $chicken;
}
}
/**
* Get total produced eggs for simulated period.
*
* @return int
*/
public function getTotalProducedEggs(): int
{
return $this->producedEggs;
}
/**
* Get total fertilized eggs for simulated period.
*
* @return int
*/
public function getTotalFertilizedEggs(): int
{
return $this->fertilizedEggs;
}
/**
* Get chicken with the most produced eggs.
*
* @return Chicken|null
*/
public function getMostProducedEggsChicken(): ?Chicken
{
return $this->mostProducedEggsChicken;
}
/**
* Get chicken with the most fertilized eggs.
*
* @return Chicken|null
*/
public function getMostFertilizedEggsChicken(): ?Chicken
{
return $this->mostFertilizedEggsChicken;
}
/**
* Get total revenue formatted in euros for unfertilized eggs.
*
* @return string
*/
public function getTotalRevenue(): string
{
$unfertilizedEggs = $this->getTotalProducedEggs() - $this->getTotalFertilizedEggs();
$revenue = $unfertilizedEggs * 0.25;
$numberFormatter = new NumberFormatter("nl", NumberFormatter::CURRENCY);
return $numberFormatter->formatCurrency($revenue, "EUR");
}
/**
* Get the total amount of newborn chickens.
*
* @return int
*/
public function getTotalNewBornChickens(): int
{
return $this->totalNewBornChickens;
}
}
class Chicken
{
private const DEFAULT_BIRTH_MONTH = 4;
private const DEFAULT_BIRTH_YEAR = 2022;
private DateTime $currentDate;
private string $id;
private DateTime $dob;
private int $eggsProduced = 0;
private int $eggsFertilized = 0;
/**
* @var Chicken[]
*/
private array $newChickens = [];
/**
* @param DateTime $currentDate
* @param DateTime|null $dob
*/
public function __construct(DateTime $currentDate, ?DateTime $dob = null)
{
if ($dob === null) {
$this->initDob();
} else {
$this->dob = $dob;
}
$this->initId();
$this->currentDate = $currentDate;
}
/**
* Get chicken ID.
*
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* Set chicken ID.
*
* @return void
*/
private function initId(): void
{
$this->id = $this->dob->format('Ymd-') . mt_rand(0, 9999);
}
/**
* Generate random date of birth for chicken.
*
* @return void
*/
private function initDob(): void
{
$daysInMonth = cal_days_in_month(
CAL_GREGORIAN,
self::DEFAULT_BIRTH_MONTH,
self::DEFAULT_BIRTH_YEAR
);
$day = mt_rand(1, $daysInMonth);
$date = new \DateTime();
$date->setDate(
self::DEFAULT_BIRTH_YEAR,
self::DEFAULT_BIRTH_MONTH,
$day
);
$this->dob = $date;
}
/**
* Get age of the chicken in months.
*
* @return int
*/
private function getAgeInMonths(): int
{
$dateInterval = $this->dob->diff($this->currentDate);
return $dateInterval->y * 12 + $dateInterval->m;
}
/**
* Simulate a chicken day.
*
* @return void
*/
public function simulateDay(): void
{
if ($this->getAgeInMonths() < 4) {
return;
}
$this->newChickens = [];
$producedEggs = mt_rand(0, 2);
$this->eggsProduced += $producedEggs;
if ($this->getAgeInMonths() < 8) {
return;
}
for ($i = 0; $i < $producedEggs; $i++) {
if (mt_rand(0, 99) > 50) {
$this->eggsFertilized++;
$this->newChickens[] = new Chicken(
$this->currentDate,
$this->currentDate
);
}
}
}
/**
* Get produced eggs.
*
* @return int
*/
public function getProducedEggs(): int
{
return $this->eggsProduced;
}
/**
* Get fertilized eggs.
*
* @return int
*/
public function getFertilizedEggs(): int
{
return $this->eggsFertilized;
}
/**
* Get newly hatched chickens.
*
* @return Chicken[]
*/
public function getNewChickens(): array
{
return $this->newChickens;
}
}
$barn = new Barn(50);
$barn->simulate(365);
echo "How many eggs are produced in the measured period ? \n";
echo sprintf("Eggs produced: %s \n\n", $barn->getTotalProducedEggs());
echo "How many eggs will be fertilized in the measured period ( The chance for a fertilized
egg will be 50% ) ? \n";
echo sprintf("Eggs fertilized: %s \n\n", $barn->getTotalFertilizedEggs());
echo "Which of the chickens produced the most eggs in the measured period ? \n";
echo sprintf(
"Chicken with ID %s produced the most eggs with a number of %s eggs. \n\n",
$barn->getMostProducedEggsChicken()->getId(),
$barn->getMostProducedEggsChicken()->getProducedEggs()
);
echo "Which of the chickens fertilized the most eggs in the measured period ? \n";
echo sprintf(
"Chicken with ID %s fertilized the most eggs with a number of %s eggs. \n\n",
$barn->getMostFertilizedEggsChicken()->getId(),
$barn->getMostFertilizedEggsChicken()->getProducedEggs()
);
echo "What will be the total revenue when the unfertilized eggs will be sold for 0.25 cent
each ? \n";
echo sprintf("Total revenue: %s euro \n\n", $barn->getTotalRevenue());
echo "How many new chickens will be born in the measured period ( a new born chicken
can lay eggs 4 months after his egg was produced and can have fertilized eggs 8
months after his egg was produced ) ? \n";
echo sprintf("Newborn chickens: %s \n\n", $barn->getTotalNewBornChickens());