-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFarm.cpp
48 lines (41 loc) · 1.3 KB
/
Farm.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
#include "Farm.h"
#include "Model.h"
#include <cassert>
#include <iostream>
using std::cout; using std::endl;
using std::string;
// the initial amount of food that a farm contains
const double initial_amount_c = 50.0;
// the amount of food that a farm produces on each update
const double production_rate_c = 2.0;
Farm::Farm (const string& in_name, Point in_location) :
Structure(in_name, in_location), amount(initial_amount_c), rate(production_rate_c)
{}
double Farm::withdraw(double amount_to_get)
{
assert(amount_to_get >= 0);
// calculate the amount of food that can be withdrawn,
// and deduct from the amount of food at hand
double amount_taken = (amount_to_get < amount)? amount_to_get : amount;
amount -= amount_taken;
Model::get_instance().notify_amount(get_name(), amount);
assert(amount >= 0);
return amount_taken;
}
void Farm::update()
{
amount += rate;
Model::get_instance().notify_amount(get_name(), amount);
cout << "Farm " << get_name() << " now has " << amount << endl;
}
void Farm::describe() const
{
cout << "Farm ";
Structure::describe();
cout << " Food available: " << amount << endl;
}
void Farm::broadcast_current_state() const
{
Structure::broadcast_current_state();
Model::get_instance().notify_amount(get_name(), amount);
}