-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBacteria.cpp
63 lines (57 loc) · 1.83 KB
/
Bacteria.cpp
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
#include "Bacteria.h"
typedef enum
{
OUT_ID = 0, OUT_AGE, OUT_FOOD
} OutputEnum;
const std::string OUTPUT_FIELDS[] = { "Bacteria ID: ", "Age: ", "Food left: " };
Bacteria::Bacteria(int the_bacteriaId, int the_eatingRate, int the_adultAge,
int the_foodUnits) : _currentAge(0), _consumedFood(0),
_bacteriaId(the_bacteriaId),_eatingRate(the_eatingRate),
_adultAge(the_adultAge),_foodUnits(the_foodUnits),
_timeUnitWithoutFood(0)
{}
Bacteria::Bacteria(const Bacteria &original) : _currentAge(0),_consumedFood(0),
_bacteriaId(original._bacteriaId), _eatingRate(original._eatingRate),
_adultAge(original._adultAge),
_foodUnits(original._foodUnits), _timeUnitWithoutFood(0)
{}
Bacteria& Bacteria::operator=(const Bacteria &rightHandSide)
{
if (this == &rightHandSide) return *this; //check for self assignment
_bacteriaId = rightHandSide._bacteriaId;
_eatingRate = rightHandSide._eatingRate;
_adultAge = rightHandSide._adultAge;
_foodUnits = rightHandSide._foodUnits;
_currentAge = rightHandSide._currentAge;
_consumedFood = rightHandSide._consumedFood;
_timeUnitWithoutFood = rightHandSide._timeUnitWithoutFood;
return *this;
}
void Bacteria::eat(int& foodLeft)
{
if (_currentAge % _eatingRate == ZERO) // time to eat
{
if (foodLeft == ZERO)
{
_timeUnitWithoutFood++;
}
else if (_foodUnits <= foodLeft) // the bacteria can eat according to its need
{
_consumedFood += _foodUnits;
foodLeft -= _foodUnits;
//_timeUnitWithoutFood = ZERO;
}
else /** the bacteria can eat only the food that remained and not according to
its need*/
{
_consumedFood += foodLeft;
foodLeft = ZERO;
//_timeUnitWithoutFood = ZERO;
}
}
}
void Bacteria::print() const
{
std::cout << OUTPUT_FIELDS[OUT_ID] << _bacteriaId << " "
<< OUTPUT_FIELDS[OUT_AGE] << _currentAge << std::endl;
}