C ++ Time Management System

0

I have to develop a time management system for the Data Structure chair in college, but I'm hitting myself to make it work. What is requested is the following:

"Develop a time management system. This system should store the description and the time of activities, not being able to extrapolate the limit of 20 activities in the system and that is able to present the sum of the time of the activities. For develop this system, implement an Activity class, composed of two attributes, description and time, where, description is a string and time is an object of type Time, composed by the hour and minute attributes, both integers "

What I've been able to develop so far is this:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

class valorMinuto
{
    int line;
    string file;

public:
    valorMinuto(int line, string file)
    {
        this->line = line;
        this->file = file;
    }
    string messange()
    {
        ostringstream oss;
        oss << "Erro na linha " << this->line << " no arquivo " << this->file << endl;
        oss << "valor de minuto menor que 0 ou maior que 59" << endl;
        return oss.str();
    }
};

class limiteExcedido
{
    int line;
    string file;

public:
    limiteExcedido(int line, string file)
    {
        this->line = line;
        this->file = file;
    }
    string messange()
    {
        ostringstream oss;
        oss << "Erro na linha " << this->line << " no arquivo " << this->file << endl;
        oss << "limite de armazenamento excedido!" << endl;
        return oss.str();
    }
};

class Tempo
{
public:
    int hora, minuto;
    Tempo();
    Tempo operator+ (const Tempo&) const;
    void setHora(int h);
    void setMinuto(int m);

};

Tempo::Tempo()
{
}

void Tempo::setHora(int h)
{
        hora = h;
}

void Tempo::setMinuto(int m)
{
    try {
        minuto = m;
        if (minuto < 0 || minuto > 60)
        {
            throw(valorMinuto(__LINE__, __FILE__));
        }
    }
    catch (valorMinuto exception)
    {
        cout << "Excecao: " << exception.messange() << endl;
    }
}

Tempo Tempo:: operator+ (const Tempo& param) const
{
    Tempo temp;
    temp.hora = hora + param.hora;
    temp.minuto = minuto + param.minuto;

    temp.hora += temp.minuto / 60;
    temp.minuto %= 60;

    return temp;
}



class Atividade
{
public:
    Tempo tempo;
    string descricao;
    Atividade();
    ~Atividade();
    int cont;
    void setHora(int x);
    void setMinuto(int x);
    int getHora();
    int getMinuto();
    Tempo getTempo();
    void setDescricao(string x);
    string getDescricao();
};

Atividade::Atividade()
{
}


Atividade::~Atividade()
{
}

void Atividade::setHora(int x)
{
    tempo.setHora(x);
}

void Atividade::setMinuto(int x)
{
    tempo.setMinuto(x);
}

int Atividade::getHora()
{
    return tempo.hora;
}

int Atividade::getMinuto()
{
    return tempo.minuto;
}

void Atividade::setDescricao(string x)
{
    descricao = x;
}

string Atividade::getDescricao()
{
    return descricao;
}

Tempo Atividade::getTempo()
{
    return tempo;
}


class App
{
    vector<Atividade> atividades;
public:
    App() {
    }

    ~App() {

    }

    void addAtividade(Atividade value)
    {
        try
        {
            if (this->atividades.size >= 20) {
                throw limiteExcedido(__LINE__, __FILE__);
            }
        }
        catch (limiteExcedido exception)
        {
            cout << "Excecao: " << exception.messange() << endl;
        }
        this->atividades.push_back(value);
    }

    Tempo soma() {
        Tempo s;
        for (vector<Atividade>::iterator it = this->atividades.begin(); it != this->atividades.end(); it++)
        {
            s = s + it->getTempo();
        }
        return s;
    }
};


int main()
{

    App app;
    app.addAtividade(*new Atividade);
    app.addAtividade(*new Atividade);
    app.addAtividade(*new Atividade);


    Tempo r = app.soma();


    system("pause");
    return 0;
}

But I do not know how to use app.addActivity () to add a new activity, or how to add the time for each activity. I'm pretty lost in development. If anyone can give me a hand or some insight, I would greatly appreciate it! Thanks!

    
asked by anonymous 08.05.2017 / 16:09

1 answer

0

1 - In the "addActivity" method, you forgot to use "()" in the part where you call the function that returns the size of the vector. The right one is "activities.size ()" or the compiler will understand that you are using a pointer to the function and not invoking a function. Also, you are catching an exception that is thrown within the same method and this does not make sense (you could have used std :: cout at once or caught the exception in the main () method). And do not pass exception classes by value, the right here is to pass by reference, or you will not be able to use polymorphism within the catch block, in addition, the compiler will create a new temporary object and the result may be different from what you expect .

void addAtividade(const Atividade& value)
{
    if (this->atividades.size() >= 20) {
        throw limiteExcedido(__LINE__, __FILE__);
    }

    this->atividades.push_back(value);
}

2 - In the main function, you are instantiating the Activity object with new and you are wrong. Operator new returns a pointer to an object, not an object itself. You must instantiate as an object in the stack and let your std :: vector include it in memory. The right thing would be like this:

int main()
{

    try
    {
        App app;
        app.addAtividade(Atividade());
        app.addAtividade(Atividade());
        app.addAtividade(Atividade());

        Tempo r = app.soma();
    }
    catch (limiteExcedido& exception)
    {
        cout << "Excecao: " << exception.messange() << endl;
    }


    system("pause");
    return 0;
}
    
08.05.2017 / 19:53