Michael Coughlan
Page 63
509
Chapter 18 ■ the COBOL repOrt Writer
Note the following:
• ReportGroupName must not appear in more than one USE statement.
• The GENERATE, INITIATE, and TERMINATE statements must not appear in the declaratives.
• The value of any control data items must not be altered in the declaratives.
• Statements in the declaratives must not reference procedures outside the declaratives.
Listing 18-3 has a good example of how declaratives may be used to extend the functionality of the Report Writer.
They are used to calculate the sales agent SalesCommission and FullSalary before SalesAgentGrp is printed. For convenience, the code is repeated in Example 18-10.
Example 18-10. Declaratives from Listing 18-3
PROCEDURE DIVISION.
DECLARATIVES.
Calc SECTION.
USE BEFORE REPORTING SalesAgentGrp.
Calculate-Salary.
MULTIPLY TotalAgentSales BY Percentage
GIVING SalesCommission ROUNDED
ADD SalesCommission, BaseSalary(StateNum)
GIVING FullSalary.
END DECLARATIVES.
Main SECTION.
Begin.
OPEN INPUT SalesFile
OPEN OUTPUT PrintFile
The SUPPRESS PRINTING Statement
The SUPPRESS PRINTING statement is used in a DECLARATIVES section to stop a particular report group from being printed. The report group suppressed is the one mentioned in the USE statement associated with the section containing the SUPPRESS PRINTING statement. The SUPPRESS PRINTING statement must be executed each time you want to stop the report group from being printed.
In Example 18-11, the CONTROL FOOTING data for StateGrp is printed only if the state sales total is above a certain threshold.
Example 18-11. Suppressing the Printing of a Report Group
PROCEDURE DIVISION.
DECLARATIVES.
CheckStateSales SECTION.
USE BEFORE REPORTING StateGrp.
PrintImportantStatesOnly.
IF TotalStateSales < 100000
SUPPRESS PRINTING
END-IF.
END DECLARATIVES.
510
Chapter 18 ■ the COBOL repOrt Writer
Control-Break Registers
The Report Writer maintains a special control-break register for each control-break item mentioned in the CONTROLS
ARE phrase in the RD entry. When a control-break item is referred to in a control footer or in the declaratives, the Report Writer supplies the value held in the control-break register and not the value in the item itself. If a control break has just occurred, the value in the control-break register is the previous value of the control-break item.
This point is demonstrated in the report produced by Listing 18-3 by printing the actual state number (the one in the record buffer) and the supplied state number (the one in the control-break register) each time StateGrp group is printed.
Using Declaratives with Files
You can also use declaratives to handle file operation errors. The metalanguage for the version of USE used with files is given in Figure 18-10.
Figure 18-10. Metalanguage for the files version of USE
When you use this version of the declaratives, you can create code that deals with any I-O error on a particular file or more generalized code that deals with INPUT, OUTPUT, I-O, or EXTEND errors on any file. When declaratives exist for a file, the INVALID KEY clause and the AT END clause are optional.
The program fragment in Example 18-12 shows how you can use declaratives to handle errors on a particular file.
Example 18-12. Declarative Procedures to Handle Unexpected File Errors
PROCEDURE DIVISION.
DECLARATIVES.
SupFile SECTION.
USE AFTER ERROR PROCEDURE ON SupplierFile.
DisplaySupplierFileStatus.
DISPLAY "Unexpected error on Supplier file. Status = " SupplierStatus
DISPLAY "The file name used was :- " SupplierFileName
STOP RUN.
VidFile SECTION.
USE AFTER ERROR PROCEDURE ON VideoFile.
DisplayVideoFileStatus.
DISPLAY "Unexpected error on Video file. Status = " VideoStatus
DISPLAY "The file name used was :- " VidFileName
STOP RUN.
511
Chapter 18 ■ the COBOL repOrt Writer
END DECLARATIVES.
Main SECTION.
Begin.
OPEN INPUT SupplierFile
OPEN INPUT VideoFile
etc
Summary
This chapter introduced you to the COBOL Report Writer. In a series of increasingly complex programs, you learned how to use the Report Writer to create control-break-based reports. You were introduced to a number of new verbs, clauses, sections, and concepts. You saw how to use the RD entry in the REPORT SECTION to specify control-break items and define the basic layout of the report page. You were introduced to the idea of a report group and shown how to create report groups linked to control-break items. You learned how to use the SUM clause for totaling and rolling forward. The final program demonstrated how to extend the capabilities of the Report Writer by using declaratives.
You then covered the verbs, clauses, and concepts of the Report Writer. And in the final section, you saw how to use declaratives for error handling in files.
The final chapter introduces OO-COBOL. This book adheres to the ANS 85 COBOL standard, so the ISO 2002
OO-COBOL is somewhat outside its remit. Nevertheless, some of the drawbacks of contained subprograms are remedied by OO-COBOL, and it is from this perspective that I examine the topic. Do not expect a course in object-oriented programming.
prOGraMMING eXerCISe
and now for a programming exercise that tests your understanding of the last two chapters. For this, you need a really sharp 2B pencil.
Introduction
Two months before the beginning of each semester, Campus Bookshop produces a Purchase Requirements Report.
This report details the books that have to be purchased for the coming semester. In the past, this was done manually; but now management has decided to computerize the operation. Accordingly, lecturers’ requirements have been captured and the results used to update a purchase requirements file. This is a permanent file that contains details of the lecturers’ book requirements for both semesters.
You are required to write a program to produce a Purchase Requirements Report from the publisher, book, and purchase requirements files. The report should be sequenced on ascending publisher name and should only detail the purchase requirements for the semester under scrutiny.
The semester number (1 or 2) should be accepted from the user at the start of the program using a simple ACCEPT
and DISPLAY.
512
Chapter 18 ■ the COBOL repOrt Writer
File Descriptions
Purchase Requirements File (Indexed)
There is a record for each book title required by a lecturer. Note that a book may be required by more than one lecturer.
Field
KeyType
Type
Length
Value
PR-Number
Primary
9
4
1–9999
Lecturer-Name
Alt with duplicates
X
20
--
Book-Number
Alt with duplicates
9
4
1–9999
Module-Code
--
X
5
--
Copies-Required
--
9
3
1–999
Semester
--
9
1
1/2
Book File (Indexed)
/>
Field
KeyType
Type
Length
Value
Book-Number
Primary
9
4
1–9999
Publisher-Number
Alt with duplicates
9
4
1–9999
Book-Title
--
X
30
--
Publisher File (Indexed)
Field
KeyType
Type
Length
Value
Publisher-Number
Primary
9
4
1–9999
Publisher-Name
Alt with duplicates
X
20
--
Publisher-Address
--
X
40
--
Print Specification
The report must be printed according to the print specification given in Figure 18-11.
513
Chapter 18 ■ the COBOL repOrt Writer
Figure 18-11. Print specification for the Purchase Requirements Report
The publisher name must be suppressed after its first occurrence. The headings should be printed at the top of each page, and *** END OF REPORT *** should be printed on line 56 on the last page of the report.
Ordinarily, a new page is required after line 50.
The Qty field, which is a synonym for Copies-Required, should be zero suppressed up to but not including the last digit.
The page number field should also be zero suppressed.
prOGraMMING eXerCISe: SOLUtION
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing18-4.
AUTHOR. MICHAEL COUGHLAN.
*The Campus Bookshop Purchase Requirements Report (DP291-91-EXAM)
*Originally written for VAX COBOL 1991
*Converted to Microfocus COBOL 2002
*Modified for COBOL book 2014
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PurchaseReqFile ASSIGN TO "Listing18-4-PRF.DAT"
514
Chapter 18 ■ the COBOL repOrt Writer
ORGANIZATION IS INDEXED
FILE STATUS IS FileStatus-PRF
ACCESS MODE IS DYNAMIC
RECORD KEY IS PRNumber-PRF
ALTERNATE RECORD KEY IS LecturerName-PRF
WITH DUPLICATES
ALTERNATE RECORD KEY IS BookNum-PRF
WITH DUPLICATES.
SELECT BookFile ASSIGN TO "Listing18-4-BF.DAT"
ORGANIZATION IS INDEXED
FILE STATUS IS FileStatus-BF
ACCESS MODE IS DYNAMIC
RECORD KEY IS BookNum-BF
ALTERNATE RECORD KEY IS PublisherNum-BF
WITH DUPLICATES.
SELECT PublisherFile ASSIGN TO "Listing18-4-PF.DAT"
ORGANIZATION IS INDEXED
FILE STATUS IS FileStatus-PF
ACCESS MODE IS DYNAMIC
RECORD KEY IS PublisherNum-PF
ALTERNATE RECORD KEY IS PublisherName-PF.
SELECT ReportFile ASSIGN TO "Listing18-4.RPT".
DATA DIVISION.
FILE SECTION.
FD PurchaseReqFile.
01 PurchaseRec-PRF.
88 EndOfPRequirements VALUE HIGH-VALUES.
88 NotEndOfPRequirements VALUE LOW-VALUES.
02 PRNumber-PRF PIC 9(4).
02 LecturerName-PRF PIC X(20).
02 BookNum-PRF PIC 9(4).
02 ModuleCode-PRF PIC X(5).
02 CopiesRequired-PRF PIC 9(3).
02 Semester-PRF PIC 9.
FD BookFile.
01 BookRec-BF.
88 EndOfBooks VALUE HIGH-VALUES.
88 NotEndOfBooks VALUE LOW-VALUES.
02 BookNum-BF PIC 9(4).
02 PublisherNum-BF PIC 9(4).
02 BookTitle-BF PIC X(30).
FD PublisherFile.
01 PublisherRec-PF.
88 EndOfPublishers VALUE HIGH-VALUES.
02 PublisherNum-PF PIC 9(4).
515
Chapter 18 ■ the COBOL repOrt Writer
02 PublisherName-PF PIC X(20).
02 PublisherAddress-PF PIC X(40).
FD ReportFile
REPORT IS PurchaseRequirementsReport.
WORKING-STORAGE SECTION.
01 File-Stati.
02 FileStatus-PRF PIC X(2).
88 PurchaseRec-PRF-Not-Found VALUE "23".
02 FileStatus-BF PIC X(2).
88 BookRec-Not-Found VALUE "23".
02 FileStatus-PF PIC X(2).
01 Current-Semester PIC 9.
REPORT SECTION.
RD PurchaseRequirementsReport
CONTROLS ARE FINAL
PublisherName-PF
PAGE LIMIT IS 66
HEADING 2
FIRST DETAIL 8
LAST DETAIL 50
FOOTING 55.
01 TYPE IS REPORT FOOTING.
02 LINE 56.
03 COLUMN 29 PIC X(23)
VALUE "*** END OF REPORT ***".
01 TYPE IS PAGE HEADING.
02 LINE 2.
03 COLUMN 27 PIC X(30)
VALUE "PURCHASE REQUIREMENTS REPORT".
03 COLUMN 77 PIC X(6)
VALUE "PAGE :".
03 COLUMN 84 PIC Z9 SOURCE PAGE-COUNTER.
02 LINE 3.
03 COLUMN 26 PIC X(32) VALUE ALL "-".
02 LINE 6.
03 COLUMN 2 PIC X(24) VALUE "PUBLISHER NAME".
03 COLUMN 33 PIC X(11) VALUE "BOOK TITLE".
03 COLUMN 57 PIC X(3) VALUE "QTY".
03 COLUMN 65 PIC X(14) VALUE "LECTURER NAME".
516
Chapter 18 ■ the COBOL repOrt Writer
01 PReq-PrintLine TYPE IS DETAIL.
02 LINE IS PLUS 2.
03 COLUMN 1 PIC X(20) SOURCE PublisherName-PF
GROUP INDICATE.
03 COLUMN 24 PIC X(30) SOURCE BookTitle-BF.
03 COLUMN 57 PIC ZZ9 SOURCE CopiesRequired-PRF.
03 COLUMN 63 PIC X(20) SOURCE LecturerName-PRF.
PROCEDURE DIVISION.
BEGIN.
DISPLAY "Enter the semester number (1 or 2) - " WITH NO ADVANCING
ACCEPT Current-Semester
OPEN INPUT PurchaseReqFile
OPEN INPUT BookFile
OPEN INPUT PublisherFile
OPEN OUTPUT ReportFile
INITIATE PurchaseRequirementsReport
MOVE SPACES TO PublisherName-PF
START PublisherFile
KEY IS GREATER THAN PublisherName-PF
INVALID KEY DISPLAY "START Pub file status" FileStatus-PF
END-START
READ PublisherFile NEXT RECORD
AT END SET EndOfPublishers TO TRUE
END-READ
PERFORM PrintRequirementsReport UNTIL EndOfPublishers
TERMINATE PurchaseRequirementsReport
CLOSE PurchaseReqFile, BookFile,
PublisherFile, ReportFile
STOP RUN.
PrintRequirementsReport.
SET NotEndOfBooks TO TRUE
MOVE PublisherNum-PF TO PublisherNum-BF
READ BookFile
KEY IS PublisherNum-BF
INVALID KEY
DISPLAY SPACES
DISPLAY "Book File Error. FileStatus = " FileStatus-BF
DISPLAY "Publisher Number - " PublisherNum-BF
DISPLAY "Publisher Rec = " PublisherRec-PF
MOVE ZEROS TO PublisherNum-BF
END-READ
PERFORM ProcessPublisher
UNTIL PublisherNum-PF NOT EQUAL TO PublisherNum-BF
OR EndOfBooks
517
Chapter 18 ■ the COBOL repOrt Writer
READ PublisherFile NEXT RECORD
AT END SET EndOfPublishers TO TRUE
END-READ.
r /> ProcessPublisher.
SET NotEndOfPRequirements TO TRUE
MOVE BookNum-BF TO BookNum-PRF
READ PurchaseReqFile
KEY IS BookNum-PRF
INVALID KEY
DISPLAY SPACES
DISPLAY "PurchReqFile Error. FileStatus = " FileStatus-PRF
DISPLAY "Book Num PRF = " BookNum-PRF
DISPLAY "Book Rec = " BookRec-BF
MOVE ZEROS TO BookNum-PRF
END-READ
PERFORM UNTIL BookNum-BF NOT EQUAL TO BookNum-PRF
OR EndOfPRequirements
IF Current-Semester = Semester-PRF THEN
Generate PReq-PrintLine
END-IF
READ PurchaseReqFile NEXT RECORD
AT END SET EndOfPRequirements TO TRUE
END-READ
END-PERFORM
READ BookFile NEXT RECORD
AT END SET EndOfBooks TO TRUE
END-READ.
518
Chapter 19
OO-COBOL
This chapter introduces you to OO-COBOL. This book adheres to the ANS 85 COBOL standard, so ISO 2002 OO-COBOL
is somewhat outside its remit. The ANS 85 version of COBOL was designed to bring structured programming to COBOL, but failings in the way contained subprograms were implemented meant this version did not fully live up to its promise.
However, the structured programming weaknesses of ANS 85 COBOL are remedied by OO-COBOL, and the chapter
examines OO-COBOL from this perspective. In this chapter, you see how OO-COBOL can be used to create informational strength modules that fully realize Parnas’s1 idea of information hiding. I show you some OO-COBOL programs and introduce you to classes and methods, but the chapter does not delve deeply into topics such as inheritance, polymorphism, properties, and interfaces. In other words, do not expect a course in object-oriented programming.
Module Strength and Module Coupling
Prior to the introduction of the ANS 74 version of COBOL, many COBOL systems consisted of huge, monolithic programs containing as many as 100,000 lines of code. It soon became clear that it was difficult, if not impossible, to maintain programs of this size. As a result, the ANS 74 version of COBOL introduced external subprograms that allowed programmers to create modular systems consisting of a number of independently compiled programs bound together into one run-unit. Unfortunately, this did not entirely solve the maintenance crisis. It turned out that some kinds of partitioned programs were as bad, or worse, than the monolithic programs they replaced. Using empirical research done at IBM as the basis for their ideas, Stevens, Myers, and Constantine2 addressed this issue by introducing structured programming and the criteria for decomposing a system into modules. In structured programming, a module is defined as any collection of executable program statements that meets all the following criteria: