/*******************************************
 ** 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 called a time-zone 
 ** and charges a different rate.  When complete, the
 ** program displays the number of minutes connected during
 ** each quarter of the day and the total user connection fee
 **
 ** Algorithm:
 **    while the start time and end time not both zero
 **      change start/end time to "minutes since midnight"
 **      determine which time-zone the start and stop times
 **       are in, and calculate the cost
 **      print results
 **
 ** 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 --
** the day is divided into 4 parts -- I call them "time-zones"
** so my #defines and variables start with TZ or tz
**********************************************************/

#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;         /* min connected in 1st time-zone       */
   int tz2Minutes = 0;         /* min connected in 2nd time-zone       */
   int tz3Minutes = 0;         /* min connected in 3rd time-zone       */
   int tz4Minutes = 0;         /* min connected in 4th time-zone       */
   int startTime, endTime;     /* military hours & minutes             */
   int startMin, endMin;       /* as "minutes since midnight"          */
   int minute;                 /* for-loop index  (start of minu       */


   /* 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);

      /* find which timezone startMin and EndMin are in
      ** and calc minutes for each affected time-zone
      ***********************************************/

      /* is startMin in 1st time zone? */
      if (TZ1START <= startMin && startMin < TZ2START)
      {
	 if (TZ1START < endMin && endMin <= TZ2START)
	 {
	    /* startMin in 1st TZ and endMin in 1st TZ */
	    tz1Minutes += (endMin - startMin);
	 }
	 else if (TZ2START < endMin && endMin <= TZ3START)
	 {
	    /* startMin in 1st TZ and endMin in 2nd TZ */
	    tz1Minutes += (TZ2START - startMin);
	    tz2Minutes += (endMin - TZ2START);
	 }
	 else if (TZ3START < endMin && endMin <= TZ4START)
	 {
	    /* startMin in 1st TZ and endMin in 3rd TZ */
	    tz1Minutes += (TZ2START - startMin);
	    tz2Minutes += (TZ3START - TZ2START);
	    tz3Minutes += (endMin - TZ3START);
	 }
	 else
	 {
	    /* startMin in 1st TZ and endMin must be 4th TZ */
	    tz1Minutes += (TZ2START - startMin);
	    tz2Minutes += (TZ3START - TZ2START);
	    tz3Minutes += (TZ4START- TZ3START);
	    tz4Minutes += (endMin - TZ4START);
	 }
      }
      /* is startMin in 2nd time-zone? */
      else if (TZ2START <= startMin && startMin < TZ3START)
      {
	 if (TZ2START < endMin && endMin <= TZ3START)
	 {
	    /* startMin in 2nd TZ and endMin in 2nd TZ */
	    tz2Minutes += (endMin - startMin);
	 }
	 else if (TZ3START < endMin && endMin <= TZ4START)
	 {
	    /* startMin in 2nd TZ and endMin in 3rd TZ */
	    tz2Minutes += (TZ3START - startMin);
	    tz3Minutes += (endMin - TZ3START);
	 }
	 else
	 {
	    /* startMin in 2nd TZ and endMin must be 4th TZ */
	    tz2Minutes += (TZ3START - startMin);
	    tz3Minutes += (TZ4START- TZ3START);
	    tz4Minutes += (endMin - TZ4START);
	 }
      }
      /* is startMin in 3rd time-zone? */
      else if (TZ3START <= startMin && startMin < TZ4START)
      {
	 if (TZ3START < endMin && endMin <= TZ4START)
	 {
	    /* startMin in 3nd TZ and endMin in 3nd TZ */
	    tz2Minutes += (endMin - startMin);
	 }
	 else
	 {
	    /* startMin in 3rd TZ and endMin must be 4th TZ */
	    tz3Minutes += (TZ4START- startMin);
	    tz4Minutes += (endMin - TZ4START);
	 }
      }
      /* startMin (and also endMin) must be in 4th TZ */
      else 
      {
	 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 time-zone
   ** 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);
}

