Need help with a class problem(event)

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;
}
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)
Sincle you don't need it: Just remove checkFoodSupply& checkF

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?
There is no need for this. Why do you think it is acutally necessary?


I would guess that Condo represents a fridge while checkFoodSupply is the container for fridges. So I would think that the class Condo has the members capacity and amount. Further more: buyFood() returns bool which returns false if amount exceeds the capacy. Then you need to add another fridge to the fridge container.

eatFood() is about the same just in negative direction. Is this correct?
already choosing the name Condo( which refer to a living place) when you talk about a fridge does not make sense, please change the name of the class.

make instead a struct called fridge which has a capacity and can add/remov food, you can also create a struct called food. Make a class called fridge or storage , or even warehous which contains fridges. then you have a warehouse manager .

it might get a littl more complex than what you have but it makes more sense and you would not get confusd by your own code...

divide to conquer.
It's already been suggested in your previous thread that your class names are not optimal.
Helios gave you good advice in several posts in that thread which you seem to have ignored.
http://www.cplusplus.com/forum/beginner/176945/

checkFoodSupply is not a good representation for a class. The class name implies an action. The name of a class should reflect an object. Actions should be represented by functions in your class. Since checkFoodSupply has a vector of condos, perhaps a better abstraction for the class would be a Building. A Building would be a collection of Condos.

I also think recordsP does not belong in the checkFoodSupply class. You're trying to record the same information in two places. This is what is getting you bound up in circles and is seldom a good idea. You should be concerned about adding/removing food from a fridge in a particular condo. If you want to know how much food there is in total, simply iterate through the Condos in the Building at the time you want the total.
Last edited on
I appreciate the help, but my problem is I have to do it with those function names and inputs. That's all that was given to me by my teacher, and I was told to implement all the functions and the private fields of both classes to make it work.

I realize there's probably tons of ways to do it... but I have to do it this way. We're learning about pointers, classes, forward declarations and so on so it fit's in with what we're doing.

Maybe I'm communicating something incorrectly because everyone is telling me to stop trying to do it like this...
Topic archived. No new replies allowed.