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


10-2 Example: Enhanced Payroll System

In this program, you are going to read the records of different employees from a payroll file. The required record is retrieved using the social security number which you enter from the keyboard. Before using this program, you have to create the text file “payroll.txt” which contains the employee records. You may use any text editor to create this file. The records in the file must be written sequentially without any gaps, and must be consistent with the record description. After the file is created (even with one record), you can use the program to append new records to it. For the purpose of testing the program, you may use the file “payroll.txt” on the companion disk.

These are the contents of the file:

1MGT5
Tammy M. Ockman
Business Manager
232-65-1567
1                        --------------->> The tag field
3333.33
2STF1
Tara S. Strahan
Secretary II
404-38-1132
2                        --------------->> The tag field
8.24
3SAL4
John G. Trainer
Sales Representative
334-88-1234
3                        --------------->> The tag field
0.25                     ------>> Notice additional fields
500.0                                in category "3"
Baton Rouge, LA
1MGT4
Sally A. Abolrous
Technical Editor
434-65-6052
4343.88
1MGT1
James A. Abolrous
President
434-55-6666
1                        --------------->> The tag field
4343.88

Before moving to the discussion, take a look at the following program:

{ ------------------------- figure 10-1 ----------------------------- }
PROGRAM EmployeeDataBase2(INPUT, OUTPUT, PayrollFile, NewFile);
CONST
  FileName = 'payroll.txt';
  TempFile = 'temp.txt';
  Header = '------------- Main Menu --------------';
  Header1 = '--------- Employee DataBase ----------';
  Header2 = '---------- Employee Record -----------';
  Separator = '--------------------------------------';
TYPE
  EmployeeRecord = RECORD
           ID                      :STRING[5];
           Name, Position                   :STRING[20];
           SSN                              :STRING[11];
           CASE Category                    :CHAR OF
            '1' :(MonthlySalary    :REAL);
            '2' :(HourlyRate       :REAL);
            '3' :(Commission,
                  BasicSalary      :REAL;
                  Area                      :STRING[20])
           END;

VAR
  NewFile, PayrollFile   :TEXT;
  EmployeeRec                    :EmployeeRecord;
  Title                  :ARRAY [1..9] OF STRING[20];
  OneLine                        :STRING[80];

{ ------------------------- Procedure ReadRec ------------------------ }
PROCEDURE ReadRec(VAR PayrollFile:TEXT; Employee:EmployeeRecord);
VAR
  SSNumber               :STRING[11];
  Found                  :INTEGER;
BEGIN
  Found:= 0;         {Reset the flag}
  ASSIGN(PayrollFile, FileName);
  RESET(PayrollFile);
  WRITELN;
  WRITE('Please enter the SSN of the employee: ');
  READLN(SSNumber);
  WHILE NOT EOF(PayrollFile) DO
    BEGIN
      WITH Employee DO
      BEGIN
        READLN(PayrollFile, ID);
        READLN(PayrollFile, Name);
        READLN(PayrollFile, Position);
        READLN(PayrollFile, SSN);
        READLN(PayrollFile, Category);
        CASE Category OF
         '1': READLN(PayrollFile, MonthlySalary);
         '2': READLN(PayrollFile, HourlyRate);
         '3': BEGIN
                READLN(PayrollFile, Commission);
                READLN(PayrollFile, BasicSalary);
                READLN(PayrollFile, Area)
              END
        END; { End of CASE structure }
        IF SSNumber = SSN THEN
          BEGIN
            WRITELN(Header2);
            WRITELN(Title[1], ID);
            WRITELN(Title[2], Name);
            WRITELN(Title[3], Position);
            WRITELN(Title[4], SSN);
             CASE Category OF
              '1': WRITELN(Title[5], MonthlySalary:0:2);
              '2': WRITELN(Title[6], HourlyRate:0:2);
              '3': BEGIN
                    WRITELN(Title[7], Commission:0:2);
                    WRITELN(Title[8], BasicSalary:0:2);
                    WRITELN(Title[9], Area)
                   END
              END;      { End of CASE structure }
            Found:= 1
          END
        END             { End of WITH block }
    END;
  CLOSE(PayrollFile);
  IF Found <> 1 THEN
    BEGIN
      WRITELN('SSN not found in file.');
      WRITELN('Please try again.');
      WRITELN
    END
  END;

{ ------------------------- Procedure AddRec ------------------------- }
PROCEDURE AddRec(VAR NewFile, PayrollFile:TEXT;
                      Employee: EmployeeRecord);
BEGIN
  ASSIGN(PayrollFile, FileName);
  RESET(PayrollFile);
  ASSIGN(NewFile, TempFile);
  REWRITE(NewFile);
{ Check for the end of the text file }
  WHILE NOT EOF(PayrollFile) DO
    BEGIN
{ Copy each record from PayrollFile to the NewFile }
      READLN(PayrollFile, OneLine);
      WRITELN(NewFile, OneLine)
    END;
{ Accept a new record from the keyboard }
  WITH Employee DO
   BEGIN
    WRITE('Please enter Employee ID: ');
    READLN(ID);
    WRITE('Name: ');                    READLN(Name);
    WRITE('Position: ');                READLN(Position);
    WRITE('SSN (xxx-xx-xxxx): ');       READLN(SSN);
    WRITE('Payroll category: ');        READLN(Category);
     CASE Category OF
      '1': BEGIN
            WRITE('Monthly Salary: ');
            READLN(MonthlySalary);
           END;
      '2': BEGIN
            WRITE('Rate: ');
            READLN(HourlyRate);
           END;
      '3': BEGIN
            WRITE('Commission: ');
            READLN(Commission);
            WRITE('Basic salary: ');
            READLN(BasicSalary);
            WRITE('Area: ');
            READLN(Area)
     END
   END;
{ Store the information in NewFile }
   WRITELN(NewFile, ID);
   WRITELN(NewFile, Name);
   WRITELN(NewFile, Position);
   WRITELN(NewFile, SSN);
   WRITELN(NewFile, Category);
   CASE Category OF
    '1': WRITELN(NewFile, MonthlySalary:0:2);
    '2': WRITELN(NewFile, HourlyRate:0:2);
    '3': BEGIN
          WRITELN(NewFile, Commission:0:2);
          WRITELN(NewFile, BasicSalary:0:2);
          WRITELN(NewFile, Area)
         END
     END
  END;
  CLOSE(NewFile);
  CLOSE(PayrollFile);
{ Copy NewFile back to Payroll File }
  ASSIGN(PayrollFile, FileName);
  REWRITE(PayrollFile);
  ASSIGN(NewFile, TempFile);
  RESET(NewFile);
  WHILE NOT EOF(NewFile) DO
   BEGIN
    READLN(NewFile, OneLine);
    WRITELN(PayrollFile, OneLine)
   END;
  CLOSE(NewFile);
  ERASE(NewFile); { Erase the temporary file }
  CLOSE(PayrollFile)
  END;

{ ------------------------- Procedure Menu --------------------------- }
PROCEDURE Menu;
VAR
  Option:INTEGER;
BEGIN
  WRITELN(Header);
  WRITELN;
  WRITELN('1. Display an employee record.');
  WRITELN('2. Add a new employee.');
  WRITELN('3. Exit.');
  WRITELN(Separator);
  WRITE('Make a choice and press a number: ');
  READLN(Option);
  CASE Option OF
    1: ReadRec(PayrollFile, EmployeeRec);
    2: AddRec(NewFile, PayrollFile, EmployeeRec);
    3: Exit
  END;
  Menu
END;

{ --------------------------- Main Program --------------------------- }
BEGIN
{ Assign titles }
  Title[1]:= 'ID: ';
  Title[2]:= 'Name: ';
  Title[3]:= 'Position: ';
  Title[4]:= 'SSN: ';
  Title[5]:= 'Salary: ';
  Title[6]:= 'Rate: ';
  Title[7]:= 'Commission: ';
  Title[8]:= 'Basic Salary: ';
  Title[9]:= 'Area: ';
  Menu
END.


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.