Michael Coughlan
Page 58
Acme Automobile Parts Limited sells motorcycle and automobile spare parts. Recently, the company purchased a computer and retained your firm to write the programs required. Your supervisor has asked you to write the program detailed next.
469
Chapter 17 ■ DireCt aCCess Files
General Description
The program is required to perform file maintenance on the vehicle master file using a transaction file of validated amendment records. The transaction file has been sorted on ascending date (YYYYMMDD). If an error is encountered when attempting to apply transactions to the vehicle master file, then the transaction record must be written to an error file (Listing17-8-Err.DAT). When a vehicle record is deleted, the spare parts stocked for that vehicle are no longer required and so must be printed to a redundant stock report (Listing17-8-Stk.RPT).
There are two types of records in the transaction file, and they are distinguished from one another by the codes
"I" (insert vehicle record) and "D" (delete vehicle record) in the first character position of the record.
Vehicle Master File
The vehicle master file (Listing17-8-VMF.DAT) is a relative file. VehicleNumber is used for the relative record number.
Each record has following description:
Field
Type
Length
Value
VehicleNumber
9
4
1–9999
VehicleDescription
X
25
–
ManufacturerName
X
20
–
Stock Master File
The stock master file (Listing17-8-SMF.DAT) is an indexed file. It is required so that you can report all the stock records that are affected when a vehicle is deleted from the vehicle master file. Each record in the stock master file has the following record description:
Field
Key Type
Type
Length
Value
PartNumber
Primary
9
7
1–9999999
VehicleNumber
Alt with duplicates
9
4
1–9999
PartDescription
–
X
25
–
Transaction File
The transaction file (AcmeTrans.DAT) is validated file, sequenced on ascending DateOfEntry. Records in the file have the following description:
Record Type
Field
Type
Length
Value
InsertVehicleRecord
TypeCode
X
1
I
DateOfEntry
9
8
YYYYMMDD
VehicleNumber
9
4
1–9999
VehicleDescription
X
25
–
DeleteVehicleRecord
TypeCode
X
1
D
DateOfEntry
9
8
YYYYMMDD
VehicleNumber
9
4
1–9999
470
Chapter 17 ■ DireCt aCCess Files
Maintenance Procedure
Type Code Action
I
If a record with this VehicleNumber already exists in either the stock or vehicle master
file, then write the transaction record to the error file. Otherwise, insert the record.
D
If the record does not exist in the vehicle master file, then write the transaction record
to the error file.
If there is no error, then read all the stock records with the same VehicleNumber as
the record to be deleted and write the details to the redundant stock report.
Rewrite the VehicleNumber field in each of these stock records with zeros. Delete the
vehicle master file record.
The Redundant Stock Report
Headings should be printed at the top of each page. See the print specification in Figure 17-16 for further details.
Figure 17-16. Print specification. Line numbers and column numbers added
471
Chapter 17 ■ DireCt aCCess Files
prOGraMMING eXerCISe: aNSWer
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing17-8.
AUTHOR. MICHAEL COUGHLAN.
*Applies Insertions and Deletions in TransFile to the VehicleFile.
*For Insertions - If a vehicle already exists in either the Stock or
*Vehicle file, the transaction record is written to the Error File otherwise inserted
*For Deletions - If the vehicle does not exist in the Vehicle File the transaction
*record is written to the Error File otherwise the Vehicle record is deleted
*If the vehicle record is deleted all the Stock records with the same VehicleNumber
*as the deleted record are written to the Redundant Stock Report and the VehicleNumber
*field in each of these Stock records is overwritten with zeros.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StockFile ASSIGN TO "Listing17-8-SMF.DAT"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PartNumSF
ALTERNATE RECORD KEY IS VehicleNumSF
WITH DUPLICATES
FILE STATUS IS StockErrStatus.
SELECT VehicleFile ASSIGN TO "Listing17-8-VMF.DAT"
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS VehicleNumKey
FILE STATUS IS VehicleErrStatus.
SELECT TransFile ASSIGN TO "Listing17-8-TRANS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT ErrorFile ASSIGN TO "Listing17-8-ERR.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT RedundantStockRpt ASSIGN TO "Listing17-8-STK.RPT".
DATA DIVISION.
FILE SECTION.
FD StockFile.
01 StockRecSF.
02 PartNumSF PIC 9(7).
02 VehicleNumSF PIC 9(4).
02 PartDescSF PIC X(25).
472
Chapter 17 ■ DireCt aCCess Files
FD VehicleFile.
01 VehicleRecVF.
02 VehicleNumVF PIC 9(4).
02 VehicleDescVF PIC X(25).
02 ManfNameVF PIC X(20).
FD TransFile.
01 TransRecTF.
02 TransTypeTF PIC X.
88 InsertionRec VALUE "I".
88 DeletionRec VALUE "D".
02 DateTF PIC X(8).
02 VehicleNumTF PIC 9(4).
02 VehicleDescTF PIC X(25).
02 ManfNameTF PIC X(20).
FD RedundantStockRpt REPORT IS StockReport.
FD ErrorFile.
01 ErrorRec PIC X(56).
WORKING-STORAGE SECTION.
01 ErrorStatusCodes.
02 StockErrStatus PIC X(2).
88 StockFileOpOK VALUE "00", "02".
88 StockRecExistis VALUE "22".
88 NoStockRec VALUE "23".
02 VehicleErrStatus PIC X(2).
88 VehicleFileOpOK VALUE "00".
88 VehicleRecExists VALUE "22".
88 NoVehicleRec VALUE "23".
01 FileVariables.
02 VehicleNumKey PIC 9(4).
02 PrevVehicleNum PIC 9(4).
01 ConditionNames.
02 FILLER PIC X.
88 EndOfStockFile VALUE HIGH-VALUES.
88 NotEndOfStockFile VALUE LOW-VALUES.
02 FILLER PIC X.
88 EndOfTransFile VALUE HIGH-VALUES.
REPORT SECTION.
RD StockReport
PAGE LIMIT IS 66
HEADING 1
FIRST DETAIL 6
LAST DETAIL 50
FOOTING 55.
473
Chapter 17 ■ DireCt aCCess Files
01 TYPE IS PAGE HEADING.
02 LINE 2.
03 COLUMN 31 PIC X(24) VALUE
"REDUNDANT STOCK REPORT".
02 LINE 3.
03 COLUMN 30 PIC X(26) VALUE ALL "-".
02 LINE 6.
03 COLUMN 2 PIC X(36) VALUE
"PART NUMBER PART DESCRIPTION".
03 COLUMN 45 PIC X(35) VALUE
"VEHICLE NO. MANUFACTURER NAME".
01 DetailLine TYPE IS DETAIL.
02 LINE IS PLUS 2.
03 COLUMN 3 PIC 9(7) SOURCE PartNumSF .
03 COLUMN 17 PIC X(25) SOURCE PartDescSF.
03 COLUMN 48 PIC 9(4) SOURCE VehicleNumSF.
03 COLUMN 60 PIC X(20) SOURCE ManfNameVF.
PROCEDURE DIVISION.
Begin.
OPEN INPUT TransFile.
OPEN I-O StockFile
VehicleFile.
OPEN OUTPUT ErrorFile
RedundantStockRpt.
INITIATE StockReport
READ TransFile
AT END SET EndOfTransFile TO TRUE
END-READ
PERFORM UNTIL EndOfTransFile
MOVE VehicleNumTF TO VehicleNumKey
VehicleNumSF
EVALUATE TRUE
WHEN InsertionRec PERFORM CheckStockFile
WHEN DeletionRec PERFORM DeleteVehicleRec
WHEN OTHER DISPLAY "NOT INSERT OR DELETE"
END-EVALUATE
READ TransFile
AT END SET EndOfTransFile TO TRUE
END-READ
END-PERFORM
TERMINATE StockReport
474
Chapter 17 ■ DireCt aCCess Files
CLOSE ErrorFile
RedundantStockRpt
TransFile
StockFile
VehicleFile
STOP RUN.
CheckStockFile.
READ StockFile KEY IS VehicleNumSF
INVALID KEY CONTINUE
END-READ
IF StockFileOpOK
PERFORM WriteErrorLine
ELSE IF NoStockRec
PERFORM InsertVehicleRec
ELSE
DISPLAY "Unexpected Read Error on Stockfile"
DISPLAY "Stockfile status = " StockErrStatus
END-IF
END-IF.
InsertVehicleRec.
MOVE ManfNameTF TO ManfNameVF
MOVE VehicleDescTF TO VehicleDescVF
MOVE VehicleNumTF TO VehicleNumVF
WRITE VehicleRecVF
INVALID KEY CONTINUE
END-WRITE
IF VehicleRecExists PERFORM WriteErrorLine
ELSE IF NOT VehicleFileOpOK
DISPLAY "Unexpected Write Error on VehicleFile."
DISPLAY "Vehicle file status = " VehicleErrStatus
END-IF
END-IF.
DeleteVehicleRec.
READ VehicleFile
INVALID KEY CONTINUE
END-READ
IF NoVehicleRec PERFORM WriteErrorLine
ELSE IF VehicleFileOpOK
DELETE VehicleFile RECORD
INVALID KEY
DISPLAY "Unexpected Delete Error on VehicleFile"
DISPLAY "Vehicle file status = " VehicleErrStatus
END-DELETE
PERFORM UpdateStockFile
ELSE
DISPLAY "DeleteProblem = " VehicleErrStatus
END-IF
END-IF.
475
Chapter 17 ■ DireCt aCCess Files
WriteErrorLine.
MOVE TransRecTF TO ErrorRec
WRITE ErrorRec.
UpdateStockFile.
MOVE VehicleNumSF TO PrevVehicleNum
READ StockFile KEY IS VehicleNumSF
INVALID KEY CONTINUE
END-READ
IF StockFileOpOK
SET NotEndOfStockFile TO TRUE
PERFORM PrintStockRpt
UNTIL VehicleNumSF NOT EQUAL TO PrevVehicleNum
OR EndOfStockFile
END-IF.
PrintStockRpt.
GENERATE DetailLine
MOVE ZEROS TO VehicleNumSF
REWRITE StockRecSF
INVALID KEY DISPLAY "ERROR ON REWRITE"
END-REWRITE
READ StockFile NEXT RECORD
AT END SET EndOfStockFile TO TRUE
END-READ.
476
Chapter 18
The COBOL Report Writer
This chapter introduces the COBOL Report Writer. In a series of increasingly complex programs, you learn how to use the Report Writer to create control-break-based report programs. By examining these programs, you are gradually introduced to the new verbs, clauses, sections and concepts of the Report Writer. You see how to use the RD entry in the REPORT SECTION to specify control-break items and define the basic layout of the page. The chapter explores report groups and how to create report groups linked to the control-break items specified in the report’s RD entry. You learn how to use the SUM clause for subtotaling and rolling forward. The final program introduces declaratives and how to use them to extend the capabilities of the Report Writer. Once you’ve seen the capabilities of the Report Writer through the example programs, the chapter explores the verbs, clauses, and concepts of the Report Writer more formally.
Declaratives can be used to extend the capabilities of the Report Writer, but you can also use them to define exception-handling procedures for files. The final section explains how to create declaratives for file error handling.
Report Writer
Producing reports is an important aspect of business programming. Nowadays, reports may consist of rows and columns of figures and be supported by summary information in the form of a variety of charts and graphs. In the past, reports consisted solely of printed figures. You’ve probably seen such reports in old films, where a management person is poring over page after page of green-lined, fan-fold computer printout.
Although producing reports is important, unfortunately the report programs produced using standard COBOL
print files (see Chapter 8) are often tedious to code. Report programs are long, achieving correct placement of horizontal and vertical print items is laborious, and the programs frequently consist of repetitions of the tasks and techniques (such as control-break processing) used in other report programs. In recognition of the importance of reports in the business domain, and to simplify the task of writing report programs, COBOL provides the Report Writer.
Like indexed files, the COBOL Report Writer used to be one of the jewels in COBOL’s crown. But today, just as relational databases have eroded the importance of indexed files, so off-the-shelf packages such as Crystal Reports with its array of charts and graphs have put COBOL’s Report Writer in the shade. Nevertheless, although summary information in the form of charts and graphs is very useful, there is still a need for printed reports; and you can learn a lot from a close acquaintance with the Report Writer.
I start by showing you an example of a report produced by the Report Writer. Then, through a series of
increasingly complex report programs, you learn how to create the program that produced that report. The final example program takes the complexity one stage further.
Example Report: Solace Solar Solutions
This report shows the sales made by agents selling solar power products in each of the 50 American states. You see the program specification and the report, and then I follow up with a discussion that highlights the report’s features.
After discussing the report, I show you the PROCEDURE DIVISION code that produced the report.
477
Chapter 18 ■ the COBOL repOrt Writer
Problem Specification
Solace Solar Sol
utions is a company that sells solar power products through its sales agents all over the United States.
Sales agents are paid a base salary (which is different from state to state) and a commission of 8% on the value of the products they sell.
The monthly report shows the value of the individual sales and the total sales made by each Solace sales agent.
The total sales made for the state and the base salary for the state are also shown. The report is printed on ascending sales agent number within ascending state name.
The report is based on a sequential sales file, which contains details of each sale made in the country. The sales file is ordered on ascending sales agent number within ascending state number. Each record of the sales file has the following description:
Field
Type
Length
Value
StateNum
9
2
1–50
SalesAgentNum
9
3
1–999
ValueOfSale
9
7
0.50–99999.99
Example Report
The first page of the example report is shown in Example 18-1. For ease of reference, I have attached line numbers to the report.
Example 18-1. Solace Solar Solutions Example Report: First Page
01 Solace Solar Solutions
02 Sales Agent - Sales and Salary Report Monthly Report
03
04 State Agent Value
05 Name Number Of Sales
06 Alabama 38 $9,325.14
07 $11,839.19
08 $19,102.61
09 Sales for sales agent 38 = $40,266.94
10
11
12 Alabama 73 $4,503.71
13 $11,659.87
14 $19,540.19
15 Sales for sales agent 73 = $35,703.77
16
17 Total sales for Alabama $75,970.71
18 Base salary for Alabama $1,149.00
19 ----------------------------------------------------------
20
21
478
Chapter 18 ■ the COBOL repOrt Writer
22 Alaska 55 $18,981.84
23 $3,065.97
24 $10,686.92
25 Sales for sales agent 55 = $32,734.73
26
27
28 Alaska 89 $11,187.72
29 $14,145.82
30 Sales for sales agent 89 = $25,333.54
31
32
33 Alaska 104 $18,005.42