C++ Time Management System

Asked

Viewed 136 times

0

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

"Develop a time management system. This system should store the description and time of activities, not being possible to extrapolate the limit of 20 activities in the system and able to present the sum of the time of the activities. To 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 integer"

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’m not sure how to use app.addAtivity() to add a new activity, or how to add each one’s time. I am well lost in development. If anyone can give me a help or some insight, I would appreciate it very much! Thank you!

1 answer

0


1 - In the "addAtivity" method, you forgot to use "( )" in the part where you call the function that returns the size of the vector. The right is "activities.size()" or the compiler will understand that you are using a pointer to the function and not invoking a function. In addition, you are capturing an exception that is thrown within the same method and that makes no sense (you could have used Std::Cout right away or captured the exception in the main() method). And do not pass exception classes by value, the right one here is to pass by reference, or you will not be able to use polymorphism within the catch block, moreover, the compiler will create a new temporary object and the result may be different than 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. New operator returns a pointer to an object, not an object itself. You should instantiate as an object on the stack and let your Std::vector include it in memory. This is how it would be right:

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;
}
  • Bah, it helped a lot!! I still have one question: how do I access each of the activities created in the app.addAtivity(Activity()) in order to set a time for each one?

  • In Std::vector, simply access as if it were a common array. activities[0] activities[1], activities[2], etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.