
PROGRAM Payroll (input, output);

{******************************************************}
{*Written by:  Joe Cool                                                                     }
{*Pascal 2210-0007           Sept. 6, 1997             }
{*155-55-9999                                                                                  }
{******************************************************}
{* This is a payroll program that will accept as input the 
 number of minutes worked. It will calculate the number
 of hours worked and the pay, given a particular pay rate.
 The pay rate is based on minutes worked. It will then
 allow the user to save the information to a text file.
 A history of paydue information is kept. The program will
 also allow the user to list out the pay history. The user
 can run the program as many times as he pleases before
 quitting.  This program can be optimized. It has additional
 code that is not necessary but there for teaching purposes
 only... i.e. for teaching how to use If/Then's etc.
 See what you can do to streamline it and then email it to me}
{******************************************************}

USES Crt;


CONST
     Rate = 20;                    {rate is .20 cents per minutes}
     MoneyLength = 7;              {field width for currency}


VAR
      TimeWorked, Ans:	Integer;
      PayDue:		Real;
      RunProgram,List:	Boolean;
      PayrollFile:      Text;




PROCEDURE OutputTime (Minutes:	integer);

{*******************************************************}
{PRE: Input is accepted from user for the number of minutes
      they have worked.
 PROCESS: Given a number of minutes >= 0; the number of
          hours worked is calculated and displayed.
 POST: The number of minutes and hours worked is displayed
       and the actual parameter "Minutes" is unchanged. }
{*******************************************************}

     VAR
           Hours:	Integer;
     
     BEGIN {procedure OutputTime}
           
           Hours:= Minutes DIV 60;
           Minutes:= Minutes MOD 60;
           Writeln(Hours, ' hours and ', Minutes, ' minutes. ');

     END; {procedure OutputTime}




PROCEDURE ComputePay (Minutes:	integer; VAR Pay: real);

{*******************************************************}
{PRE: Minutes is equal to the time worked in minutes.
 PROCESS: The global constant Rate is used to calculate the
	  amount of pay due
 POST: Pay is set to the pay due expressed in dollars.
{*******************************************************}

      VAR
           PennyPay:	Integer;

       BEGIN  {Procedure ComputePay}
	
	PennyPay:= Rate * Minutes;
	Pay:= PennyPay/100;

       END; {Procedure ComputePay}




PROCEDURE RecordData (VAR Payroll: text; Pay: real);

{*******************************************************}
{PRE: Passes in a file that may or may not have data
 PROCESS: Writes payroll information to the file.
 POST: A file with payroll information added.
{*******************************************************}

       BEGIN  {Procedure RecordData}

        APPEND(Payroll);
        Writeln(Payroll, Pay:7:2);
        CLOSE(Payroll);

       END; {Procedure RecordData}




PROCEDURE ListData (VAR Payroll: text);

{*******************************************************}
{PRE: Passes in a file that may or may not have data
 PROCESS: Reads payroll information to the file.
 POST: Lists data on screen.
{*******************************************************}
       VAR
           History: Real;

       BEGIN  {Procedure ListData}

        RESET(Payroll);
        Write('The following Listing contains the contents ');
        Write('of the Payroll File'); Writeln;
        Writeln;
        
        IF EOF(Payroll) Then Writeln('The File is Empty.')
        ELSE
        REPEAT
        {*******************************************************}
        {PRE: Payroll file exits and is not empty, EOF()=false;
         PROCESS: Reads the contents of the file.
         POST: Information is displayed to the screen and EOF()=true;
        {*******************************************************}

             Readln(Payroll, History);
             Writeln('$ ',History:7:2);

        UNTIL EOF(Payroll);   {Repeat Loop End}

        CLOSE(Payroll);

       END; {Procedure ListData}



Procedure Response;
{*******************************************************}
{PRE: the global variable "ans" exists
 PROCESS: writes out response info and accepts input 
 POST: input is accepted and written into "ans"
{*******************************************************}

     BEGIN
      Writeln('Enter 1 = ''yes'' or 0 = ''no'' ');  
      Writeln;
      Readln(Ans);
     END;



FUNCTION cube (VAR num: real) :real;
      VAR
	   X: real;

      BEGIN
	   num:= X * X * X;
      END;


BEGIN {Main Program}

      TimeWorked:= 0;	 	        {initializing variables}
      Ans:= 0;
      PayDue:= 0.0;
      RunProgram:= false;
      List:= false;
      Assign(PayrollFile, 'pay.txt'); {assigning the actual file to the variable file}

      Clrscr;


      Writeln;
      Write('Would you like to calculate your pay rate? ');
      Response;

      IF Ans = 1 Then RunProgram := true
         Else RunProgram := false;

	While RunProgram Do
      {*******************************************************}
      {PRE: Run is true
       PROCESS: The program calculates pay due until the
	        user wishes to stop.
       POST: Run is false
      {*******************************************************}

	 BEGIN  {While Run Loop}

           Writeln;
           Writeln('Please enter the number of minutes you worked: ');
           Writeln;
     	     Readln(TimeWorked);
	     Write('You worked: ');
	      
           OutputTime(TimeWorked);	  	  {prints out hours and mins worked}

	     ComputePay(Timeworked, Paydue);  {computes the pay that is due}
           Writeln;
	     Writeln('At ', Rate, ' cents per minute, ');
	     Writeln('you earned:  $', PayDue:MoneyLength:2);
           Writeln;

	     {At this point, the original program ended. The remaining sections
		are built up to teach how to use procedures and text files}

           Ans:= 0;	{reinitialize variables for reuse}
           Writeln;
           Writeln('Would you like to save recent pay history?');
           Response;    {save paydue to a file}

           IF Ans = 1 Then List:= true
               Else List:= false;

               IF List Then RecordData(PayrollFile, PayDue);

           Ans:= 0; List:=false; 	{reinitialize variables for reuse}
           Writeln;
	     Write('Would you like to run the program again? ');
	     Response;	{controls the while loop exit criteria}

           IF Ans = 1 Then RunProgram := true
                Else RunProgram := false;

           END; {While Run Loop}


     Ans:= 0; List:=false; 	{reinitialize variables for reuse}
     Writeln;
     Writeln('Would you like to see recent payroll history?');
     Response;

        IF Ans = 1 Then List:= true
             Else List:= false;

        IF List Then ListData(PayrollFile);  {Else do nothing}

     Writeln;
     Writeln('Goodbye');
     Readln;

END. {Main Program}

