// Tempo.cpp: implementation of the CTempo class.
//
//////////////////////////////////////////////////////////////////////

#include "iostream.h"

#include "Tempo.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTempo::CTempo()
{
	nHoras = nMinutos = nSegundos = 0;

	cout << "Construtor executado.\n";
}

CTempo::CTempo(const CTempo& t)
{
	nHoras = t.nHoras;
	nMinutos = t.nMinutos;
	nSegundos = t.nSegundos;

	cout << "Construtor cópia executado.\n";
}

CTempo::CTempo(const int nHor, const int nMin, const int nSeg)
{
	nHoras = nHor;
	nMinutos = nMin;
	nSegundos = nSeg;

	if ( Valido() )
		cout << "Construtor de parâmetros executado.\n";
	else
	{
		cout << "Valores iniciais nao validos! Inicializando valores por defeito.\n";
		CTempo();
	}
}

CTempo::~CTempo()
{

}

void CTempo::Leitura()
{
	do
	{
		cout << "Horas: ";
		cin >> nHoras;

		cout << "Minutos: ";
		cin >> nMinutos;

		cout << "Segundos: ";
		cin >> nSegundos;

		if ( Valido() )
			return;
	}
	while (TRUE);
}


void CTempo::Apresenta()
{
	cout << nHoras << ":" << nMinutos << ":" << nSegundos << endl;
}

CTempo& CTempo::operator =(const CTempo &t)
{
	if (this == &t)
		return *this;

	nHoras = t.nHoras;
	nMinutos = t.nMinutos;
	nSegundos = t.nSegundos;

	return *this;
}

int CTempo::operator ==(const CTempo &t)
{
	if (nHoras == t.nHoras &&
		nMinutos == t.nMinutos &&
		nSegundos == t.nSegundos)
		return TRUE;
	else
		return FALSE;
}

CTempo& CTempo::operator +=(const CTempo &t)
{
	Adiciona(t);

	return *this;
}

CTempo CTempo::operator +(const CTempo &t)
{
	CTempo aux(*this);
	
	aux.Adiciona(t);

	return aux;
}

/////////////////////////////////////////////////////
//
// Funcoes privadas
//
/////////////////////////////////////////////////////

int CTempo::Valido()
{
	if (nHoras < 0 || nHoras > 23 ||
		nMinutos < 0 || nMinutos > 59 ||
		nSegundos < 0 || nSegundos > 59)
		return FALSE;
	else
		return TRUE;
}

void CTempo::Adiciona(const CTempo& tempo)
{
	nHoras += tempo.nHoras;
	nMinutos += tempo.nMinutos;
	nSegundos += tempo.nSegundos;

	if ( nSegundos >= MAX_SEGUNDOS )
	{
		nSegundos = nSegundos % MAX_SEGUNDOS;
		nMinutos++;
	}
	if ( nMinutos >= MAX_MINUTOS )
	{
		nMinutos = nMinutos % MAX_MINUTOS;
		nHoras++;
	}
	if ( nHoras >= MAX_HORAS )
		nHoras = nHoras % MAX_HORAS;
}

