Michael Coughlan
Page 59
34 $17,614.20
35 Sales for sales agent 104 = $35,619.62
36
37 Total sales for Alaska $93,687.89
38 Base salary for Alaska $1,536.00
39 ----------------------------------------------------------
40
41
42 Arizona 23 $4,237.72
43
44
45
46
47
48
49 Programmer - Michael Coughlan Page : 1
Report Writer Tasks
To get a feel for what the Report Writer can do, let’s examine in some detail what it has to do to produce this report:
• Print the report heading lines (lines 01–02). These are printed, once only, at the beginning of
the report.
• Print the subject heading lines (page headings). These are printed at the top of each page—on
lines 04–05 on the first page and lines 01–02 on subsequent pages.
• Print a footer at the bottom of each page (showing the name of the programmer and a page
number: line 49). To print the page number, the Report Writer must keep a page count.
• Keep a line count, and change the page when the count is greater than 42 (unless the next thing
to print is a sales agent total line, a state total line, a base salary line, or the final total line).
• Print the details of a sales agent’s sales (for example, as shown for sales agent 38 on lines 06–08).
Because the sales file only contains a state number, the Report Writer must get the state name
from a lookup table.
• Suppress the sales agent number and state name after their first occurrence (but restore them
if there is a change of page, sales agent, or state name: see lines 06, 12, 22, 28, 33, 42).
• When the sales agent number changes, print the total sales accumulated for the sales agent
(lines 15, 25, 35)
479
Chapter 18 ■ the COBOL repOrt Writer
• When the state number changes, print the total sales accumulated for the state (lines 17, 37).
• When the state number changes, get the base salary for the state from a lookup table, and print
it (lines 18, 38).
• When the state number changes, print a line of hyphens to separate this state from the next
(lines 19, 39).
Accumulate all the sales values, and print them as a final total at the end of the report (not shown on the example page).
Report Writer PROCEDURE DIVISION
If you had to create the Solace Solar Solutions sales report using the approach shown in Chapter 8 (that is, using a control-break program and the WRITE verb), you would probably write a program that had a PROCEDURE DIVISION
with more than 100 lines of code. It is interesting to discover that the Report Writer can do all this work in just the 10
COBOL statements shown in Example 18-2.
Example 18-2. PROCEDURE DIVISION That Produces the Sales Report
PROCEDURE DIVISION.
Begin.
OPEN INPUT SalesFile.
OPEN OUTPUT PrintFile.
READ SalesFile
AT END SET EndOfFile TO TRUE
END-READ.
INITIATE SolaceSalesReport.
PERFORM PrintSalaryReport
UNTIL EndOfFile.
TERMINATE SolaceSalesReport.
CLOSE SalesFile, PrintFile.
STOP RUN.
PrintSalaryReport.
GENERATE DetailLine.
READ SalesFile
AT END SET EndOfFile TO TRUE
END-READ.
So Much Work, So Little Code
How can so much work be done in so little PROCEDURE DIVISION code? How does the Report Writer know that page headings or page footers are required? If you wrote a program to print this report using WRITE statements, you would need a control-break program with a PERFORM UNTIL StateNum NOT EQUAL TO PrevStateNum loop to process each
state and an inner loop PERFORM UNTIL SalesAgentNum NOT EQUAL TO PrevSalesAgentNum to process the sales for each sales agent. Without those loops, how does the Report Writer know it is time to print the sales agent totals or the state totals, and how does it accumulate those totals in the first place?
To achieve so much in so little PROCEDURE DIVISION code, the Report Writer uses a declarative approach to
programming rather than the imperative (procedural) approach familiar to most programmers. In imperative
programming, you tell the computer how to do what you want done. In declarative programming, you tell the
computer what you would like done, and the computer works out how to do it. When you use the Report Writer, you 480
Chapter 18 ■ the COBOL repOrt Writer
declare what to print when a page heading, page footer, sales agent total, state total, or final total is required, and the Report Writer works out when to print these items. In keeping with the adage that “there is no such thing as a free lunch,” the PROCEDURE DIVISION of a Report Writer program is short because most of the work is done in the (greatly expanded) DATA DIVISION.
How the Report Writer Works
The Report Writer works by recognizing that many reports take (more or less) the same shape. There may be headings at the beginning of the report and footers at the end. There may be headings at the top of each page and footers at the bottom. Headings or footers may need to be printed whenever there is a control break (that is, when the value in a specified field changes, such as when the sales agent number or state number changes in Example 18-1). In addition, the detail lines that display the information summarized in control-break totals also need to be printed.
The Report Writer calls these different report items report groups. Reports are organized around report groups.
The Report Writer recognizes the seven types of report group shown in Example 18-3; the indentation shows their relative importance/order of execution.
Example 18-3. Report Group Types
REPORT HEADING or RH group
- printed once at the beginning of the report
PAGE HEADING or PH group
- printed at the top of each page
CONTROL HEADING or CH group
- printed at the beginning of each control break
DETAIL or DE group
- printed each time the GENERATE statement is executed
CONTROL FOOTING or CF group
- printed at the end of each control break
PAGE FOOTING or PF group
- printed at the bottom of each page
REPORT FOOTING or RF group
- printed once at the end of the report.
For each report, there must be a Report Description (RD) entry in the REPORT SECTION of the DATA DIVISION that fully describes the report. The report groups that describe the report are defined as records in the RD entries. Most groups are defined once for each report, but control groups are defined for each control-break item. For instance, in the example program, control footings are defined on SalesAgentNum, StateNum, and FINAL. FINAL is a special control group that is invoked before or after the normal control groups (before if CONTROL HEADING FINAL is used, and after if CONTROL FOOTING FINAL is used).
Ordinary control groups are defined on a control-break data item. The Report Writer monitors the contents of the designated data item, and when the value changes, a control break is automatically initiated. When the control break is initiated, the CONTROL FOOTING group of the breaking item (if there is one) and the CONTROL HEADING group of the next item are printed.
Writing a report program consists of a number of tedious tasks such as keeping track of the line count to ensure that page headings or footers are printed when required, or simply moving data values into their corresponding items in the print line. In addition, when you write a report according to a program specification, you often h
ave to adhere to the report layout specified in a print layout form such as that shown in Figure 18-1. When you have to adhere to such a form, it can be tricky to get the vertical and horizontal placement of printed items correct. Counting characters to figure out what size to make each of the fields that define a print line is tedious and time consuming.
481
Chapter 18 ■ the COBOL repOrt Writer
Figure 18-1. Print layout form showing the layout required for a report
The Report Writer makes it easier to write report programs by
• Allowing simple vertical and horizontal placement of printed items using the LINE IS and
COLUMN IS phrases in the data declaration
• Automatically moving data values to output items using the SOURCE phrase
• Keeping a line count, and automatically generating report and page headers and footers at the
appropriate times
• Keeping a page count that can be referenced in the report declaration
• Recognizing control breaks, and automatically generating the appropriate control headings
and footers
• Automatically accumulating totals, subtotals, and final totals
Writing a Report Program
Let’s see how to write a report program using the Report Writer. I start with a simplified version of the program that produced the report in Example 18-1. Succeeding examples add to it to demonstrate additional Report Writer facilities. The final example demonstrates even more than the report in Example 18-1.
482
Chapter 18 ■ the COBOL repOrt Writer
Modifying the Specification
This first example program creates a report program that does the following:
• Prints a report heading
• Prints a heading and a footer on each page
• For each sale record, prints the state name (obtained from a table), the sales agent number,
and the value of the sale
• Prints the total value of the sales made by each sales agent
• Prints a line of hyphens at the end of each state to separate the states from one another
The first page of the report produced by this program is shown in Example 18-4 (line numbers have been added).
The program that produces the report is shown in Listing 18-1.
Example 18-4. Simplified Version of the Report Showing Only Sales Agent Totals
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 Alabama 38 $11,839.19
08 Alabama 38 $19,102.61
09 Sales for sales agent 38 = $40,266.94
10
11
12 Alabama 73 $4,503.71
13 Alabama 73 $11,659.87
14 Alabama 73 $19,540.19
15 Sales for sales agent 73 = $35,703.77
16 ----------------------------------------------------------
17
18
19 Alaska 55 $18,981.84
20 Alaska 55 $3,065.97
21 Alaska 55 $10,686.92
22 Sales for sales agent 55 = $32,734.73
23
24
25 Alaska 89 $11,187.72
26 Alaska 89 $14,145.82
27 Sales for sales agent 89 = $25,333.54
28
29
30 Alaska 104 $18,005.42
31 Alaska 104 $17,614.20
32 Sales for sales agent 104 = $35,619.62
33 ----------------------------------------------------------
34
35
483
Chapter 18 ■ the COBOL repOrt Writer
36 Arizona 23 $4,237.72
37 Arizona 23 $13,315.00
38 Sales for sales agent 23 = $17,552.72
39
40
41 Arizona 90 $2,078.93
42 Arizona 90 $17,228.88
43 Arizona 90 $8,929.96
44 Sales for sales agent 90 = $28,237.77
45 ----------------------------------------------------------
46
47
48
49 Programmer - Michael Coughlan Page : 1
Listing 18-1. Simplified Report Program
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing18-1.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SalesFile ASSIGN TO "Listing18-1-Sales.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT PrintFile ASSIGN TO "Listing18-1.Rpt".
DATA DIVISION.
FILE SECTION.
FD SalesFile.
01 SalesRecord.
88 EndOfFile VALUE HIGH-VALUES.
02 StateNum PIC 99.
02 SalesAgentNum PIC 999.
02 ValueOfSale PIC 9(5)V99.
FD PrintFile
REPORT IS SolaceSalesReport.
WORKING-STORAGE SECTION.
01 StateNameTable.
02 StateNameValues.
03 FILLER PIC X(14) VALUE "Alabama".
03 FILLER PIC X(14) VALUE "Alaska".
03 FILLER PIC X(14) VALUE "Arizona".
03 FILLER PIC X(14) VALUE "Arkansas".
03 FILLER PIC X(14) VALUE "California".
03 FILLER PIC X(14) VALUE "Colorado".
03 FILLER PIC X(14) VALUE "Connecticut".
03 FILLER PIC X(14) VALUE "Delaware".
03 FILLER PIC X(14) VALUE "Florida".
03 FILLER PIC X(14) VALUE "Georgia".
484
Chapter 18 ■ the COBOL repOrt Writer
03 FILLER PIC X(14) VALUE "Hawaii".
03 FILLER PIC X(14) VALUE "Idaho".
03 FILLER PIC X(14) VALUE "Illinois".
03 FILLER PIC X(14) VALUE "Indiana".
03 FILLER PIC X(14) VALUE "Iowa".
03 FILLER PIC X(14) VALUE "Kansas".
03 FILLER PIC X(14) VALUE "Kentucky".
03 FILLER PIC X(14) VALUE "Louisiana".
03 FILLER PIC X(14) VALUE "Maine".
03 FILLER PIC X(14) VALUE "Maryland".
03 FILLER PIC X(14) VALUE "Massachusetts".
03 FILLER PIC X(14) VALUE "Michigan".
03 FILLER PIC X(14) VALUE "Minnesota".
03 FILLER PIC X(14) VALUE "Mississippi".
03 FILLER PIC X(14) VALUE "Missouri".
03 FILLER PIC X(14) VALUE "Montana".
03 FILLER PIC X(14) VALUE "Nebraska".
03 FILLER PIC X(14) VALUE "Nevada".
03 FILLER PIC X(14) VALUE "New Hampshire".
03 FILLER PIC X(14) VALUE "New Jersey".
03 FILLER PIC X(14) VALUE "New Mexico".
03 FILLER PIC X(14) VALUE "New York".
03 FILLER PIC X(14) VALUE "North Carolina".
03 FILLER PIC X(14) VALUE "North Dakota".
03 FILLER PIC X(14) VALUE "Ohio".
03 FILLER PIC X(14) VALUE "Oklahoma".
03 FILLER PIC X(14) VALUE "Oregon".
03 FILLER PIC X(14) VALUE "Pennsylvania".
03 FILLER PIC X(14) VALUE "Rhode Island".
03 FILLER PIC X(14) VALUE "South Carolina".
03 FILLER PIC X(14) VALUE "South Dakota".
03 FILLER PIC X(14) VALUE "Tennessee".
03 FILLER PIC X(14) VALUE "Texas".
03 FILLER PIC X(14) VALUE "Utah".
03 FILLER PIC X(14) VALUE "Vermont".
03 FILLER PIC X(14) VALUE "Virginia".
03 FILLER PIC X(14) VALUE "Washington".
03 FILLER PIC X(14) VALUE "West Virginia".
03 FILLER PIC X(14) VALUE "Wisconsin".
03 FILLER PIC X(14) VALUE "Wyoming".
02 FILLER REDEFINES StateNameValues.
03 State OCCURS 50 TIMES.
04 StateName PIC X(14).
REPORT SECTION.
RD SolaceSalesReport
CONTROLS ARE StateNum
SalesAgentNum
PAGE LIMIT IS 54
FIRST DETAIL 3
LAST DETAIL 46
FOOTING 48.
485
Chapter 18 ■ the COBOL repOrt Writer
01 TYPE IS REPORT HEADING NEXT GROUP PlUS 1.
02 LINE 1.
03 COLUMN 20 PIC X(32)
VALUE "Solace Solar Solutions".
02 LINE 2.
03 COLUMN 6 PIC X(51)
VALUE "Sales Agent - Sales and Salary Report Monthly Report".
01 TYPE IS PAGE HEADING.
02 LINE IS PLUS 1.
03 COLUMN 2 PIC X(5) VALUE "State".
03 COLUMN 16 PIC X(5) VALUE "Agent".
03 COLUMN 32 PIC X(8) VALUE "Value".
02 LINE IS PLUS 1.
03 COLUMN 2 PIC X(4) VALUE "Name".
03 COLUMN 16 PIC X(6) VALUE "Number".
03 COLUMN 31 PIC X(8) VALUE "Of Sales".
01 DetailLine TYPE IS DETAIL.
02 LINE IS PLUS 1.
03 COLUMN 1 PIC X(14)
SOURCE StateName(StateNum).
03 COLUMN 17 PIC ZZ9
SOURCE SalesAgentNum.
03 COLUMN 30 PIC $$$,$$$.99 SOURCE ValueOfSale.
01 SalesAgentGrp
TYPE IS CONTROL FOOTING SalesAgentNum NEXT GROUP PLUS 2.
02 LINE IS PLUS 1.
03 COLUMN 15 PIC X(21) VALUE "Sales for sales agent".
03 COLUMN 37 PIC ZZ9 SOURCE SalesAgentNum.
03 COLUMN 43 PIC X VALUE "=".
03 TotalAgentSales COLUMN 45 PIC $$$$$,$$$.99 SUM ValueOfSale.
01 StateGrp TYPE IS CONTROL FOOTING StateNum NEXT GROUP PLUS 2.
02 LINE IS PLUS 1.
03 COLUMN 1 PIC X(58) VALUE ALL "-".
01 TYPE IS PAGE FOOTING.
02 LINE IS 49.
03 COLUMN 1 PIC X(29) VALUE "Programmer - Michael Coughlan".
03 COLUMN 55 PIC X(6) VALUE "Page :".
03 COLUMN 62 PIC ZZ9 SOURCE PAGE-COUNTER.
PROCEDURE DIVISION.
Begin.
OPEN INPUT SalesFile.
OPEN OUTPUT PrintFile.
READ SalesFile
AT END SET EndOfFile TO TRUE
END-READ.
486
Chapter 18 ■ the COBOL repOrt Writer
INITIATE SolaceSalesReport.
PERFORM PrintSalaryReport
UNTIL EndOfFile.
TERMINATE SolaceSalesReport.