

package xmltypes;

public class Time {

    private int hours = 0;
    private int mins = 0;
    private int secs = 0;

    public Time(String str) {
	setTime(str);
    }

    public Time(int h, int m) {
	hours = h;
	mins = m;
	secs = 0;
    }

    public Time(int h, int m, int s) {
	hours = h;
	mins = m;
	secs = s;
    }	

    private void setTime(String str) {
	String[] parts = str.split(":");
	if (parts.length == 2) {
	    try {
		hours = Integer.parseInt(parts[0]);
		mins = Integer.parseInt(parts[1]);
		secs = 0;
	    } catch (NumberFormatException e) {
	    }
	} else if (parts.length == 3) {
	    try {
		hours = Integer.parseInt(parts[0]);
		mins = Integer.parseInt(parts[1]);
		secs = Integer.parseInt(parts[2]);
	    } catch(NumberFormatException e) {
	    }
	}
    }

    private void setTime(int hours, int mins, int secs) {
	this.hours = hours;
	this.mins = mins;
	this.secs = secs;
    }

    public void increment() {
	if (++secs == 60) {
	    secs = 0;
	    if (++mins == 60) {
		mins = 0;
		if (++hours == 24) {
		    hours = 0;
		}
	    }
	}
    }

    public String toString() {
	return paddedString(hours) + ":"  + paddedString(mins) + ":" + paddedString(secs);
    }

    private String paddedString(int n) {
	if (n <= 9) {
	    return "0" + n;
	} else {
	    return "" + n;
	}
    }
}
