Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Logic and Design Fourth Edition, Comprehensive

Similar presentations


Presentation on theme: "Programming Logic and Design Fourth Edition, Comprehensive"— Presentation transcript:

1 Programming Logic and Design Fourth Edition, Comprehensive
Chapter 11 Sequential File Merging, Matching, and Updating

2 Objectives Understand sequential files and the need for merging them
Create the mainline and housekeeping() logic for a merge program Create the mergefiles() and finishUp() modules for a merge program Modify the housekeeping() module to check for eof Programming Logic and Design, Fourth Edition, Comprehensive

3 Objectives (continued)
Understand master and transaction file processing Match files to update master file fields Allow multiple transactions for a single master file record Update records in sequential files Programming Logic and Design, Fourth Edition, Comprehensive

4 Understanding Sequential Data Files and the Need for Merging Files
Sequential file: records are stored one after another in some order Temporal order: order based on time Records are often stored based on the contents of one or more fields within each record Merging files: combining two or more files while maintaining the sequential order Programming Logic and Design, Fourth Edition, Comprehensive

5 Understanding Sequential Data Files and the Need for Merging Files (continued)
Two conditions required for merging files: Each file must have same record layout Each file must be sorted in the same order based on the same field Programming Logic and Design, Fourth Edition, Comprehensive

6 Understanding Sequential Data Files and the Need for Merging Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

7 Creating the Mainline and housekeeping() Logic for a Merge Program
Mainline logic similar to other file processing programs, except for handling two files With two input files, must determine when both files are at eof In housekeeping() module: Define a flag variable to indicate that both files have reached eof Must define two input files At end of module, read one record from each input file Programming Logic and Design, Fourth Edition, Comprehensive

8 Creating the Mainline and housekeeping() Logic for a Merge Program (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

9 Creating the Mainline and housekeeping() Logic for a Merge Program (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

10 Creating the mergeFiles() and finishUp() Modules for a Merge Program
Two records are in memory, one from each file Must decide which record to write to output file first After writing one record, must read again from the same file Then compare both records again to determine which is written next Must always have one record from each file to compare before writing one Programming Logic and Design, Fourth Edition, Comprehensive

11 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

12 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
Merge technique: Compare two records Write the record with the lower value Read another record from the same input file Programming Logic and Design, Fourth Edition, Comprehensive

13 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

14 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
Data to be merged: Programming Logic and Design, Fourth Edition, Comprehensive

15 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
High value: a value that is greater than any possible value in a field Because the files are different lengths, you must continue to process the longer file To use the same loop, you must handle the comparison operation Technique: Use a high value for the comparison to guarantee that the record will be “selected” and written Programming Logic and Design, Fourth Edition, Comprehensive

16 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

17 Creating the mergeFiles() and finishUp() Modules for a Merge Program (continued)
When both files are at eof, execute the finishUp() module Programming Logic and Design, Fourth Edition, Comprehensive

18 Modifying the housekeeping() Module in the Merge Program to Check for eof
Must check for eof during first read, in case one file is empty Programming Logic and Design, Fourth Edition, Comprehensive

19 Modifying the housekeeping() Module in the Merge Program to Check for eof (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

20 Modifying the housekeeping() Module in the Merge Program to Check for eof (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

21 Master and Transaction File Processing
When merging files, the records usually contain the same fields in the same order When not the same format, one file may be used to update another file Master file: holds relatively permanent data Transaction file: contains temporary data to be used to update the master file Matching record: record in the master file with same key field value as a record in the transaction file Programming Logic and Design, Fourth Edition, Comprehensive

22 Master and Transaction File Processing (continued)
Matching may be done for several reasons: To update the master file: transaction file record is used to make changes to the master file record Use information from both master and transaction file to create another entity, such as an invoice There may be 0, 1, or many transaction records matching each master file record Two approaches to updating a master file: Update in place, changing the existing master file Create a copy of the master, making the changes in the new version Programming Logic and Design, Fourth Edition, Comprehensive

23 Master and Transaction File Processing (continued)
When a new copy of the master is created during updating: Parent file: original saved master file Child file: updated version of master file Programming Logic and Design, Fourth Edition, Comprehensive

24 Matching Files to Update Fields in Master File Records
Logic used to match records in two files is similar to merge logic Programming Logic and Design, Fourth Edition, Comprehensive

25 Matching Files to Update Fields in Master File Records (continued)
Goal: create a new master file with updated custTotalSales field Technique: Inspect each master file record Determine if there are any new transaction records matching the master file record Add transaction amount to master file custTotalSales field in master record Pre-sorting both master and transaction files will improve the efficiency Programming Logic and Design, Fourth Edition, Comprehensive

26 Matching Files to Update Fields in Master File Records (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

27 Matching Files to Update Fields in Master File Records (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

28 Matching Files to Update Fields in Master File Records (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

29 Matching Files to Update Fields in Master File Records (continued)
3 possibilities when comparing records: transCustNumber value = custNumber transCustNumber value > custNumber transCustNumber value < custNumber If =, update custTotalSales If >, there was no sale for that customer If <, there is no matching master record  error Programming Logic and Design, Fourth Edition, Comprehensive

30 Matching Files to Update Fields in Master File Records (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

31 Matching Files to Update Fields in Master File Records (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

32 Allowing Multiple Transactions for a Single Master File Record
To handle multiple transactions for a single master file record, you must Match a transaction to the master file record Process that transaction Read again only from the transaction file Determine if the new transaction applies to the same master record Programming Logic and Design, Fourth Edition, Comprehensive

33 Allowing Multiple Transactions for a Single Master File Record (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

34 Updating Records in Sequential Files
Application must be able to: Make changes to data in a master file record Add new records to master file Eliminate records in master file Transaction file records usually contain a transaction code to indicate the type of action to take Programming Logic and Design, Fourth Edition, Comprehensive

35 Updating Records in Sequential Files (continued)
Types of transaction records: Addition: create a new master file record Deletion: remove a master file record Change: modify data in a master file record Programming Logic and Design, Fourth Edition, Comprehensive

36 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

37 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

38 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

39 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

40 Updating Records in Sequential Files (continued)
When transaction record matches master file record: If transaction code = “A”  error If transaction code = “C”, make the changes in the master record If transaction code is not “A” or “C”, must be a “D”, so do not write existing master file record to the new master file When complete, read one new record from each of the input files Programming Logic and Design, Fourth Edition, Comprehensive

41 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

42 Updating Records in Sequential Files (continued)
If master file record > transaction record, there is no master file record that matches the transaction record If transaction code is not an “A”  error When complete, do not read another master record There may be more transactions for the newly added record Programming Logic and Design, Fourth Edition, Comprehensive

43 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

44 Updating Records in Sequential Files (continued)
If master file record < transaction record, there is no transaction record that matches the transaction record Write the master file record to the new master file Read the next master file record Programming Logic and Design, Fourth Edition, Comprehensive

45 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

46 Updating Records in Sequential Files (continued)
If transaction file reaches eof first, set transEmpNum to 999 Master file record will always be lower, causing master file record to be written to new master file If master file reaches eof first, set EmpNum to 999 Transaction record will always be lower, causing new records to be added (if code = “A”) or error to be generated (if code not = “A”) When both files reach eof, set bothAtEof = “Y” and perform finishUp() module Programming Logic and Design, Fourth Edition, Comprehensive

47 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

48 Updating Records in Sequential Files (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

49 Summary Sequential file: records are stored one after another in some order Key field: field on which you sort records When merging two files Compare records from each input file Write the appropriate record from one file Read another record from the same file When eof is reached on one input file, set key field to a high value and finish processing the other file Programming Logic and Design, Fourth Edition, Comprehensive

50 Summary (continued) Master file: holds relatively permanent data
Transaction file: holds temporary data that corresponds to records in master file To match transaction file and master file, determine if a transaction record exists for each master If yes: update or delete the master based on the transaction code If no: add a new master record based on the transaction code Programming Logic and Design, Fourth Edition, Comprehensive

51 Summary (continued) With multiple transactions per master file record
Process a matching transaction record Read again from the transaction file When transaction record > master file record, read from master file Transaction file records usually have the same fields as the master, with a transaction code field Programming Logic and Design, Fourth Edition, Comprehensive


Download ppt "Programming Logic and Design Fourth Edition, Comprehensive"

Similar presentations


Ads by Google