/**
 * An enumerated type for the months of the year
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 2.0
 */
public enum Month
{
   JANUARY   ("January",  31),
   FEBRUARY  ("February", 28),
   MARCH     ("March",    31), 
   APRIL     ("April",    30),
   MAY       ("May",      31),
   JUNE      ("June",     30), 
   JULY      ("July",     31),
   AUGUST    ("August",   31),
   SEPTEMBER ("September",30),
   OCTOBER   ("October",  31),
   NOVEMBER  ("November", 30),
   DECEMBER  ("December", 31);

   private final int    days;
   private final String name;

   /**
    * Explicit Value Constructor.
    *
    * @param name   The name of the Month
    * @param days   The number of days in the Month
    */
   private Month(String name, int days)
   {
      this.name = name;
      this.days = days;
   }

   /**
    * Get the 3-letter abbreviation for this Month.
    *
    * @return  The 3-letter abbreviation
    */
   public String getAbbreviation()
   {
      String       result;
      
      if (name.length() > 3) result = name.substring(0,3)+".";
      else                   result = name;

      return result;      
   }

   /**
    * Get the numeric value of this Month (in the interval [1,12]).
    *
    * @return  The numeric value of this Month
    */
   public int getNumber()
   {
      return ordinal()+1; // ordinal() returns the position in the declaration
   }

   /**
    * Get the (normal) number of days in this Month.
    *
    * @return  The normal number of days in this Month.
    */
   public int getLength()
   {
      return days;      
   }
   
   /**
    * Return the Month that corresponds to a given String.
    *
    * @param s   The String representation
    * @return    The corresponding Month (or null)
    */
   public static Month parseMonth(String s)
   {
       Month[] all;
       all = Month.values();
       for (int i=0; i<all.length; i++)
       {
          if (s.equalsIgnoreCase(all[i].name) 
              || s.equalsIgnoreCase(all[i].getAbbreviation()))
          {
              return all[i];
          }
       }
       return null;
   }
   
   /**
    * Get a String representation of this Month
    *
    * @return   The String representation (i.e., the name)
    */
   public String toString()
   {
      return name;      
   }
}
