/*
   RobustTime.java by Craig Persiko
   Variation of Solution to CS 111B Practice Problem 3

   This class is used to store a time of day and output it.
   It also stores the current time of day and manipulates it.
*/

class InvalidTimeException extends RuntimeException
{
  public InvalidTimeException(int hr, int min)
  {
    super("Error: " + hr + ":" + min
          + " is an invalid Time: hour must be between 0 and 23,"
          + "\nand minute must be between 0 and 59.");
  }
}

class RobustTime
{
  // instance variables for an object to store a time of day:
  private int hours;
  private int minutes;

  // static variables to store the current time of day:
  private static int curHours = 0;
  private static int curMinutes = 0;

  // set current time to parameters
  public static void setCurTime(int hr, int min) throws InvalidTimeException
  {
    RobustTime t = new RobustTime(hr, min); // this constructor checks hr and min
    curHours = t.hours;
    curMinutes = t.minutes;
  }

  public static void showCurTime(boolean showAMPM)
  {
    showTime(showAMPM, curHours, curMinutes);
  }

  public static void addToCurTime(int min)
  {
    int allMinutes;

    curMinutes += min;

    if (curMinutes >= 60 || curMinutes < 0)
    {  // handle extra minutes or hours, or negative minutes or hours
      allMinutes = curHours * 60 + curMinutes;
      allMinutes %= 1440; // 1440 minutes = 24 hours.  Remove any full days from our time
      if (allMinutes < 0) // if it's before midnight
        allMinutes += 1440;  // add a day to get the time it should be (yesterday)
      curHours = allMinutes / 60; // integer division for integer result
      curMinutes = allMinutes % 60;
    }
  }

  // return true if this object's time is after the current time
  public boolean isLaterToday()
  {
    if(hours > curHours)
      return true;
    else if(hours == curHours && minutes > curMinutes)
      return true;
    else
      return false;
  }

  // Default Constructor
  // initialize time to 0 (midnight)
  public RobustTime()
  {
    hours = minutes = 0;
  }

  // initialize time using parameters and throw an exception
  // if hours is now between 0 and 23 and minutes is not between 0 and 59.
  public RobustTime(int hr, int min) throws InvalidTimeException
  {
    if (hr >= 24 || min >= 60 || min < 0 || hr < 0)
      throw new InvalidTimeException(hr, min);

    hours = hr;
    minutes = min;
  }

  // display time on the screen, using 24-hour time if showAMPM is false,
  // or 12-hour time if showAMPM is true.  Display in standard h:mm format.
  public void show(boolean showAMPM)
  {
    showTime(showAMPM, hours, minutes);
  }

  // a static function to display any time (hours and minutes passed as parameters)
  // on the screen, using 24-hour time if showAMPM is false,
  // or 12-hour time if showAMPM is true.  Display in standard h:mm format.
  private static void showTime(boolean showAMPM, int h, int m)
  {
     String A_or_P;

     if(showAMPM)
     {
       if(h > 12)  // afternoon / evening
       {
         A_or_P = "P";
         System.out.print((h - 12) + ":");
       }
       else if (h == 12)  // noon
       {
         A_or_P = "P";
         System.out.print(h + ":");
       }
       else if (h == 0)  // midnight
       {
         A_or_P = "A";
         System.out.print(12 + ":");
       }
       else  // morning
       {
         A_or_P = "A";
         System.out.print(h + ":");
       }
       // output minutes and AM or PM:
       if(m < 10)
         System.out.print("0");
       System.out.print(m + A_or_P + "M");
     }
     else  // 24-hour time
     {
       System.out.print(h + ":");
       if(m < 10)
         System.out.print("0");
       System.out.print(m);
     }
  }
}



syntax highlighted by Code2HTML, v. 0.9