package cse1030; /** * A class that represents a time using hours and minutes. The time is based * on a 12-hour clock. The time represented by SimpleTime does not * automatically change with the passing of time; a client is able to manually * set the time using the provided mutator methods. * * @author CSE1030 * */ public class SimpleTime implements Comparable { private int hour; private int minute; private boolean isAM; /** * Create a SimpleTime representing midnight (12:00 AM). * * @throws IllegalArgumentException * if hour is not in the range 1-12 * @throws IllegalArgumentException * if minute is not in the range 0-59 */ public SimpleTime() { this(12, 0, true); } /** * Create a SimpleTime object that represents the same time * as another SimpleTime object. * * @param other the SimpleTime object to copy */ public SimpleTime(SimpleTime other) { this(other.getHour(), other.getMinute(), other.isAM()); } /** * Create a SimpleTime representing the time with the given hour * and minute. * *

* If isAM is true then the object represents a time * that is between midnight and just before noon (12:00 AM-11:59 AM), * otherwise the object represents a time that is between noon and just before * midnight (12:00 PM-11:59 PM). * * @param hour * the hour of the day * @param minute * the minute of the hour * @param am * use true to indicate a time in the AM; use * false to indicate a time in the PM. * @throws IllegalArgumentException * if hour is not in the range 1-12 * @throws IllegalArgumentException * if minute is not in the range 0-59 */ public SimpleTime(int hour, int minute, boolean am) { this.setHour(hour); this.setMinute(minute); this.setAM(am); } /** * Get the hour of the day (1-12) represented by this object. * * @return the hour */ public int getHour() { return this.hour; } /** * Set the hour of the day (1-12) represented by this object. * * @param hour * the hour to set * @throws IllegalArgumentException * if hour is not in the range 1-12 */ public void setHour(int hour) { if (hour < 1 || hour > 12) { throw new IllegalArgumentException("hour must be between 1-12"); } this.hour = hour; } /** * Get the minute of the hour (0-59) represented by this object. * * @return the minute */ public int getMinute() { return this.minute; } /** * Set the minute of the hour (0-59) represented by this object. * * @param minute * the minute to set * @throws IllegalArgumentException * if minute is not in the range 0-59 */ public void setMinute(int minute) { if (minute < 0 || minute > 59) { throw new IllegalArgumentException("minute must be between 0-59"); } this.minute = minute; } /** * Returns true if the time represented by this object is in the * AM, and false otherwise. * * @return true if the time represented by this object is in the * AM, and false otherwise. */ public boolean isAM() { return this.isAM; } /** * Set the time to AM or PM. * * @param am * use true to indicate a time between midnight and just * before noon (12:00 AM-11:59 AM), and false to * represent a time that is between noon and just before midnight * (12:00 PM-11:59 PM). */ public void setAM(boolean am) { this.isAM = am; } /** * Generate a hash code for this object. * * @return a hash code for this object */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + hour; result = prime * result + (isAM ? 1231 : 1237); result = prime * result + minute; return result; } /** * Compare this SimpleTime to an object for equality. * *

* The result is true if and only if obj is a * SimpleTime object, and both objects represent the same time of * day (i.e., identical hour, minute, and AM/PM). * * @return true if both SimpleTime objects represent * the same time of day. */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } SimpleTime other = (SimpleTime) obj; if (hour != other.hour) { return false; } if (isAM != other.isAM) { return false; } if (minute != other.minute) { return false; } return true; } /** * Returns a string representation of the time of day. The string has the * format XX:YY AM or XX:YY PM where XX * is the hour of the day and YY is the minute of the day. Both * the hour and minute fields are always two characters wide. * * @return the time of day represented by this object as a string */ public String toString() { String ampm = this.isAM() ? "AM" : "PM"; return String.format("%2d:%02d %s", this.getHour(), this.getMinute(), ampm); } /** * Compares this SimpleTime object to another * SimpleTime object. * *

* The result is the difference in minutes between this object * and the other object. For example, if the time represented by * this object is 11:00 AM and the time represented by the * other object is 1:05 PM then * this.compareTo(other) returns -125. * * @return the difference in minutes between this object and the * other object. */ @Override public int compareTo(SimpleTime other) { int hour = this.getHour() % 12; if (!this.isAM()) { hour += 12; } int minutes = this.getMinute() + hour * 60; int otherHour = other.getHour() % 12; if (!other.isAM()) { otherHour += 12; } int otherMinutes = other.getMinute() + otherHour * 60; return minutes - otherMinutes; } }