Sequential File Merging, Matching, and Updating

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

description

11. Sequential File Merging, Matching, and Updating. Programming Logic and Design, Second Edition, Comprehensive. Objectives. After studying Chapter 11, you should be able to: Understand sequential files and the need for merging them - PowerPoint PPT Presentation

Transcript of Sequential File Merging, Matching, and Updating

Page 1: Sequential File Merging, Matching, and Updating

Chapter 11 1

11

Sequential File Merging, Matching, and Updating

Programming Logic and Design, Second Edition, Comprehensive

11

Page 2: Sequential File Merging, Matching, and Updating

Chapter 11 2

11

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

Page 3: Sequential File Merging, Matching, and Updating

Chapter 11 3

11

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

Page 4: Sequential File Merging, Matching, and Updating

Chapter 11 4

11Understanding 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

Page 5: Sequential File Merging, Matching, and Updating

Chapter 11 5

11Understanding 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

Page 6: Sequential File Merging, Matching, and Updating

Chapter 11 6

11Understanding Sequential

Data Files and the Need for Merging Files

Page 7: Sequential File Merging, Matching, and Updating

Chapter 11 7

11Creating 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

Page 8: Sequential File Merging, Matching, and Updating

Chapter 11 8

11Flowchart for Mainline

Logic of Merge Program

Page 9: Sequential File Merging, Matching, and Updating

Chapter 11 9

11Flowchart for housekeeping()

Module in Merge Program, Version 1

Page 10: Sequential File Merging, Matching, and Updating

Chapter 11 10

11Creating 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”

Page 11: Sequential File Merging, Matching, and Updating

Chapter 11 11

11

Beginning of the mainLoop() Module

Page 12: Sequential File Merging, Matching, and Updating

Chapter 11 12

11Continuation of mainLoop() Module for Merge Program

Page 13: Sequential File Merging, Matching, and Updating

Chapter 11 13

11

Names from Two Files to Merge

Page 14: Sequential File Merging, Matching, and Updating

Chapter 11 14

11The finishUp()

Module for Merge Program

Page 15: Sequential File Merging, Matching, and Updating

Chapter 11 15

11Modifying the

housekeeping() Module to Check for Eof

• It is good practice to check for eof every time you read

Page 16: Sequential File Merging, Matching, and Updating

Chapter 11 16

11Master 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

Page 17: Sequential File Merging, Matching, and Updating

Chapter 11 17

11Master 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

Page 18: Sequential File Merging, Matching, and Updating

Chapter 11 18

11Master 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

Page 19: Sequential File Merging, Matching, and Updating

Chapter 11 19

11Matching 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

Page 20: Sequential File Merging, Matching, and Updating

Chapter 11 20

11Matching Files to Update

Fields in Master File Records

Page 21: Sequential File Merging, Matching, and Updating

Chapter 11 21

11Matching 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

Page 22: Sequential File Merging, Matching, and Updating

Chapter 11 22

11

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

Page 23: Sequential File Merging, Matching, and Updating

Chapter 11 23

11

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

Page 24: Sequential File Merging, Matching, and Updating

Chapter 11 24

11

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

Page 25: Sequential File Merging, Matching, and Updating

Chapter 11 25

11The readEmp()

and readTrans() Modules

Page 26: Sequential File Merging, Matching, and Updating

Chapter 11 26

11

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

Page 27: Sequential File Merging, Matching, and Updating

Chapter 11 27

11

The translsLargerThanEmp() Module

Page 28: Sequential File Merging, Matching, and Updating

Chapter 11 28

11The finishUp()

Module for Update Program

Page 29: Sequential File Merging, Matching, and Updating

Chapter 11 29

11

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

Page 30: Sequential File Merging, Matching, and Updating

Chapter 11 30

11

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

Page 31: Sequential File Merging, Matching, and Updating

Chapter 11 31

11

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