// ------------------------------------------------------------------------- /** * This class represents a single appointment that might be stored in * a timetable. The appointment consists of a time and a string * description. The time is stored as an hour number, 0-23 (military * time). * * @author Stephen Edwards * @version Feb 22, 2013 */ public class Appointment { //~ Instance/static variables ............................................. private int hour; private String description; //~ Constructor ........................................................... // ---------------------------------------------------------- /** * Creates a new Appointment object. * @param anHour the time for this appointment (0-23, military time) * @param aDescription the description of this appointment */ public Appointment(int anHour, String aDescription) { hour = anHour; description = aDescription; } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Getter for the appointment's description property. * @return the appointment's description */ public String description() { return description; } // ---------------------------------------------------------- /** * Getter for the appointment's hour property. * @return the appointment's hour (time) as an integer (0-23, * military time) */ public int hour() { return hour; } // ---------------------------------------------------------- /** * Setter for the appointment's description property. * @param newDescription the new description for this appointment */ public void setDescription(String newDescription) { description = newDescription; } // ---------------------------------------------------------- /** * Setter for the appointment's hour property. * @param newHour the new hour for this appointment (0-23) */ public void setHour(int newHour) { hour = newHour; } // ---------------------------------------------------------- /** * A convenience setter that takes a time like "9am" or "11pm" and * sets this appointment's hour. * @param time a 12-hour am/pm time for this appointment */ public void setTime(String time) { String digits = time.substring(0, time.length() - 2); int newHour = Integer.parseInt(digits); char half = time.toLowerCase().charAt(time.length() - 2); if (half == 'p' && newHour < 12) { newHour += 12; } else if (half == 'a' && newHour == 12) { newHour = 0; } setHour(newHour); } // ---------------------------------------------------------- /** * Generate a string representation for this appointment, with the * form "11am: dentist" or "2pm: class". The string consists of the * 12-hour time representation with a (lower case) "am" or "pm" * designator, followed by a colon and space, and then the appointment * description. * @return a printable representation of this appointment */ public String toString() { String half = (hour > 11) ? "pm" : "am"; int printableHour = hour; if (printableHour > 11) { printableHour -= 12; } if (printableHour == 0) { printableHour = 12; } return printableHour + half + ": " + description; } }