So I wrote two classes, the first class will eat food or buy food and store it inside a fridge. The second class creates a vector of these fridges and it can run tests on them, checking the amount of food inside each.
Right now instead of having to call the function in my second class(checkFoodAmount), I'd like for it to simply be automatically updated whenever I eat or buy food from my first class(an event.)
So my old code was 100% working, and I'm trying to edit the functions so it does this...
My #1 question is how do I initialize below in my constructor, it works before I added that and not now.
Condo::Condo(checkFoodSupply& checkF, unsigned int fridgenumber)
MY second question is, how would I create a command inside my
unsigned int Condo::eatFood()
function, which automatically would send a value to "
double foodAmountDifferent(double different);
everytime food is eaten?
I think it will work if I get those functions done, and then implement my foodAmountDifferent function to update the vector, or is my logic wrong?
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
|
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long ulong;
ulong now();
class Condo
{
public:
Condo(unsigned int fridgenumber);//total number of fridges
double foodSupply() const; // returns total amount of food in all fridges, if a fridge has food it counts as 2 food supply.
unsigned int buyFood(); //fills one fridge up
unsigned int eatFood();//empties an entire fridge
private:
unsigned int fridgez;
unsigned int fullFridges;
unsigned int emptyFridges;
};
Condo::Condo(checkFoodSupply& checkF, unsigned int fridgenumber)
:
fridgez(fridgenumber),
fullFridges(0),
emptyFridges(fridgenumber)
{}
unsigned int Condo::buyFood()
{
if(fullFridges<fridgez)
{
emptyFridges--;
fullFridges++;
return fullFridges;
}
else
return fullFridges;;
}
double Condo::foodSupply() const
{
return fullFridges*2;
}
unsigned int Condo::eatFood()
{
if(emptyFridges< fridgez)
{
emptyFridges++;
fullFridges--;
return fullFridges;
}
else
return fullFridges;
}
struct foodAmount
{
//! food amount at time of test.
double food;
ulong time;
};
class checkFoodSupply
{
public:
// Condo's being checked
checkFoodSupply(const std::vector<Condo>& Condos);
double recordFood();//record amount of food currently being contained by all the condo's and record it for later
//record of food records
double foodAmountDifferent(double different);
std::vector<foodAmount> records() const;
private:
std::vector<Condo> CondoSupply;
std::vector<foodAmount> recordsP;
};
checkFoodSupply::checkFoodSupply(const std::vector<Condo>& Condos):
CondoSupply(Condos)
{}
double foodAmountDifferent(double different){}
double checkFoodSupply::recordFood() {
double totalSupply = 0.0;
for (auto & condo : CondoSupply) //
{
totalSupply+=condo.foodSupply();
}
recordsP.push_back( {totalSupply,ulong});
return totalSupply;
}
std::vector<foodAmount> checkFoodSupply::records() const
{
return recordsP;
}
int main()
{
unsigned int fridgeNum = 10;
Condo myCondo(fridgeNum);
Condo myCondo1(11);
Condo myCondo2(12);
myCondo.buyFood();
myCondo.buyFood();
std::vector<Condo> CondoList= {myCondo,myCondo1,myCondo2};
checkFoodSupply CondoTest(CondoList);
ulong t=0;
for(t = 0; t < 10; t++)
{
CondoTest.recordFood(t);
}
std::vector<foodAmount> records;
records = CondoTest.records();
std::cout<<records[1].time<<" "<<records[1].food;
}
|