Download presentation
Presentation is loading. Please wait.
Published byRussell Waters Modified over 8 years ago
1
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11
2
Chapter 112 Objectives After studying Chapter 11, you should be able to: Understand sequential files and the need for merging them Create the mainline and housekeeping() logic for a merge program Create the mainLoop() and finishUp() modules for a merge program Modify the housekeeping() module to check for eof
3
11 Chapter 113 Objectives After studying Chapter 11, you should be able to: 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
4
11 Chapter 114 Understanding Sequential Data Files and the Need for Merging Files A sequential file is a file where records are stored one after another in some order Examples of sequential files include: –A file of employees stored in order by Social Security number –A file of parts for a manufacturing company stored in order by part number –A file of customers for a business stored in alphabetical order by last name
5
11 Chapter 115 Understanding Sequential Data Files and the Need for Merging Files Merging files involves combining two or more files while maintaining the sequential order Before you can merge files, two closely related conditions must be met: –Each file used in the merge must be sorted in order based on some field –Each file used in the merge must be sorted in the same order (ascending or descending) on the same field as the others
6
11 Chapter 116 Understanding Sequential Data Files and the Need for Merging Files
7
11 Chapter 117 Creating the Mainline and housekeeping() Logic for a Merge Program The mainline logic for a program that merges two files is the same main logic you’ve used before in other programs: a housekeeping() module, a mainLoop() module that repeats until the end of the program, and a finishUp() module When you declare variables within the housekeeping() module, you must declare the bothAtEof flag and initialize it to any value other than “Y” A data file contains data only for another computer program to read
8
11 Chapter 118 Flowchart for Mainline Logic of Merge Program
9
11 Chapter 119 Flowchart for housekeeping() Module in Merge Program, Version 1
10
11 Chapter 1110 Creating the mainLoop() and finishUp() Modules for a Merge Program When you begin the mainLoop() module, two records—one from eastFile and one from westFile —are sitting in the memory of the computer Using the sample data from Figure 11-1, you can see that the record containing “Able” should be written to the output file while Chen’s record waits in memory because the eastName “Able” is alphabetically lower than the westName “Chen”
11
11 Chapter 1111 Beginning of the mainLoop() Module
12
11 Chapter 1112 Continuation of mainLoop() Module for Merge Program
13
11 Chapter 1113 Names from Two Files to Merge
14
11 Chapter 1114 The finishUp() Module for Merge Program
15
11 Chapter 1115 Modifying the housekeeping() Module to Check for Eof It is good practice to check for eof every time you read
16
11 Chapter 1116 Master and Transaction File Processing You use a master file to hold relatively permanent data about your customers The file with the customer purchases is a transaction file that holds more temporary data Often, you periodically use a transaction file to find a matching record in a master file, so you can update the master file by making changes to the values in its fields
17
11 Chapter 1117 Master and Transaction File Processing Here are a few other examples of files that have a master-transaction relationship: –A library maintains a master file of all patrons and a transaction file with information about each book or other items checked out –A college maintains a master file of all students and a transaction file for each course registration –A telephone company maintains a master file of every telephone line (number) and a transaction file with information about every call
18
11 Chapter 1118 Master and Transaction File Processing When you update a master file, you can take two approaches: –You can actually change the information on the master file –You can create a copy of the master file, making the changes on the new version. The saved version of a master file is the parent file; the updated version is the child file
19
11 Chapter 1119 Matching Files to Update Fields in Master File Records The logic you use to perform a match between master and transaction file records is similar to the logic you use to perform a merge As with a merge, you must begin with both files sorted in the same order on the same field
20
11 Chapter 1120 Matching Files to Update Fields in Master File Records
21
11 Chapter 1121 Matching Files to Update Fields in Master File Records Figure 11-16 shows some sample data you can use to walk through the logic for this program
22
11 Chapter 1122 Updating Records in Sequential Files Assume you have a master employee file as shown on the left side of Figure 11-19 Sometimes a new employee is hired and must be added to this file, or an employee quits and must be removed from the file Sometimes you need to change an employee record by recording a raise in salary for example, or a change of department
23
11 Chapter 1123 Updating Records in Sequential Files An addition record on the transaction file would contain data in the fields transNum, transName, transSalary, and transDept ; an addition record represents a new employee, and data for all the fields must be entered for the first time
24
11 Chapter 1124 Updating Records in Sequential Files A deletion record really needs data in only two fields—a “D” in transCode and a number in transNum A change record contains a “C” code and needs data only in fields that are going to be changed The mainline logic for an update program is the same as that for the merging and matching programs shown in Figure 11-13
25
11 Chapter 1125 The readEmp() and readTrans() Modules
26
11 Chapter 1126 Updating Records in Sequential Files As shown in Figure 11-23, within the theyAreEqual() module you check the transCode and perform one of three actions: –If the code is an “A”, print an error message –If the code is a “C”, you need to make changes –If the code is not an “A” or a “C”, it must be a “D” and the record should be deleted If the transaction record contains code “A”, that’s fine because an addition transaction shouldn’t have a master record
27
11 Chapter 1127 The translsLargerThanEmp() Module
28
11 Chapter 1128 The finishUp() Module for Update Program
29
11 Chapter 1129 Summary A sequential file is a file whose records are stored one after another in some order The mainline logic for a program that merges two files contains a housekeeping() module, a mainLoop() module that repeats until the end of the program, and a finishUp() module When beginning the mainLoop() module for a merge program, you compare records from each file to be merged
30
11 Chapter 1130 Summary In the housekeeping() module of a merge program, you read from two input files, check for the end of each file, and set a flag if both input files are at eof You use a master file to hold relatively permanent data and a transaction file to hold more temporary data that corresponds to records in the master file The logic you use to perform a match between master and transaction file records involves comparing the files to determine whether there is a transaction for each master record; when there is, you update the master record
31
11 Chapter 1131 Summary Using the logic that allows multiple transactions per master file record, whenever a transaction matches a master file record, you process the transaction, then you read only from the transaction file A sophisticated update program allows you to make changes to data in a record and update a master file by adding new records or eliminating records you no longer want
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.