Zacząłem pisać, ale chwilowo muszę przerwac i nie wiem czy później będzie mi się chciało kończyć, więc wklejam to co mam. Jeszcze nie będzie działac, ale powinno być z górki z implementacją:
#include <vector>
#include <chrono>
#include <thread>
#include <algorithm>
#include <iostream>
using namespace std;
class Human;
class HumanNullObject;
class Chief;
class Resource {
const char *name;
public:
Resource(const char *name) :name(name) {
}
const char *getName() {
return name;
}
virtual bool isNearby() = 0;
virtual bool isChief() { return false; }
};
class Company {
Company() { }
Company(Company const&) = delete;
void operator=(Company const&) = delete;
static HumanNullObject nullHuman;
vector<Resource> resources;
public:
static Company &getInstance() {
static Company instance;
return instance;
}
void addResource(Resource &r) {
resources.push_back(r);
}
void removeResource(Resource &r) {
resources.erase(std::remove(resources.begin(), resources.end(), r), resources.end());
}
Human &getChief() {
for(auto const& resource: this->resources) {
if (resource.isChief()) {
return resource;
}
}
return Company::nullHuman;
}
};
class Human : public Resource {
public:
Human(const char *name) : Resource(name) {
// humans are just tools
}
virtual bool isAtWork() = 0;
virtual bool isHappy() = 0;
};
class Liquid {
const char *name;
public:
Liquid(const char *name) {
this->name = name;
}
};
class Coffee : public Liquid {
public:
Coffee() : Liquid("coffee") {
}
};
class Container : public vector<const char*> {
public:
Container() {
}
};
class HumanNullObject : public Human {
bool isNearby() { return false; }
bool isHappy() { return false; }
bool isAtWork() { return false; }
};
class Programmer : public Human {
public:
Programmer(const char *name = "yet another programmer") : Human(name) {
}
bool isAtWork() {
return rand() % 2;
}
bool hasNoStrengthToWork() {
return true;
}
bool isNotHappy() {
return !this->isHappy();
}
bool isHappy() {
return false;
}
bool isNearby() {
return rand() % 3;
}
Human &getChief() {
return Company::getInstance().getChief();
}
void goSleep() {
std::this_thread::sleep_for(std::chrono::milliseconds(x));
}
void fill(Container &container, int enableMilk, int sugarSpoons) {
if (enableMilk) {
container.push_back("milk");
}
for (int i = 0; i < sugarSpoons; i++) {
container.push_back("sugar");
}
}
};
class Chief {
bool isAtWork() {
return false;
}
void fire(Programmer &disposableTool) {
Company::getInstance().removeResource(disposableTool);
}
};
class Cup : public Container {
public:
Cup(Liquid &content) {
}
const static bool MILK_ENABLED = false; // no budget
const static int SUGAR_2_SPOONS = 2; // 2 spoons of sugar
};
int main() {
Programmer programmer;
if (programmer.isAtWork() && programmer.hasNoStrengthToWork())
{
Coffee coffee;
Cup cup(coffee);
programmer.fill(cup, Cup::MILK_ENABLED, Cup::SUGAR_2_SPOONS);
if (programmer.isNotHappy() && programmer.getChief().isNearby() == false)
{
programmer.goSleep();
}
}
}