
/**
 * A 12-hour (non-military time) digital clock.
 *
 * @author Matt Boutell.
 *         Created Mar 27, 2008.
 */
public class Clock implements Comparable {
	// You can use an alternate fields, as long as you update .equals()
	private int hours;
	private int minutes;
	private String meridian; // AM or PM

	/**
	 * Constructs a clock with the given time. You may assume
	 * that the parameters are all in the valid ranges.
	 *
	 * @param hours
	 * @param minutes
	 * @param meridian AM or PM
	 */
	public Clock(int hours, int minutes, String meridian) {
		this.hours = hours;
		this.minutes = minutes;
		this.meridian = meridian;
	}
	
	/** 
	 * Examples: 4:55 PM, 12:03 AM. Note that the hours may be 1 digit,
	 * the minutes must be 2 digits, and there's a space before the meridian,
	 * and no other spaces. 
	 */
	@Override
	public String toString() {
		String string1 = String.format("%d:%02d %s", this.hours, this.minutes, this.meridian);
		return string1;
	}
	
	/**
	 * Advances the clock's time by one minute. I know, that's a big TICK!
	 */
	public void tick() {
		if (this.minutes < 59) {
			this.minutes += 1;
		} else if(this.hours < 11) {
			this.minutes = 0;
			this.hours += 1;
		} else if(this.hours == 11 && this.meridian == "AM") {
			this.minutes = 0;
			this.hours += 1;
			this.meridian = "PM";
		} else if(this.hours == 11 && this.meridian == "PM") {
			this.minutes = 0;
			this.hours += 1;
			this.meridian = "AM";
		} else if(this.hours == 12) {
			this.minutes = 0;
			this.hours = 1;
		}
	}

   /**
    * Is this time the same as other?
    */
	@Override
	public boolean equals(Object other) {
		Clock rhs = (Clock)other;
		return (this.hours == rhs.hours
		&& this.minutes == rhs.minutes
		&& this.meridian == rhs.meridian);
	}

	/**
	 * Compares two times within a given day. Midnight is the earliest time in a day.
     * Hint: a well-chosen helper function could make your life much easier here!<br>
     * 
	 * Returns -1 if this time is before the other.<br>
	 * Returns  0 if the times are equal.<br>
	 * Returns 1 if this time is after the other.<br>
	 * 
	 * Throw an IllegalArgumentException if other isn't a Clock.
	 */
	@Override
	public int compareTo(Object other) {
		
		Clock other1 = (Clock) other;
		if (this.meridian == "AM" && other1.meridian == "PM") {
			return -1;
		} else if(this.meridian == "PM" && other1.meridian == "AM") {
			return 1;
		} else if(this.hours > other1.hours && this.hours != 12 ) {
			return 1;
		} else if(this.hours == 12 && other1.hours == 1) {
			return -1;
		} else if(this.hours < other1.hours && other1.hours != 12) {
			return -1;
		} else if(this.hours == 1 && other1.hours == 12) {
			return 1;
		} else if(this.minutes > other1.minutes) {
			return 1;
		} else if(this.minutes < other1.minutes) {
			return -1;
		}
		return 0;
	}
}
