Загрузка данных


Этап 2. Разработка кода
pump_control.h
#ifndef PUMP_CONTROL_H
#define PUMP_CONTROL_H

#include <string>

class Pump
{
private:
    bool state;

public:
    Pump();

    void turnOn();
    void turnOff();
    bool isOn() const;
};

#endif
pump_control.cpp
#include "pump_control.h"
#include "logger.h"

Pump::Pump()
{
    state = false;
}

void Pump::turnOn()
{
    if (!state)
    {
        state = true;
        logMessage("Pump turned ON");
    }
}

void Pump::turnOff()
{
    if (state)
    {
        state = false;
        logMessage("Pump turned OFF");
    }
}

bool Pump::isOn() const
{
    return state;
}
logger.h
#ifndef LOGGER_H
#define LOGGER_H

#include <string>

void logMessage(const std::string& message);

#endif
logger.cpp
#include "logger.h"
#include <fstream>

void logMessage(const std::string& message)
{
    std::ofstream file("log.txt", std::ios::app);

    if(file.is_open())
    {
        file << message << std::endl;
    }
}
command_interface.h
#ifndef COMMAND_INTERFACE_H
#define COMMAND_INTERFACE_H

#include "pump_control.h"

void processCommands(Pump& pump);

#endif
command_interface.cpp
#include "command_interface.h"
#include <iostream>

void processCommands(Pump& pump)
{
    std::string command;

    while (true)
    {
        std::cout << "Enter command (ON/OFF/EXIT): ";
        std::cin >> command;

        if(command == "ON")
        {
            pump.turnOn();
        }
        else if(command == "OFF")
        {
            pump.turnOff();
        }
        else if(command == "EXIT")
        {
            break;
        }
        else
        {
            std::cout << "Unknown command!" << std::endl;
        }
    }
}
irrigation_init.h
#ifndef IRRIGATION_INIT_H
#define IRRIGATION_INIT_H

void initializeSystem();

#endif
irrigation_init.cpp
#include "irrigation_init.h"
#include "pump_control.h"
#include "command_interface.h"

void initializeSystem()
{
    Pump pump;
    processCommands(pump);
}
main.cpp
#include "irrigation_init.h"

int main()
{
    initializeSystem();
    return 0;
}