/*
Time.java
Adapted from Deitel and Deitel, Java How To Program

We do not have separate "header" and "implementation" files
Notice there is no main function (as with C++, there would be a separate driver)

*/

import java.text.DecimalFormat;  // for formatting

// Must be in file named Time.java
// Notice public visibility modifier before class

public class Time{

   // instance data declared here
   private int hour;
   int minute; // unlike C++, this is not private!  has default visibility
   int second;

    private static DecimalFormat twoDigits = new DecimalFormat("00");

    /** Default constructor sets all values to 0 */
    public Time()
    {
        this(0,0,0);  // invokes 3-parameter constructor
    }

    /** Constructor, sets hour, minute and second to user-specified values */
    public Time(int h, int m, int s)
    {
        setTime(h, m, s);
    }

    /** setTime sets hour, minute and second to user-specified values */
    public void setTime(int h, int m, int s)
    {
        setHour(h);
        setMinute(m);
        setSecond(s);
    }

    /** setHour
    @param h hour (0 to 24)
    modifier (mutator) for hour */
    public void setHour(int h)
    {
        hour = ((h >= 0 && h < 24) ? h : 0);
    }

    /** setMinute
    @param m minutes (0 to 60)
    modifer (mutator) for minute) */
    public void setMinute(int m)
    {
        minute = ((m >= 0 && m < 60) ? m : 0);
    }

    /** setSecond
    @param s seconds (0 to 60)
    modifier (mutator) for second */
    public void setSecond(int s)
    {
        second = ((s >= 0 && s < 60) ? s : 0);
    }

    /** getHour
    @return hour */
    public int getHour()
    {
        return hour;
    }

    /** getMinute
    @return minute */  
    public int getMinute()
    {
        return minute;
    }

    /** getSecond
    @return second  */
    public int getSecond()
    {
        return second;
    }

    /** toString
    @return formatted string hh:mm am  */
    public String toString()
    {
        return ((getHour() == 12 || getHour() == 0) ? 12 : getHour() % 12)
                    + ":" +
                    twoDigits.format(getMinute()) + ":" +
                    twoDigits.format(getSecond()) +
                    (getHour() < 12 ? " AM" : " PM" );
    }

    /** equals */
    public boolean equals(Object v) {
    if (v instanceof Time) {
       Time t = (Time) v; // needed because parameter is an Object
          // equal if all three fields match
        return (t.hour == hour) && (t.minute == minute) && (t.second  == second);
     }
     else
        return false; // false if Object is not a Time object
    } 

}