sams_teach_yourself_cobol_in_24_hours_-_hour_18_master_file_updating

Sams Teach Yourself COBOL in 24 Hours - Hour 18 Master File Updating

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Part III

Business Processing

Hour

18 Master File Updating

19 Reporting

20 Advanced Reporting

Hour 18 Master File Updating

Much of business processing is centered on updating a central repository of information. Transactions that are captured from a variety of locations are accumulated and applied to a master file. A typical transaction in your little shop might be a sale transaction. You need to update the dealer master file with this sale to capture the commission that you are to collect for handling the sale. In this hour, you learn the basics of transaction entry and master file update. The following topics are covered:

• Collecting transactional data

• Data validation

• Sequential file updating

• Indexed file updating

Updating a master file requires you to collect transactional data that is as accurate as possible. There is an old saying in the computer industry: GIGO—Garbage In, Garbage Out. The programmer’s job is to ensure that the transactional data is as accurate as it can be. Having accurate data can aid in making processing efficient and accurate.

Programming for Transaction Entry To update your master file, you need to capture some transactions. For this example (see Listing 18.1), you need to design and code a program that allows you to enter sale transactions that can be used to update a master file. You need to capture transaction date, category, dealer, price, and quantity. The category can be ANTI, CRAF, HOLI, JEWL, MISC, or XMAS. ANTI is for Antiques, CRAF is for Crafts, HOLI is for Holiday items other than Christmas, JEWL is for Jewelry, MISC is for Miscellaneous items, and XMAS is for Christmas items. The Output file is a Line Sequential file named Trans.Txt. The program used to create the transaction file is shown in Listing 18.1.

The required Divisions are coded first.

Listing 18.1 Transaction Entry Program

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt18a. 000004* Transaction Entry 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Special-Names. 000010 Crt Status Is Keyboard-Status 000011 Cursor Is Cursor-Position. 000012 Input-Output Section. The file is selected Optional, which creates the file when it is opened, if it does not yet exist.

000013 File-Control. 000014 Select Optional Trans-File Assign To “Trans.Txt” 000015 Organization Is Line Sequential 000016 File Status Is Trans-File-Status. 000017 Data Division. 000018 File Section. In the transaction record, 40 characters of Filler area are reserved for future expansion.

000019 FD Trans-File. 000020 01 Trans-Record. 000021 03 Transaction-Date Pic 9(8). 000022 03 Transaction-Type Pic X(4). 000023 03 Transaction-Dealer Pic X(8). 000024 03 Transaction-Price Pic S9(7)V99. 000025 03 Transaction-Qty Pic 9(3). 000026 03 Filler Pic X(40). In Working-Storage, the necessary data items are stored for use by the program.

000027 Working-Storage Section. 000028 01 Keyboard-Status. 000029 03 Accept-Status Pic 9. 000030 03 Function-Key Pic X. 000031 88 F1-Pressed Value X“01”. 000032 88 F3-Pressed Value X“03”. 000033 88 F4-Pressed Value X“04”. 000034 03 System-Use Pic X. 000035 01 Cursor-Position. 000036 03 Cursor-Row Pic 9(2) Value 1. 000037 03 Cursor-Column Pic 9(2) Value 1. 000038 01 File-Error-Flag Pic X Value Space. 000039 88 File-Error Value “Y”. 000040 01 Trans-File-Status Pic XX Value Spaces. 000041 88 Trans-File-Success Value “00” Thru “09”. 000042 01 Error-Message Pic X(50) Value Spaces. 000043 01 Open-Error-Message. 000044 03 Filler Pic X(31) 000045 Value “Error Opening Transaction File ”. 000046 03 Open-Status Pic XX Value Spaces. 000047 01 Write-Error-Message. 000048 03 Filler Pic X(31) 000049 Value “Error Writing Transaction File ”. 000050 03 Write-Status Pic XX Value Spaces. The Screen Section contains the screen definition for the data entry program. Notice the use of color and the different required fields.

000051 Screen Section. 000052 01 Data-Entry-Screen 000053 Blank Screen, Auto 000054 Foreground-Color Is 7, 000055 Background-Color Is 1. 000056* 000057 03 Screen-Literal-Group. 000058 05 Line 01 Column 30 Value “Darlene’s Treasures“ 000059 Highlight Foreground-Color 4 Background-Color 1. 000060 05 Line 03 Column 28 Value “Transaction Entry Program ” 000061 Highlight. 000062 05 Line 4 Column 01 Value “Date: ”. 000063 05 Line 5 Column 01 Value “Category: ”. 000064 05 Line 6 Column 01 Value “Dealer Number:”. 000065 05 Line 7 Column 01 Value “Price:”. 000066 05 Line 8 Column 01 Value “Quantity:”. 000067 05 Line 22 Column 01 Value “F1-Save Record ”. 000068 05 Line 22 Column 23 Value “F3-Exit”. 000069 05 Line 22 Column 56 Value “F4-Clear”. 000070 03 Required-Reverse-Group Reverse-Video Required. 000071 05 Line 4 Column 16 Pic 99/99/9999 000072 Using Transaction-Date. 000073 05 Line 5 Column 16 Pic X(4) 000074 Using Transaction-Type. 000075 05 Line 6 Column 16 Pic X(8) 000076 Using Transaction-Dealer. 000077 05 Line 7 Column 16 Pic ZZ,ZZZ.99- 000078 Using Transaction-Price 000079 Blank When Zero. 000080 05 Line 8 Column 16 Pic ZZ9 000081 Using Transaction-Qty 000082 Blank When Zero. 000083 03 Highlight-Display Highlight. 000084 05 Line 20 Column 01 Pic X(50) From Error-Message 000085 Foreground-Color 5 Background-Color 1. After opening the transaction file, the processing loop is performed until the user exits by pressing F3 or a serious file error occurs.

000086 Procedure Division. 000087 Chapt18a-Start. 000088 Perform Open-File 000089 If Not File-Error 000090 Initialize Trans-Record 000091 Perform Process-Input Until F3-Pressed Or 000092 File-Error 000093 Perform Close-File 000094 End-If 000095 Stop Run 000096 . The Open with Extend allows the user to enter some data and then exit the program. When the user returns to the data entry task, the new transactions will be added to the end of the file. By opening the file Extend instead of Output, you prevent the previously entered transactions from being lost.

000097 Open-File. 000098 Open Extend Trans-File 000099 If Not Trans-File-Success 000100 Move Trans-File-Status To Open-Status 000101 Move Open-Error-Message To Error-Message 000102 Perform Display-And-Accept-Error 000103 End-If 000104 . The Process-Input Paragraph displays the screen and accepts the user input. It then determines the appropriate action based on the key that is pressed. The Continue statement after the F3 lets the program fall through the end of the Evaluate and thus the Paragraph. Because the Perform of this Paragraph is testing for F3, you do not need to Perform any action when F3 is pressed. You may omit checking for the F3 key, and the program will still function, as it will fall into the Other condition. However, accounting for all valid function keys is a good practice. This approach makes it much easier if you later need to add another function key. By accounting for each function key in the Evaluate, you can decide which one is still available for use.

000105 Process-Input. 000106 Display Data-Entry-Screen 000107 Accept Data-Entry-Screen 000108 Move Spaces To Error-Message 000109 Evaluate True 000110 When F1-Pressed 000111 Perform Write-Record 000112 When F4-Pressed 000113 Initialize Trans-Record 000114 When F3-Pressed 000115 Continue 000116 When Other 000117 Continue 000118 End-Evaluate 000119 . After a successful Write, the record is cleared so that no leftover data remains on the screen. The error message on the screen is updated to indicate a successful record Write, and the cursor is positioned for the next record entry.

000120 Write-Record. 000121 Write Trans-Record 000122 If Trans-File-Success 000123 Initialize Trans-Record 000124 Move “Record Written” To Error-Message 000125 Move “0101” To Cursor-Position 000126 Else 000127 Move Trans-File-Status To Write-Status 000128 Move Write-Error-Message To Error-Message 000129 Perform Display-And-Accept-Error 000130 End-If 000131 . The Display-And-Accept-Error Paragraph is used whenever a serious error should terminate processing. What the user keys to terminate the Accept does not matter, as the program will end shortly after executing this paragraph. The File-Error condition is checked in determining when the processing loop should end.

000132 Display-And-Accept-Error. 000133 Set File-Error To True 000134 Display Data-Entry-Screen 000135 Accept Data-Entry-Screen 000136 . 000137 Close-File. 000138 Close Trans-File 000139 .

Data Validation This program, as written, has some problems. Although it allows the user to enter the required data to create a transaction file that can be used for update, there is a lot of room for input error.

The program will Accept dates that may or may not be dates. The user may key any number he or she desires into the date field. The dealer number is not converted to upper-case, although the dealer numbers are all in uppercase letters with numbers. The user may enter any dealer number he or she can imagine, and there is no assurance that the dealer being entered is in the dealer file.

The category is not checked for validity, nor is it converted to uppercase. All of these fields are in need of some type of data validation.

Data validation is what the programmer, and thus the program, does to ensure that the data being entered is as valid and accurate as possible. You can do several things to this program to ensure that the user enters accurate data.

Image

Although the date entered should be checked for validity, you are not checking it in this program. Date validation will be covered in Hour 21, “Date Manipulation.”

I suggest that you code a data validation paragraph after the Accept and before the Write. You should not Write the data record unless it passes all data validations.

Add the following flag field to Working-Storage:

000040 01 Validate-Flag Pic X Value Spaces. 000041 88 Validation-Passed Value “Y”. Then change the Evaluate statement where the save record key is detected to Perform a data validation paragraph and check the status of that validation before carrying out the record Write.

000104 Evaluate True 000105 When F1-Pressed 000106 Perform Validate-Data 000107 If Validation-Passed 000108 Perform Write-Record 000109 End-If 000110 When F4-Pressed 000111 Initialize Trans-Record 000112 When F3-Pressed 000113 Continue 000114 When Other 000115 Continue 000116 End-Evaluate The first thing you can do to help ensure accurate information is to convert the Pic X fields to uppercase with an Inspect statement. However, the entire transaction record does not need to be converted. Inspecting two fields means executing the Inspect twice, which is wasteful. Instead, change the record description of the transaction file as follows. This code does not change the position or length of any data in the record. It does, however, group the fields so that a single Inspect can convert them both to uppercase.

000020 01 Trans-Record. 000021 03 Transaction-Date Pic 9(8). 000022 03 Transaction-Text. 000023 05 Transaction-Type Pic X(4). 000024 05 Transaction-Dealer Pic X(8). 000025 03 Transaction-Price Pic S9(7)V99. 000026 03 Transaction-Qty Pic 9(3). 000027 03 Filler Pic X(40). Now you may use the Inspect instruction on the field Transaction-Text and convert both fields to uppercase.

000119 Validate-Data. 000120 Inspect Transaction-Text Converting 000121 “abcdefghijklmnopqrstuvwxyz” To 000122 “ABCDEFGHIJKLMNOPQRSTUVWXYZ” Now that the fields are uppercase, you can validate the category against a list of valid categories. If the category entered is not on the list, you can issue a warning message to the user and position the cursor on the field in error.

The starting assumption is that the data is valid. A flag is set if any invalid data is encountered.

000124 Move “Y” to Validate-Flag 000125 If Not (Transaction-Type = “ANTI” Or “CRAF” Or “HOLI”Oor “JEWL” 000126 Or “MISC” Or “XMAS”) 000127 Set Validation-Error To True 000128 Move “0516” to Cursor-Position 000129 Move 000130 “Invalid Category Must be ANTI CRAF HOLI JEWL MISC or XMAS” 000131 To Error-Message 000132 End-If This simple validation prevents the user from accidentally entering garbage into the transaction file.

Another field that can be validated is the dealer number. If a copy of the dealer master file exists on the computer where the data entry is being performed, you can check the dealer number against the dealer file by doing a Random Read on the dealer file.

First, add the Select and FD for the dealer file. Random access is used because you will be making a single keyed Read against the file for every dealer number entered.

000017 Select Dealer-File Assign to “Dealer.Dat” 000018 Organization Indexed 000019 Access Random 000020 Record Key Dealer-Number 000021 Alternate Record Key Dealer-Name 000022 File Status Dealer-Status. Image

For brevity, in this example the normal COBOL verbiage has been removed and only the relevant portions of the code are presented.

000034 FD Dealer-File. 000035 01 Dealer-Record. 000036 03 Dealer-Number Pic X(8). 000037 03 Dealer-Name. 000038 05 Last-Name Pic X(25). 000039 05 First-Name Pic X(15). 000040 05 Middle-Name Pic X(10). 000041 03 Address-Line-1 Pic X(50). 000042 03 Address-Line-2 Pic X(50). 000043 03 City Pic X(40). 000044 03 State-or-Country Pic X(20). 000045 03 Postal-Code Pic X(15). 000046 03 Home-Phone Pic X(20). 000047 03 Work-Phone Pic X(20). 000048 03 Other-Phone Pic X(20). 000049 03 Start-Date Pic 9(8). 000050 03 Last-Rent-Paid-Date Pic 9(8). 000051 03 Next-Rent-Due-Date Pic 9(8). 000052 03 Rent-Amount Pic 9(4)V99. 000053 03 Consignment-Percent Pic 9(3). 000054 03 Last-Sold-Amount Pic S9(7)V99. 000055 03 Last-Sold-Date Pic 9(8). 000056 03 Sold-to-Date Pic S9(7)V99. 000057 03 Commission-to-Date Pic S9(7)V99. 000058 03 Filler Pic X(15). Several fields must be added to Working-Storage to handle the file.

000077 01 Dealer-Status Pic X(2) Value Spaces. 000078 88 Dealer-Success Value “00” Thru “09”. 000083 01 Dealer-Open-Error-Message. 000084 03 Filler Pic X(31) Value “Error Opening Dealer File”. 000085 03 Open-Dealer-Status Pic XX Value Spaces. Add the statements necessary to handle opening the file at the beginning of the program and closing the file at the end. There is no need to Open the file if the transaction file Open fails.

000121 Perform Open-File 000122 If Not File-Error 000123 Perform Open-Dealer-File 000124 End-If The paragraph that opens the dealer file is coded as follows:

000142 Open-Dealer-File. 000143 Open Input Dealer-File 000144 If Not Dealer-Success 000145 Move Dealer-Status To Open-Dealer-Status 000146 Move Dealer-Open-Error-Message To Error-Message 000147 Perform Display-And-Accept-Error 000148 End-if 000149 . Don’t forget to close the file at the end of the program.

000129 Perform Close-File 000130 Perform Close-Dealer-File Now that you have access to the file, you need to code the logic necessary in the validation paragraph.

000181 Move Transaction-Dealer To Dealer-Number 000182 Read Dealer-File 000183 Invalid Key 000184 Set Validation-Error To True 000185 Move “0616” to Cursor-Position 000186 Move “Invalid Dealer Number Entered” To Error-Message 000187 End-Read These validations greatly reduce the chance of erroneous data entering the system. Data validations such as these are very common in business systems.

Updating a Master File The two common methods used to update master files with transaction data are Sequential update and Random update. Each style has advantages and disadvantages. The Sequential update is easy to recover from if an error occurs during the update process. The Random update can apply transactions in any order. These update styles are discussed in detail in the following two sections.

Every file update should include some type of control statistics, which you can use to find and correct any problems that occur with the update. In the following examples, the dealer master is updated with sales transactions. To save you time, a sample transaction file has been prepared for each update. The total number of transactions, the number of rejected transactions, and the commission amount are reported.

Updating a Sequential Master File Updating a Sequential file involves several elements. The main advantage of such an update is that transactions are processed against a Sequential Input file, creating a new master file as Output. If something happens in the update process, the original Input master file is untouched. Sequential updates require every master file record to be read and then written to an Output master file. If you are dealing with many transactions, this approach makes sense. However, if only a single transaction exists, the entire Input master file must still be read and written. Additionally, in a multiuser or network environment, where multiple users might need access to the master file simultaneously, you must prohibit this access until the update process is complete.

When updating a Sequential file, master and transaction files must be in the same order. The files must be sorted by the field that identifies the master record to the transaction record. There might be multiple transactions for a single master record. The programmer must ensure that all transactions for a master record have been applied before the Output master file record is written and the next master file Input record is read.

The Sequential update proceeds as follows: A record is read from the transaction file and the master file. One of three things can be true. If the transaction key matches the master file key, then an update needs to be applied. If the transaction file key is less than the master file key, then no matching master file record exists and the transaction is rejected. If the transaction key is greater than the master file key, then no further transactions, if any, occur for that master record. It should be written to the Output master, and the next master record Read.

As records are read and processed, one of two things can happen. Either the end of the master file is reached before the end of the transaction file, or the end of the transaction file is reached before the end of the master file. When the end of the transaction file is reached, any remaining master file records can be written directly to the new master file. If the end of the master file is reached first, then any transactions remaining are rejected, as no more master file records are available for updating.

In this update, rejected transactions are saved to a file. When the problem with the transactions is corrected, then they can be applied in a later update. As a good programmer, you should never terminate an update process just because you don’t know what to do.

Nothing short of a hardware failure should terminate your update. Writing the rejects to another file isolates the invalid transactions so that the problem can be researched and repaired. For these examples, the only error that rejects a transaction is when no matching master file record exists.

Walking through a Sequential update program is the best way to understand the process, as shown in Listing 18.2. The program starts as would any other COBOL program.

Listing 18.2 Sequential File Update

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt18c. 000004* Sequential File Update 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. The files used by the program are all Line Sequential files. By making the files Optional, you need not worry about any Open failures. If a file does not exist, it is created. The files Trans.Seq and Dealer.Seq are provided on the CD-ROM, and after you install the CD-ROM, they will exist in the \DATAFILE directory of your hard drive as well. Copy these files into the \TYCOBOL folder. These files are properly formatted and sorted in dealer number sequence. The transaction file contains numerous records, some duplicate dealer numbers, and some contain invalid data. The Dealer.Out file is the new master file, and Reject.Txt contains any rejected transactions.

000010 File-Control. 000011 Select Optional Trans-File Assign To “Trans.Seq” 000012 Organization Is Line Sequential. 000013 Select Optional Dealer-File Assign To “Dealer.Seq” 000014 Organization Is Line Sequential. 000015 Select Optional Dealer-Out Assign To “Dealer.Out” 000016 Organization Is Line Sequential. 000017 Select Optional Reject-File Assign To “Reject.Txt” 000018 Organization Is Line Sequential. The record layouts for the Output master and the reject file do not need to be coded. When the records are written, they will be written from the master and transaction Input file record areas.

000019 Data Division. 000020 File Section. 000021 Fd Trans-File. 000022 01 Trans-Record. 000023 03 Transaction-Date Pic 9(8). 000024 03 Transaction-Text. 000025 05 Transaction-Type Pic X(4). 000026 05 Transaction-Dealer Pic X(8). 000027 03 Transaction-Price Pic S9(7)v99. 000028 03 Transaction-Qty Pic 9(3). 000029 03 Filler Pic X(40). 000030 Fd Reject-File. 000031 01 Reject-Record Pic X(72). 000032 Fd Dealer-File. 000033 01 Dealer-Record. 000034 03 Dealer-Number Pic X(8). 000035 03 Dealer-Name. 000036 05 Last-Name Pic X(25). 000037 05 First-Name Pic X(15). 000038 05 Middle-Name Pic X(10). 000039 03 Address-Line-1 Pic X(50). 000040 03 Address-Line-2 Pic X(50). 000041 03 City Pic X(40). 000042 03 State-Or-Country Pic X(20). 000043 03 Postal-Code Pic X(15). 000044 03 Home-Phone Pic X(20). 000045 03 Work-Phone Pic X(20). 000046 03 Other-Phone Pic X(20). 000047 03 Start-Date Pic 9(8). 000048 03 Last-Rent-Paid-Date Pic 9(8). 000049 03 Next-Rent-Due-Date Pic 9(8). 000050 03 Rent-Amount Pic 9(4)v99. 000051 03 Consignment-Percent Pic 9(3). 000052 03 Last-Sold-Amount Pic S9(7)v99. 000053 03 Last-Sold-Date Pic 9(8). 000054 03 Sold-To-Date Pic S9(7)v99. 000055 03 Commission-To-Date Pic S9(7)v99. 000056 03 Filler Pic X(15). 000057 Fd Dealer-Out. 000058 01 Dealer-Out-Record Pic X(376). Working-Storage contains the fields necessary to process the records and to collect the statistics.

000059 Working-Storage Section. 000060 01 Current-Commission Pic S9(7)v99 Value Zeros. 000061 01 Total-Commission Pic S9(7)v99 Value Zeros. 000062 01 Transactions-Read Pic 9(5) Value Zeros. 000063 01 Transactions-Rejected Pic 9(5) Value Zeros. Hour 21 is devoted to the intricacies of date processing. However, for this program the most recent transaction date is being stored in the master record. Because the dates are stored in month, day, year format, it is impossible to compare them directly to determine which date is the most recent. The value “01042000:” evaluates to less than “08111999”, when in actuality the former is a later date. Work-Date and Reverse-Date in lines 64 through 71 are provided to allow the formatting of the dates in year, month, day format for comparison.

000064 01 Work-Date. 000065 03 Work-MM Pic 9(2). 000066 03 Work-DD Pic 9(2). 000067 03 Work-YYYY Pic 9(4). 000068 01 Reverse-Date. 000069 03 Work-YYYY Pic 9(4). 000070 03 Work-MM Pic 9(2). 000071 03 Work-DD Pic 9(2). 000072 01 Compare-Date-1 Pic 9(8). 000073 01 Compare-Date-2 Pic 9(8). These edit fields are used for formatting the audit counts that are displayed at the end of the program.

000074 01 Edit-Count Pic ZZ,ZZ9. 000075 01 Edit-Amt Pic Z,ZZZ,ZZZ.99-. The program starts by reading a single record from each file. The Read may result in an At End condition if the files don’t exist. This condition does not terminate the update process or cause any problems. The absence of transactions becomes apparent when the counts are displayed at the end of the process. The Read Paragraphs move High-Values into the data records when the end of file is reached. This value serves a dual purpose. First, it is the indicator that is used to terminate the update process. When both files have been completely read, then both data records are High-Values. Second, if one file reaches the end first, any comparisons with the remaining file’s data result in the remaining data values being less. This status ensures that no attempt is made to read past the end of the master file or transaction file.

000076 Procedure Division. 000077 Chapt18c-Start. 000078 Display “Begin Process Chapt18c” 000079 Open Output Reject-File 000080 Dealer-Out 000081 Input Trans-File 000082 Dealer-File 000083 Perform Read-Dealer 000084 Perform Read-Trans The process is performed until both records contain High-Values; that is, each reaches the end of the file. When both files have been completely read, the update process is complete. The files may then be closed and the processing statistics displayed.

000085 Perform Process-Files Until 000086 Trans-Record = High-Values And 000087 Dealer-Record = High-Values 000088 Close Reject-File 000089 Dealer-Out 000090 Trans-File 000091 Dealer-File 000092 Move Transactions-Read To Edit-Count 000093 Display “Processing Complete” 000094 Display “Transactions Read ” Edit-Count 000095 Move Transactions-Rejected To Edit-Count 000096 Display “Transactions Rejected ” Edit-Count 000097 Move Total-Commission To Edit-Amt 000098 Display “Total Commission ” Edit-Amt 000099 Stop Run 000100 . 000101 The Process-Files Paragraph is where the actual update process occurs. The current transaction record is compared to the master record. Based on the results of the compare, one of three things can happen. If the current dealer number is less than that of the transaction file, then the process is finished with the present master record and it can be written to the Output file.

000102 Process-Files. 000103 Evaluate True 000104 When Dealer-Number < Transaction-Dealer 000105 Perform Write-Dealer-Out 000106 Perform Read-Dealer If the dealer number in the master file is greater than the current transaction, then this transaction cannot be applied to a dealer record and is rejected. After each valid transaction is applied, a new one is read. If a dealer number in the master file is greater than the dealer number in the transaction file, there was no matching dealer number for the transaction.

000107 When Dealer-Number > Transaction-Dealer 000108 Perform Write-Reject 000109 Perform Read-Trans If the dealer number in the master file matches the dealer number in the transaction file, then the dealer record can be updated. After this transaction is used to update the master file record, a new transaction is read. Notice that no new master file record is read because multiple transactions might apply for each master file record. Transactions must be read and applied until no more match. When a transaction is read that has a higher dealer number, then the existing master file record can be written to the Output file. It does not matter in the least if the record has been modified or not. All master records must be written to the new Output file.

000110 When Dealer-Number = Transaction-Dealer 000111 Perform Apply-Transaction 000112 Perform Read-Trans 000113 End-Evaluate 000114 . This Paragraph is where the master file fields are updated with the appropriate fields from the transaction record. First the Sold-To-Date is incremented by the proper amount. The Compute statement takes care of multiplying the unit price by the quantity and adding the result to the master field. The second Compute figures the commission on this item based on the consignment percentage that is stored in the dealer file. The percentage is divided by 100 because it was stored as a whole number. After the consignment amount is computed, it is added to the master file record and to the audit totals.

000115 Apply-Transaction. 000116 Compute Sold-To-Date = Sold-To-Date + 000117 (Transaction-Qty * Transaction-Price) 000118 Compute Current-Commission Rounded = 000119 (Transaction-Qty * Transaction-Price) * 000120 (Consignment-Percent / 100) 000121 Add Current-Commission To Commission-To-Date 000122 Total-Commission The last sale date from the master file is reversed so that it can be properly compared. This step is done simply, and the result is stored in a temporary field that is used in the comparison. The transaction date is similarly reversed. Then the two dates are compared, and if the transaction date is after the last sold date in the master file, the transaction date is moved to the master record. It is very important to remember that the nonreversed date is moved to the record. This step ensures that all the dates are in the same format.

000123 Move Last-Sold-Date To Work-Date 000124 Move Corresponding Work-Date To Reverse-Date 000125 Move Reverse-Date To Compare-Date-1 000126 Move Transaction-Date To Work-Date 000127 Move Corresponding Work-Date To Reverse-Date 000128 Move Reverse-Date To Compare-Date-2 000129 If Compare-Date-2 > Compare-Date-1 000130 Move Transaction-Date To 000131 Last-Sold-Date 000132 End-If 000133 . The following Paragraphs are performed from elsewhere in the program. Notice that when a reject record is written, the count of rejected records is incremented. Note also that as each file is read, if the end of file is reached, High-Values is moved into the record area. This step is the key to controlling the process. By having High-Values in the record, no compare can be greater than that of the value in the data record, and no further data records will be read. As each transaction record is read, the count of transactions is incremented. It is important to realize that this counter is incremented only when the Read is successful, not At End of file. A frequent error when accumulating these types of counts is to increment the counter for the Read that resulted in an end of file condition.

000134 Write-Dealer-Out. 000135 Write Dealer-Out-Record From Dealer-Record 000136 . 000137 Write-Reject. 000138 Add 1 To Transactions-Rejected 000139 Write Reject-Record From Trans-Record 000140 . 000141 Read-Dealer. 000142 Read Dealer-File 000143 At End 000144 Move High-Values To Dealer-Record 000145 End-Read 000146 . 000147 Read-Trans. 000148 Read Trans-File 000149 At End 000150 Move High-Values To Trans-Record 000151 Not At End 000152 Add 1 To Transactions-Read 000153 End-Read 000154 . As you can see, the Sequential update is straightforward and can be written in relatively few lines of code. After the Sequential update is complete, the original master file must be replaced with the new Output file. This step can occur after the counts are validated and the user is satisfied with the results. If the update process is interrupted for any reason, you can restart the Sequential update from the beginning with no ill effects.

Enter, compile, and run the sample program in Listing 18.2. The Output screen from this program, if it is properly coded, is shown in Figure 18.1.

Updating an Indexed Master File Random updates are applied against Indexed master files. The update is called Random because the transactions do not need to be in any particular order. Each transaction is matched and applied against a master file record. This type of update is much easier to program than a Sequential file update.

Although a Sequential file update may seem less efficient, as it has to Read and Write every master file record, in many cases the Indexed file update is less efficient. Because the transaction records are not in any particular sequence, after each update is applied, the master file record must be rewritten. If many transaction records exist for a particular master file record, this process can be very inefficient.

Figure 18.1 Results of running the program in Listing 18.2.

Image

The advantages to this type of update relate to the relative ease of programming such an update. The process is simple and easy to follow. The transaction file is processed from beginning to end. As each transaction record is read, the related master file record is read. If the master file record is not found, then the transaction is rejected and the program moves on to the next transaction. If the master file does exist, the transaction is applied and the master file record is rewritten. When all transactions have been processed, the update is complete.

One disadvantage occurs when a problem develops during the update. It is virtually impossible to back out the transactions that have been applied. You must either determine the last successful transaction or restore a backup copy of the master file and reapply the update.

Review the following program that updates the Indexed file Dealer.Dat from Trans.Txt. These files are included on the CD-ROM in the \DATAFILE directory.

The example in Listing 18.3 uses one less file than the Sequential update did. There is no Output master file. The Indexed dealer file is accessed in Random mode.

Listing 18.3 Indexed File Update

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt18d. 000004* Indexed File Update 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Input-Output Section. 000010 File-Control. 000011 Select Optional Trans-File Assign To “Trans.Txt” 000012 Organization Is Line Sequential. 000013 Select Optional Dealer-File Assign To “Dealer.Dat” 000014 Organization Indexed 000015 Access Random 000016 Record Key Dealer-Number 000017 Alternate Record Key Dealer-Name 000018 File Status Dealer-Status. 000019 Select Optional Reject-File Assign To “Reject.Txt” 000020 Organization Is Line Sequential. 000021 Data Division. 000022 File Section. 000023 Fd Trans-File. 000024 01 Trans-Record. 000025 03 Transaction-Date Pic 9(8). 000026 03 Transaction-Text. 000027 05 Transaction-Type Pic X(4). 000028 05 Transaction-Dealer Pic X(8). 000029 03 Transaction-Price Pic S9(7)v99. 000030 03 Transaction-Qty Pic 9(3). 000031 03 Filler Pic X(40). 000032 Fd Reject-File. 000033 01 Reject-Record Pic X(72). 000034 Fd Dealer-File. 000035 01 Dealer-Record. 000036 03 Dealer-Number Pic X(8). 000037 03 Dealer-Name. 000038 05 Last-Name Pic X(25). 000039 05 First-Name Pic X(15). 000040 05 Middle-Name Pic X(10). 000041 03 Address-Line-1 Pic X(50). 000042 03 Address-Line-2 Pic X(50). 000043 03 City Pic X(40). 000044 03 State-Or-Country Pic X(20). 000045 03 Postal-Code Pic X(15). 000046 03 Home-Phone Pic X(20). 000047 03 Work-Phone Pic X(20). 000048 03 Other-Phone Pic X(20). 000049 03 Start-Date Pic 9(8). 000050 03 Last-Rent-Paid-Date Pic 9(8). 000051 03 Next-Rent-Due-Date Pic 9(8). 000052 03 Rent-Amount Pic 9(4)v99. 000053 03 Consignment-Percent Pic 9(3). 000054 03 Last-Sold-Amount Pic S9(7)v99. 000055 03 Last-Sold-Date Pic 9(8). 000056 03 Sold-To-Date Pic S9(7)v99. 000057 03 Commission-To-Date Pic S9(7)v99. 000058 03 Filler Pic X(15). 000059 Working-Storage Section. 000060 01 Current-Commission Pic S9(7)v99 Value Zeros. 000061 01 Total-Commission Pic S9(7)v99 Value Zeros. 000062 01 Transactions-Read Pic 9(5) Value Zeros. 000063 01 Transactions-Rejected Pic 9(5) Value Zeros. 000064 01 Work-Date. 000065 03 Work-MM Pic 9(2). 000066 03 Work-DD Pic 9(2). 000067 03 Work-YYYY Pic 9(4). 000068 01 Reverse-Date. 000069 03 Work-YYYY Pic 9(4). 000070 03 Work-MM Pic 9(2). 000071 03 Work-DD Pic 9(2). 000072 01 Compare-Date-1 Pic 9(8). 000073 01 Compare-Date-2 Pic 9(8). 000074 01 Used-Transaction-Flag Pic X Value Spaces. 000075 88 Used-This-Tran Value “Y”. 000076 01 Edit-Count Pic ZZ,ZZ9. 000077 01 Edit-Amt Pic Z,ZZZ,ZZZ.99-. Three new fields are necessary in Working-Storage: one to note when the transaction file has reached end of file, another to return the File Status value of operations against the dealer Indexed file, and a flag that is set in case an error occurs on the Indexed file that is captured by the Declaratives. If such an error exists, processing the update should terminate. The only error that can occur here is a critical error caused by a serious problem with the Indexed file, such as a hardware failure.

000078 01 Dealer-Status Pic XX Value Zeros. 000079 88 Dealer-Success Value “00” Thru “09”. 000080 01 Trans-Flag Pic X Value Spaces. 000081 88 End-Of-Trans Value “Y”. 000082 01 Dealer-Flag Pic X Value Spaces. 000083 88 Dealer-Error Value “Y”. 000084 Procedure Division. 000085 Declaratives. 000086 Dealer-File-Error Section. 000087 Use After Standard Error Procedure On Dealer-File 000088 . 000089 Dealer-Error-Paragraph. 000090 Display “Error on Dealer File” Dealer-Status 000091 Set Dealer-Error To True 000092 . The processing loop is simple. The files are opened, and then the transaction file is read and processed until the end of file is reached or an error occurs in the Indexed dealer file.

000093 End Declaratives. 000094 Chapt18d-Start. 000095 Display “Begin Process Chapt18d” 000096 Open Output Reject-File 000097 Input Trans-File 000098 I-O Dealer-File 000099 Perform Process-Files Until End-Of-Trans Or Dealer-Error 000100 Close Reject-File 000101 Trans-File 000102 Dealer-File 000103 Move Transactions-Read To Edit-Count 000104 Display “Processing Complete” 000105 Display “Transactions Read ” Edit-Count 000106 Move Transactions-Rejected To Edit-Count 000107 Display “Transactions Rejected ” Edit-Count 000108 Move Total-Commission To Edit-Amt 000109 Display “Total Commission ” Edit-Amt 000110 Stop Run 000111 . 000112 000113 Process-Files. 000114 Read Trans-File 000115 At End Set End-Of-Trans To True 000116 Not At End 000117 Add 1 To Transactions-Read 000118 Perform Attempt-Transaction 000119 End-Read 000120 . Before a master file record can be updated, the program must use a Read statement to determine whether the corresponding record exists in the master file. If the Read fails because of an invalid key, then the record is written to the reject file. If the Read is successful, the master file is updated, using the same logic as in the Sequential update, and then rewritten.

000121 Attempt-Transaction. 000122 Move Transaction-Dealer To Dealer-Number 000123 Read Dealer-File 000124 Invalid Key 000125 Perform Write-Reject 000126 Not Invalid Key 000127 Perform Apply-Transaction 000128 End-Read 000129 . 000130 Apply-Transaction. 000131 Compute Sold-To-Date = Sold-To-Date + 000132 (Transaction-Qty * Transaction-Price) 000133 Compute Current-Commission Rounded = 000134 (Transaction-Qty * Transaction-Price) * 000135 (Consignment-Percent / 100) 000136 Add Current-Commission To Commission-To-Date 000137 Total-Commission 000138 Move Last-Sold-Date To Work-Date 000139 Move Corresponding Work-Date To Reverse-Date 000140 Move Reverse-Date To Compare-Date-1 000141 Move Transaction-Date To Work-Date 000142 Move Corresponding Work-Date To Reverse-Date 000143 Move Reverse-Date To Compare-Date-2 000144 If Compare-Date-2 > Compare-Date-1 000145 Move Transaction-Date To 000146 Last-Sold-Date 000147 End-If 000148 Rewrite Dealer-Record 000149 . 000150 Write-Reject. 000151 Add 1 To Transactions-Rejected 000152 Write Reject-Record From Trans-Record 000153 . As you can see, the Indexed file update is very easy to follow and can be efficient. However, if the potential exists for many transaction records to be applied against a single master file record, this approach can be less efficient than a simple Sequential update.

Summary In this hour, you learned the following:

• How data validation can eliminate problems that might occur later during an update process.

• Several methods for validating user entered data. These include checking the entry against an internal table and validating against an Indexed file.

• Two common file update procedures, Sequential and Random, and the advantages and disadvantages of each.

• How each of these updates is coded and data is processed.

• The importance of reporting errors, continuing processing when possible, and accumulating audit totals.

• Why the most efficient update method depends on the types and numbers of transactions being applied.

Q&A Q When updating a Sequential file, what do I need to watch for?

A Common mistakes include not processing the remaining records in the file that does not reach end of file first and writing the new master record before all transactions are applied.

Q Can I apply the techniques for master file updating to other updates?

A Yes. The Random update, in particular, is very similar to the updates that occur in interactive programs in which users are entering data and updating a master file record with each entry.

Q Can’t I just ignore data validation for user input and then take care of the validation in the update program?

A Sure, but it’s not a good idea. Many systems, people, and procedures can come between you and the input of the transactional data. Tracing problems to their source can be very difficult, so you are better off having as much up-front data validation as possible. However, if you do encounter an error caused by invalid data in a transaction record, don’t abort the update process. Store the invalid data for later problem diagnosis.

Q I really think the Sequential update is confusing. Can I use a Random update all the time instead?

A Maybe and maybe not. Your master file may not be an Indexed file. Additionally, you may find that a Sequential update is many times more efficient than a Random update, depending on the transactions you are processing.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the section “Quiz and Exercise Questions and Answers” that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

Fair Use Sources

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_18_master_file_updating.txt · Last modified: 2024/04/28 03:37 by 127.0.0.1