#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// =============================================================================

class PlayTime {
public:
	PlayTime(int m=0, int s=0);
	void add(const PlayTime& t);
private:
	void normalize();
	int min;
	int sec;
friend ostream& operator<<(ostream& out, const PlayTime& t);
};

istream& operator>>(istream& in, PlayTime& t);

PlayTime::PlayTime(int m, int s): min(m), sec(s) {
	normalize();
}

void PlayTime::add(const PlayTime& t) {
	min+=t.min;
	sec+=t.sec;
	normalize();
}

void PlayTime::normalize() {
	long tsec(min*60+sec);
	min=tsec/60;
	sec=tsec%60;
}

ostream& operator<<(ostream& out, const PlayTime& t) {
	return out<<setfill('0')<<setw(2)<<t.min<<":"<<setw(2)<<t.sec;
}

istream& operator>>(istream& in, PlayTime& t) {
	int i;
	if (in>>i) {
		if (in.peek()==':') {
			in.get();
			int j;
			if (in>>j)
				t=PlayTime(i, j);
		}
		else
			t=PlayTime(0, i);
	}
	return in;
}

// =============================================================================

// De class PlayList bevat:
//	-	een pointer naar een PlayListElement genaamd first dit is het eerste
//		element in de gelinkte lijst.
//		-	een PlayListElement bevat:
//			-	een PlayTime object genaamd pt.
//			-	een pointer naar een PlayListElement genaamd next.
//				(deze pointer is 0 als de lijst geen elementen meer heeft).

class PlayList {
public:
	PlayList();
	~PlayList();
	void insert(const PlayTime& t);
	PlayTime total() const;
private:
	class PlayListElement {
	public:
		PlayTime pt;
		PlayListElement* next;
	};
	PlayListElement* first;
friend ostream& operator<<(ostream& out, const PlayList& l);
};

istream& operator>>(istream& in, PlayList& l);

PlayList::PlayList(): first(0) {
}

PlayList::~PlayList() {
	while (first!=0) {
		PlayListElement* listElement(first);
		first=first->next;
		delete listElement;
	}
}

PlayTime PlayList::total() const {
	PlayTime t;
	PlayListElement* pList(first);
	while (pList) {
		t.add(pList->pt);
		pList=pList->next;
	}
	return t;
}

void PlayList::insert(const PlayTime& t) {
	PlayListElement* newlistElement(new PlayListElement);
	newlistElement->pt=t;
	newlistElement->next=first;
	first=newlistElement;
}

ostream& operator<<(ostream& out, const PlayList& l) {
	PlayList::PlayListElement* pList(l.first);
	while (pList!=0) {
		out<<pList->pt<<endl;
		pList=pList->next;
	}
	return out;
}

istream& operator>>(istream& in, PlayList& l) {
	PlayTime pt;
	while (in>>pt) {
		l.insert(pt);
	}
	return in;
}

// =============================================================================

int main () {
	ifstream fin("playlist.txt");
	if (fin!=0) {
		PlayList lijst;
		fin>>lijst;
		cout<<"De lijst:"<<endl<<lijst;
		cout<<"Totale speelduur: "<<lijst.total()<<endl;
	}
	else
		cout<<"De file playlist.txt kan niet worden geopend!"<<endl;
	cout<<"Druk op de return-toets."<<endl;
	cin.get();
	return 0;
}
