/* This file contains the implementation for library Day.

   Names Declared:
      Day, an enumeration of the values Sunday through Saturday
      operator<<, a function to output a Day value
      operator>>, a function to input a Day value
-----------------------------------------------------------------*/

#include "Day.h"

#include <iostream.h>

/*------------------------------------------------------------------
   This function displays a Day value.

   Receive:  Out, an ostream reference
             DayVal, a Day value
   Output:   the character string corresponding to DayVal onto Out
   Return:   ostream reference Out
-------------------------------------------------------------------*/

ostream& operator<< (ostream& Out, Day DayVal)
{
   const char
      ErrorMsg[] = "\n*** <<: invalid Day value received!\n";

   switch (DayVal)
   {
      case Sunday:         Out << "Sunday";
                           break;
      case Monday:         Out << "Monday";
                           break;
      case Tuesday:        Out << "Tuesday";
                           break;
      case Wednesday:      Out << "Wednesday";
                           break;
      case Thursday:       Out << "Thursday";
                           break;
      case Friday:         Out << "Friday";
                           break;
      case Saturday:       Out << "Saturday";
                           break;
      case DayOverflow:    Out << "Day Overflow";
                           break;
      case DayUnderflow:   Out << "Day Underflow";
                           break;
      default:
                           cerr << ErrorMsg;
   }

   return Out;
}

/*-----------------------------------------------------------------
   This function inputs a Day value.

   Receive: In, an istream reference
            DayVal, a Day reference
   Input:   A character string, from In
   Output:  An error message, if there is no Day value that
               corresponds to the input string.
   Return:  If there is a Day value corresponding to the string,
               return that Day value via parameter DayVal
            istream In
------------------------------------------------------------------*/

#include <ctype.h>
#include "Strings.h"                      // class Strings

istream& operator>> (istream& In, Day& DayVal)
{
   Strings
      Str;                                // the input string
   const char
      ErrorMsg[] = "\n*** >>: invalid Day value received!\n";

   In >> Str;                             // 1. Input the string

   for (int i = 0; i < Str.Length(); i++) // 2. Convert it
      if (isupper(Str[i]))                //      to lowercase
         Str[i] = tolower(Str[i]);

   if (Str == "sunday")                   // 3. Set DayVal  to the
      DayVal = Sunday;                    //      value corresponding
   else if (Str == "monday")              //      to the string Str
      DayVal = Monday;
   else if (Str == "tuesday")
      DayVal = Tuesday;
   else if (Str == "wednesday")
      DayVal = Wednesday;
   else if (Str == "thursday")
      DayVal = Thursday;
   else if (Str == "friday")
      DayVal = Friday;
   else if (Str == "saturday")
      DayVal  = Saturday;
   else
   {
      DayVal = DayOverflow;               // Return error value
      cerr << ErrorMsg;                   // Display error msg
   }

   return In;                             // 4. return In
}

