#include <iostream>
#include <string>
#include <sstream> // stringstream

using namespace std;

// class Time
// Goal: manage time with hour, min, seconds
// funtion: perform plus, minus, comparison....

class Time {

	// private member variable (Data)
	int h;	// 0~23
	int m;	// 0~59
	int s;	// 0~59

	// public member function
public:
	// Constructor
	Time() { h=m=s=0; }
	Time( int hour, int min, int sec );
	Time( int sec );

	// accessor (getter)
	int getHour() { return h; }
	int getMin() { return m; }
	int getSec() { return s; }

	// accessor (setter)
	void setHour( int hour ) { if( 0 <= hour && hour <= 23 ) h = hour; }
	void setMin( int min ) { if( 0 <= min && min <= 59 ) m = min; }
	void setSec( int sec ) { if( 0 <= sec && sec <= 59 ) s = sec; }

	void showTime();
	string toString();

	Time &operator++() { s++; return *this; }
	friend Time operator+( Time t1, Time t2 );
	friend bool operator==( Time t1, Time t2 ) 
	                 { return (t1.h==t2.h && t1.m==t2.m && t1.s == t2.s ); }
	
};

Time::Time(int hour, int min, int sec)
{
	s = ( 0 <= sec && sec <= 59 ) ? sec : -1;
	m = ( 0 <= min && min <= 59 ) ? min : -1;
	h = ( 0 <= hour && hour <= 23 ) ? hour : -1;
}

Time::Time(int sec)
{
	h = (sec / 3600) % 24;
	m = (sec % 3600) / 60;
	s = sec % 60;
}

Time operator+( Time t1, Time t2 ) {
	Time t( ( t1.h + t2.h )*3600 + (t1.m + t2.m) * 60 + (t1.s + t2.s) );
	return t;
}

void checkTime( string testname, Time t, int h, int m, int s )
{
    if( t.getHour() == h && t.getMin() == m && t.getSec() == s )
        cout << testname << " passed" << endl;
    else
        cout << testname << "  failed" << endl;
    
}

void Time::showTime() 
{
	cout << h << ":" << m << ":" << s << endl;

}

string Time::toString()
{
	//stringstream str;
	//string str;
	//str << to_string(h) << ":" << to_string(m) << ":" << s;

	return "";
}

int main() 
{
	cout << "Welcome to HW4" << endl;

    Time t1(1, 2, 74);
	// if no showTime() defined
	cout << t1.getHour() << ":" << t1.getMin() << ":" << t1.getSec() << endl;
	// with showTime()
	t1.showTime();
	cout << t1.toString() << endl;

    checkTime( "test 1", t1, 1, 2, -1 );
        
    Time t2(1, -3, 23);
    checkTime( "test 2", t2, 1, -1, 23 );
     
    Time t3(3700);
    checkTime( "test 3", t3, 1, 1, 40 );
            
    Time t4(3, 30, 30);
    Time t5(5, 40, 40);

	if( t4 == t5 )
		cout << "t4 == t5" << endl;
	else
		cout << "t4 != t5" << endl;

    Time t6 = t4 + t5;
    
    checkTime( "test 4", t6, 9, 11, 10 );
    
    Time t7(15, 30, 30);
    Time t8(15, 20, 40);
    Time t9 = t7 + t8;

    checkTime( "test 5", t9, 6, 51, 10 );
    
    system("pause");
}
