Wątek przeniesiony 2016-12-27 12:16 z Kosz przez Shalom.

fragment kodu - pomocy

0

Jestem bardzo początkująca w programowaniu, dlatego proszę bardziej doświadczonych o sprawdzenie poniższego kodu i ewentualne naniesienie poprawek :)

Programmer programmer;
Cup cup;
Coffee coffee;

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();
   }
}
0

Co ten kod ma robić? Na pewno nie jest to poprawny kod w C.

0

Zmienne programista, kubek, kawa, krótko mówiąc.. jeśli programista jest w pracy i nie ma siły to robi kawę, jeśli jest nieszczesliwy i nie ma szefa obok idzie spać

0

Mistrza Karola tym sucharem nie pobijesz.

0

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();
        }
    }
}
0

wow! msm dzięki niesamowite!

1 użytkowników online, w tym zalogowanych: 0, gości: 1