home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Application 3: Payroll

The file you have just created contains a good deal of information about employees and can be used for more than one purpose. You can use some or all of the information in this file to create different reports or other data files. In the following application, the file “EMPFILE.TXT” is read but only three fields from each record are used: “ID,” “Name,” and “HourlyRate.” The program first displays an employee’s information on the screen, then the user is prompted to enter “HoursWorked” for this employee. The “Wages” are then calculated by multiplying “HourlyRate” and “HoursWorked.” After processing each record the “ID,” “Name,” and “Wages” are stored in a new file “PAYFILE.TXT.” The new file is used to produce a payroll report for this pay period.

{ ------------------------------- figure 9-7 ----------------------------- }
PROGRAM PayRoll(INPUT,OUTPUT,MasterFile,PayFile);
TYPE
  AddressRecord = RECORD
             Street          :STRING[18];
             City                   :STRING[15];
             State           :STRING[2];
             Zip                    :String[5];
           END;
  EmployeeRecord = RECORD
             ID                     :INTEGER;
             Name                   :STRING[20];
             AddressRec             :AddressRecord;
             Phone                  :STRING[12];
             Rate                   :REAL;
             MaritalStatus   :CHAR;
           END;
  PayRecord = RECORD
         ID            :INTEGER;
         Name          :STRING[20];
         Wages         :REAL;
       END;
VAR
  MasterFile, PayFile  :TEXT;
  EmployeeRec                :EmployeeRecord;
  PayRec                     :PayRecord;
  HoursWorked, Wages         :REAL;

{ --------------- Procedure GetInfo ------------------ }
{ This procedure reads the employee file "EMPFILE.TXT"
  and displays the ID, Name, and Hourly Rate. }
PROCEDURE GetInfo(VAR F:TEXT);
BEGIN
  WITH EmployeeRec DO
    WITH AddressRec DO
      BEGIN
        READLN(F,ID);        WRITELN('ID: ',ID);
        READLN(F,Name);      WRITELN('Name: ',Name);
        READLN(F,Street);
        READLN(F,City);
        READLN(F,State);
        READLN(F,Zip);
        READLN(F,Phone);
        READLN(F,Rate);      WRITELN('Hourly rate: $', Rate:0:2);
        READLN(F,MaritalStatus);
      END;
END;

{ --------------- Procedure CalcWages ---------------- }
{ This procedure is used to calculate wages.
  The result is returned to the main program }
PROCEDURE CalcWages(HoursWorked:REAL; VAR Wages:REAL);
BEGIN
  WITH EmployeeRec DO
    Wages:= Hoursworked * Rate;
  Wages:= ROUND(100 * Wages) / 100 { rounding cents }
END;

{ -------------- Procedure FilePayRoll --------------- }
{ This procedure is used to write one record to
  the output file “PAYFILE.TXT” }
PROCEDURE FilePayRoll(VAR F:TEXT; VAR P:TEXT; Wages:REAL);
BEGIN
   WITH EmployeeRec DO
     BEGIN
       PayRec.ID:= ID;
       PayRec.Name:= Name;
       PayRec.Wages:= Wages
     END;
   WITH PayRec DO
     WRITELN(P, ID:3, Name:20, Wages:10:2)
END;

{ --------------- Main Program ------------------ }
BEGIN
  ASSIGN(MasterFile, 'EMPFILE.TXT'); RESET(MasterFile);
  ASSIGN(PayFile, 'PAYFILE.TXT'); REWRITE(PayFile);
  WHILE NOT EOF(MasterFile) DO
    BEGIN
      GetInfo(MasterFile);
      WRITE('Please enter hours worked for this pay period: ');
      READLN(HoursWorked);
      CalcWages(HoursWorked, Wages);
      FilePayRoll(MasterFile, PayFile, Wages)
    END;
  CLOSE(MasterFile);
  CLOSE(PayFile)
END.

Sample run:

Assume that the file “EMPFILE.TXT” contains three records. The program will use these records as follows:

ID: 122                             ----> Information from file
Name: Tammy M. Ockman                     ----> Information from file
Hourly rate: $22.45                 ----> Information from file
Please enter hours worked for this pay period: 160 ----> Entered by user
ID: 123
Name: Tara S. Strahan
Hourly rate: $15.24
Please enter hours worked for this pay period: 160
ID: 125
Name: John G. Trainer
Hourly rate: $28.55
Please enter hours worked for this pay period: 140.5

The program creates the file “PAYFILE.TXT” containing the following records:

122   Tammy M. Ockman 3592.00
123   Tara S. Strahan 2438.40
125   John G. Trainer 4011.28

The program consists of three procedures:

• “GetInfo” to read one record of the file “EMPFILE.TXT” and display only the selected fields. Notice that you have to read all of the record fields even if you do not need them all.
• “CalcWages” to carry out the calculations.
• #147;FilePayRoll” to write the record “PayRec” to the file “PAYFILE.TXT.”

These are some important points of the program:

1.  When file variables (such as “MasterFile” and “PayFile”) are passed to subprograms, they must be passed as variable parameters (using VAR). The type TEXT is used with such parameters:
PROCEDURE FilePayRoll(VAR F:TEXT; VAR P:TEXT; Wages:REAL);
2.  Some identifiers (such as “Name” and “ID”) are used in both “EmployeeRec” and “PayRec.” This does not cause any problem because they are all fielded variables; remember that the scope of a fielded variable is limited to its own record. Also, the identifier “Wages” was declared both as a global variable and as a fielded variable (in the record “PayRec”) and was also used as a local variable in the procedure “FilePayRoll.”
3.  Take a look at these assignment statements in the procedure “FilePayRoll”:
WITH EmployeeRec DO
BEGIN
PayRec.ID:= ID;
PayRec.Name:= Name;
PayRec.Wages:= Wages

The first two statements copy the values of the fields “ID” and “Name” from “EmployeeRec” to the corresponding fields in “PayRec.” The WITH statement modifies only the variables which belong to the record “EmployeeRec” (“ID” and “Name”). A variable such as “PayRec.ID” is not affected by the WITH statement because it is explicitly modified by “PayRec.” In the last statement, no variables at all are affected by the WITH statement.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.