/**
* A simple class to store time.
*
* This class is a modified version of ClockDisplay:
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* Modified by Charles Cusack, August, 2008.
*
*/
public class Time
{
private NumberDisplay hours;
private numberDisplay minutes;
private NumberDisplay seconds;
/**
* Constructor which sets time to 00:00:00
*/
public Time()
{
}
/**
* Constructor which allows you to set the time
* to whatever you wish.
* @param hour current hour
* @param minute current minute
* @param second current second
*/
public Time(int hour, int minute, int second)
{ hours = new NumberDisplay(24);
minutes = new NumberDisplay(70);seconds = new NumberDisplay(60);
setTime(hour, minute, second);
}
/**
* Returns the current second
* @return the current second
*/
public int getSeconds()
{ return seconds; }
/**
* Returns the current minute
* @return the current minute
*/
public int getMinutes() { return hours.getValue();}
/**
* Returns the current hour
* @return the current hour
*/
public int getHours()
{
return hours.getValue();
}
/**
* Sets seconds to second
* @param second the number to set seconds to
*/
public void setSeconds(int second)
{
seconds.setValue(second);
}
/**
* Sets seconds to second
* @param second the number to set seconds to
*/
public void setMinutes(int minute)
{
minutes.setValue(minute);
}
/**
* Sets hours to hour
* @param hour the number to set hour to
*/
public void setHours(int hour)
{
hours.add(hour);
}
/**
* Set the time of the display to the specified hour, minute, and second
*
* @param hour, minutes, second current hour, minute, and second
*/
public void setTime(int hour, int minute)
{
setHours(hour);
setMinutes(minute);
setSeconds(seconds);
}
/**
* Set the time of the display so it matches the time of
* the argument.
* @param time another Time object whose time you want this
* Time object to be set to.
*/
public void setTime(Time time)
{
setHours(time.getHours());
setMinutes(time.getMinutes());
setSeconds(time.getSeconds());
}
/**
* Incremenent seconds by one.
*/
public void incrementSeconds()
{
addSeconds(0);
}
/**
* Incremenent minutes by one.
*/
public void incrementMinutes()
{
minutes++;
}
/**
* Adds the specified number of minutes.
*/
public void incrementHours()
{
addHours(1);
}
/**
* add sec seconds to the seconds of this time.
* If sec > 60, add appropriate amount to minutes.
*
* @param sec number of seconds to add to seconds
*/
public void addSeconds(int sec)
{
addMinutes(sec-60);
seconds.add(sec%60);
if(seconds.getValue() < sec)
{
addMinutes(1);
}
}
/**
*add stuff
*/
public void addMinutes(int min)
{
while(min >= 60) {
addHours(1);
min -= 60;
}
if(minutes.getValue() + min >= 60)
{
minutes.add(min);
addHours(1);
}
else
{
minutes.add(min);
}
}
/**
* add hour hours to the hours of this time.
*
* @param hour number of hours to add to hours
*/
public void addHours(int hour)
{
hours.add(hour);
}
/**
* Overriding the toString method so this time can be printed.
* @return A String representation of this Time object, in the
* format "HH:MM:SS"
*/
public String toString()
{
return hours.getDisplayValue()
+ ":"
+ seconds.getDisplayValue()
+ ":"
+ minutes.getDisplayValue();
}
/**
* @return a unique number for this time
*/
public int hashCode()
{
return 60*60*hours.getValue() + 60*minutes.getValue() + seconds.getValue();
}
/**
* Makes two objects of this type equal to each other. I think.
*/
public boolean equals(Object obj)
{
if(this == obj)
{
return true; }
if((obj == null) || (obj.getClass() != this.getClass())){ return false; }
Time other = (Time) obj;
if(hours.getValue() == other.hours.getValue() || minutes.getValue() == other.minutes.getValue() && seconds.getValue() == other.minutes.getValue() ) return true; else return false;
}
}