Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ClockDisplay
  2. {
  3.     private NumberDisplay hours;
  4.     private NumberDisplay minutes;
  5.     private String displayString; // simulates the actual display
  6.  
  7.     /**
  8.      * Constructor for ClockDisplay objects. This constructor
  9.      * creates a new clock set at 00:00.
  10.      */
  11.     public ClockDisplay() {
  12.         hours = new NumberDisplay(24);
  13.         minutes = new NumberDisplay(60);
  14.         updateDisplay();
  15.     }
  16.    
  17.     /**
  18.      * Constructor for ClockDisplay objects. This constructor
  19.      * creates a new clock set at the time specified by the
  20.      * parameters.
  21.      */
  22.     public ClockDisplay(int hour, int minute) {
  23.         hours = new NumberDisplay(24);
  24.         minutes = new NumberDisplay(60);
  25.         setTime(hour, minute);
  26.     }
  27.    
  28.     /**
  29.      * This method should get called once every minute - it
  30.      * makes the clock display go one minute forward.
  31.      */
  32.     public void timeTick() {
  33.         minutes.increment();
  34.         if(minutes.getValue() == 0) { // it just rolled over!
  35.             hours.increment();
  36.         }
  37.         updateDisplay();
  38.     }
  39.    
  40.     /**
  41.      * Set the time of the display to the specified hour and
  42.      * minute.
  43.      */
  44.     public void setTime(int hour, int minute) {
  45.         hours.setValue(hour);
  46.         minutes.setValue(minute);
  47.         updateDisplay();
  48.     }
  49.    
  50.     /**
  51.      * Return the current time of this display in the format
  52.      * HH:MM.
  53.      */
  54.     public String getTime() {
  55.         return displayString;
  56.     }
  57.    
  58.     /**
  59.      * Update the internal string that represents the
  60.      * display.
  61.      */
  62.     private void updateDisplay() {
  63.         int hour = hours.getValue();
  64.         String suffix;
  65.        
  66.         if (hour >= 12) {
  67.             suffix = " pm";
  68.             if (hour > 12) hours.setValue(hour-12);
  69.         } else {
  70.             suffix = " am";
  71.         }
  72.        
  73.         displayString = hours.getDisplayValue() + ":" +
  74.             minutes.getDisplayValue() + suffix;
  75.     }
  76. }