/*******************************************
 ** File: proj1.c
 ** Author: D. Frey
 ** Date: 1/30/00
 ** Section: 101
 ** SSN: 123-45-6789
 ** EMail: frey@cs.umbc.edu
 **
 **      This program calculates connection fees
 ** for user's of the FB Internet Service.
 ** Start and end times are input as 24-hour
 ** military time.  It accepts start/end times
 ** until 0 is entered as both start time and end time.
 **
 ** The connection fees are based on the time of day.
 ** Each 6-hour period (0000 - 0600, 0600 - 1200, 1200 - 1800
 ** and 1800 - 0000) is a different rate.  When complete, the
 ** program displays the number of minutes connected during
 ** each time-zone of the day and the total user connection fee
 **
 ** Algorithm:
 **   The fee calculation part of this algorithm looks at
 **  the start time and end time to determine if they are in
 **  the same quarter of the day.  If so, subtract the start
 **  from the end to get the minutes.  If not, subtract the
 **  start from the beginning of the next time-zone to get the
 **  minutes in this time-zone.  "move" the start time to beginning
 **  of the next time-zone.
 **
 **    while start/end times are not both zero
 **       change military time to "minutes since midnight"
 **       if start is in 1st time-zone of the day
 **          if end is in 1st time-zone
 **            minutes in 1st time-zone = end - start
 **          else
 **            minutes in this time-zone = start of next time-zone - start
 **            start = start of next time-zone
 **
 **      The 'if' is repeated for each time-zone of the day
 **
 ** Assumptions
 **    1. the user connect time does not cross midnight
 **       (i.e  startTime is always less than stopTime
 **    2. There will be no invalid input
 **
 ******************************************************/

#include <stdio.h>

/* constants --
** since the day is divided into 6-hours periods ("time-zones")
** the constant names start with TZ1, TZ2, TZ3, TZ4 which indicate
** the 1st, 2nd, 3rd or 4th time-zone of the day *************************/

#define TZ1START  0            /* first quarter of day starts at 0000  */
#define TZ1RATE   0.02         /* 2 cents per minute midnight to 6AM   */
#define TZ2START  (6 * 60)     /* 6:00 AM as minutes since midnight    */
#define TZ2RATE   0.03         /* 3 cents per minute 6:01 - noon       */
#define TZ3START  (12 * 60)    /* 12:00 noon as mins since midnight    */
#define TZ3RATE   0.05         /* 5 cents per minute 12:01 to 6PM      */
#define TZ4START  (18 * 60)    /* 6PM as minutes since midnight        */
#define TZ4RATE   0.06         /* 6 cents per minute 6PM to midnight   */

main ( ) 
{
   double totalFee;            /* total fee for all connect time        */
   int tz1Minutes = 0;         /* minutes in 1st time-zone of the day   */
   int tz2Minutes = 0;         /* minutes in 2nd time-zone of the day   */
   int tz3Minutes = 0;         /* minutes in 3rd time-zone of the day   */
   int tz4Minutes = 0;         /* minutes in 4th time-zone of the day   */
   int startTime, endTime;     /* military hours & minutes              */
   int startMin, endMin;       /* as "minutes since midnight"           */

   /* print greeting and directions to the user */
   printf ("\n");
   printf ("Welcome to the B & F ISP Connection Fee Calculator\n"
           "Please enter connection start time and end time\n"
           "on the same line in 24-hour military time\n"
           "Enter BOTH start and stop time as 0000 to quit\n\n");

   /* get the first start/end time
   ** this is the "priming read" */
   printf ("Please enter start time & stop time: ");
   scanf ("%d%d", &startTime, &endTime);
 
   /* process start/end times until 0 is entered for both */
   while (startTime != 0 || endTime != 0)
   {
      /* change military times into "minutes since midnight" 
      ** assumes valid input
      *****************************************************/
      startMin = (startTime / 100) * 60 + (startTime % 100);
      endMin = (endTime / 100) * 60 + (endTime % 100);
      
      /* did we start in 1st time-zone ?? */
      if (startMin < TZ2START)
      {
         /* if endTime also in 1st time-zone,
	 ** subtract times to get minutes */
         if (endMin <= TZ2START)
         {
            tz1Minutes = endMin - startMin;
         }
         else
         {
            /* if endTime in another time-zone,
            ** subtract startMin from 2nd time-zone startTime
            ** to calculate minutes in the 1st time-zone
            ** "move" startMin to start of 2nd time-zone */
            tz1Minutes = TZ2START - startMin;
            startMin = TZ2START;
	 }
      }

      /* any time in 2nd time-zone ?? */
      if (TZ2START <= startMin && startMin <= TZ3START)
      {
	 /* if endMin also in 2nd time-zone,
	 ** subtract times to get minutes */
	 if (endMin <= TZ3START)
	 {
	    tz2Minutes = endMin - startMin;
	 }
	 else
	 {
	    /* if endTime in another time-zone,
	    ** subtract startMin from 3rd time-zone startTime
	    ** to calculate minutes in the 2nd time-zone
	    ** "move" startMin to start of 3rd time-zone */
	    tz2Minutes = TZ3START - startMin;
	    startMin = TZ3START;
	 }
      }

      /* any time in 3rd time-zone ?? */
      if (TZ3START <= startMin && startMin <= TZ4START)
      {
         /* if endMin also in 3rd time-zone,
         ** subtract times to get minutes */
         if (endMin <= TZ4START)
         {
            tz3Minutes = endMin - startMin;
         }
         else
         {
	    /* if endMin in another time-zone,
            ** subtract startMin from 4th time-zone startTime
            ** to calculate minutes in the 3rd time-zone
            ** "move" startMin to start of 4th time-zone */
	    tz3Minutes = TZ4START - startMin;
	    startMin = TZ4START;
         }
      }

      /* check for time in 4th time-zone
      ** assumes user time does not cross midnight */
      if (TZ4START <= startMin)
      {
         tz4Minutes = endMin - startMin;
      }
      
      /* get the next user start/end times */
      printf ("Please enter start time & stop time: ");
      scanf ("%d%d", &startTime, &endTime);
   }

   /* print the times and fees for each 6-hour period
   ** print the total user fee
   *****************************************************/

   printf ("\nYour connection time and fees:\n");
   printf ("%3d minutes at %0.2f cost $%8.2f\n",
	   tz1Minutes, TZ1RATE, tz1Minutes * TZ1RATE);
   printf ("%3d minutes at %0.2f cost $%8.2f\n",
	   tz2Minutes, TZ2RATE, tz2Minutes * TZ2RATE);
   printf ("%3d minutes at %0.2f cost $%8.2f\n", 
	   tz3Minutes, TZ3RATE, tz3Minutes * TZ3RATE);
   printf ("%3d minutes at %0.2f cost $%8.2f\n",
	   tz4Minutes, TZ4RATE, tz4Minutes * TZ4RATE);
   totalFee = (TZ1RATE * tz1Minutes) + (TZ2RATE * tz2Minutes)
      + (TZ3RATE * tz3Minutes) + (TZ4RATE * tz4Minutes);
   printf ("------------------------------------\n");
   printf ("\t\tTotal Cost: $%0.2f\n\n", totalFee);
}

