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-4 Updating Records

The algorithm to update records in the file is as follows:

1.  Enter the social security number of the employee whose record is to be updated.
2.  Open the payroll file for reading, and a temporary file for writing.
3.  Read the payroll file up to the end-of-file. For each record, check the SSN field against the social security number.
4.  Copy each record to the temporary file until you reach the record to be updated.
5.  For the record to be updated, read the new data from the keyboard and write them to the temporary file.
6.  Copy the rest of the records into the temporary file.
7.  Copy the temporary file into the original payroll file.
8.  Delete the temporary file.

You may add the following procedure to the program: (This procedure is on the companion disk under the name “upd-proc.pas.”)

{ ---------------------- Procedure UpdateRec ------------------------- }
PROCEDURE UpdateRec(VAR NewFile, PayrollFile:TEXT;
                        Employee:EmployeeRecord);
VAR
  SSNumber   :STRING[11];
  Found      :INTEGER;
BEGIN
  Found:= 0;
  ASSIGN(PayrollFile, FileName);
  RESET(PayrollFile);
  ASSIGN(NewFile, TempFile);
  REWRITE(NewFile);
  WRITE('Please enter the SSN of the employee to be updated: ');
  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(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 of CASE structure }
         END
       ELSE
         BEGIN
          Found:= 1;
          WRITELN('Please enter the updated information:');
          WRITE('ID: ');   READLN(ID);
          WRITELN(NewFile, ID);
          WRITE('Name: ');    READLN(Name);
          WRITELN(NewFile, Name);
          WRITE('Position: '); READLN(Position);
          WRITELN(NewFile, Position);
          WRITELN(NewFile, SSN);
          WRITE('Category: '); READLN(Category);
          WRITELN(NewFile, Category);
           CASE Category OF
            '1': BEGIN
                  WRITE('Salary: ');
                  READLN(MonthlySalary);
                  WRITELN(NewFile, MonthlySalary:0:2)
                 END;
            '2': BEGIN
                  WRITE('Hourly Rate: ');
                  READLN(HourlyRate);
                  WRITELN(NewFile, HourlyRate:0:2)
                 END;
            '3': BEGIN
                  WRITE('Commission: ');
                  READLN(Commission);
                  WRITELN(NewFile, Commission:0:2);
                  WRITE('Basic Salary: ');
                  READLN(BasicSalary);
                  WRITELN(NewFile, BasicSalary:0:2);
                  WRITE('Area: ');
                  READLN(Area);
                  WRITELN(NewFile, Area)
                 END
             END; { End of CASE structure }
         END
      END { End of WITH block }
    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);
{ User Messages }
  IF Found =1 THEN
    WRITELN('The employee ', SSNumber, ' is updated.')
  ELSE
    BEGIN
      WRITELN('The SSN ', SSNumber, ' is not found.');
      WRITELN('Check the number and try again.');
      WRITELN
    END
  END;

The “Found” flag is of the INTEGER type. However, you may use any other type such as BOOLEAN which makes your program more readable. With a boolean flag, you may use statements like “IF Found” and “IF NOT Found.”

You also need to modify the menu procedure in order to incorporate the update option, as follows: (This procedure is on the companion disk under the name “mnu-pro2.pas.”)

{ ------------------------- 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. Delete an employee.');
  WRITELN('4. Update an employee record.');
  WRITELN('5. 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: DelRec(NewFile, PayrollFile, EmployeeRec);
    4: UpdateRec(NewFile, PayrollFile, EmployeeRec);
    5: Exit
  END;
  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.