Book Read Free

Michael Coughlan

Page 60

by Beginning COBOL for Programmers-Apress (2014) (pdf)


  CLOSE SalesFile, PrintFile.

  STOP RUN.

  PrintSalaryReport.

  GENERATE DetailLine.

  READ SalesFile

  AT END SET EndOfFile TO TRUE

  END-READ.

  The first thing to note is that the PROCEDURE DIVISION for this program is the same as for the program that produced the report in Example 18-1. This is your first indication that most of the work is being done in the DATA DIVISION. As I add complexity to the program in the succeeding examples, eventually I have to make some changes to the PROCEDURE DIVISION code.

  The second thing to note is that just like an ordinary print file, a Report Writer file must have a SELECT and ASSIGN

  clause in the ENVIRONMENT DIVISION and an FD entry in the DATA DIVISION. But look at the FD entry. Instead of a record description, you have a REPORT IS SolaceSalesReport entry. This entry tells you that the Report Writer is being used and that this particular report is called SolaceSalesReport. This entry links the PrintFile with the report described in the REPORT SECTION. That is the next thing to note; when you use the Report Writer, you describe the report in the REPORT SECTION.

  The first entry in the REPORT SECTION is the RD entry, which is followed by the name of the report. This is the same name you use in the REPORT IS entry in the FD entry of the PrintFile. The RD entry has a number of clauses. The first is the CONTROLS ARE clause, which allows you to identify the control-break item(s) that the Report Writer must monitor in order to detect a control break. These entries usually identify fields in an input file, but they don’t have to.

  The remaining RD clauses specify information about the page, such as the size of the page, the first line where a detail line may be printed, and the line after which the footer may be printed.

  The remaining entries identify the required report groups. Each report group is a record and must start with a 01 level number. As noted in Example 18-3, there are seven possible types of report groups, and the first entry in the report group must specify the type of the group. Listing 18-1 has the following report groups:

  • A REPORT HEADING group that specifies what is to be printed at the start of the report.

  • A PAGE HEADING group that specifies what is to be printed at the top of each page.

  • A DETAIL group that specifies what is to be printed for each sales record.

  • CONTROL FOOTING groups for the SalesAgentNum control-break item and the StateNum

  control-break item. These groups specify what is printed when a control break occurs on

  SalesAgentNum or StateNum.

  • A PAGE FOOTING group that specifies what is to be printed at the bottom of each page.

  Report Groups

  Let’s look at some of the entries in these report groups in more detail. The REPORT HEADING group is of interest because it demonstrates absolute position using the LINE clause. It also demonstrates the COLUMN clause, which you use to specify the horizontal placement of the material to be printed. The final item of interest in this group is the NEXT

  GROUP PLUS clause, which specifies that the next group will start one line down from this group. Although, as you see with the PAGE HEADING group, you can specify vertical placement using a relative reference rather than an absolute line number, that is not always sufficient for all your positioning needs. Sometimes you require a combination of the NEXT GROUP PLUS and the LINE IS PLUS clauses.

  487

  Chapter 18 ■ the COBOL repOrt Writer

  You might think that the PAGE HEADING group would also use absolute positioning, but in this example, the page headings are not printed on the same line on every page. On the first page, they are printed after the report heading lines; but on the other pages, they are printed on the first line. For this reason, LINE IS PLUS relative positioning is used for this group. Because the other groups, except the PAGE FOOTING group, are also printed on different lines on the page, they also use LINE IS PLUS relative positioning.

  The main clause of interest in the DETAIL report group is the SOURCE clause. This clause specifies that the data for this print item is to come from some source data item. This is how the Report Writer gets values for the state name (from the table), the sales agent number, and the value of the sale.

  SalesAgentGrp is a CONTROL FOOTING group. It is printed whenever there is a control break on SalesAgentNum.

  When you create a CONTROL group, you have to associate the group with a control item mentioned in the RD..CONTROLS

  ARE phrase. This is how the Report Write associates a particular control-break item with a CONTROL HEADING or FOOTING group. At the moment, this group only prints a line of hyphens; but the next program uses it to accumulate and print the total sales agent sales.

  The final group to consider is the PAGE FOOTING group. The item of interest in this group is PAGE-COUNTER, which is identified as the source of the page number printed in this footer. PAGE-COUNTER is a special Report Writer register that automatically keeps a count of the pages printed.

  PROCEDURE DIVISION Notes

  Now that you have seen the role played by the DATA DIVISION entries in producing the report, you need to know how the report is driven from the PROCEDURE DIVISION. The Report Writer introduces three new verbs: INITIATE, GENERATE, and TERMINATE.

  When the INITIATE verb is executed, all the heading groups, such as REPORT HEADING and the first PAGE HEADING, are produced. All the system registers, such as PAGE-COUNTER, are set to their starting values.

  When the TERMINATE verb is executed, all the relevant FOOTING groups, such as REPORT FOOTING and the last PAGE

  FOOTING, are produced.

  The report is driven by the GENERATE verb. GENERATE is normally associated either with the DETAIL group (as it is in this example) or with the report name. When GENERATE is associated with a DETAIL group, each time the GENERATE

  statement is executed, the DETAIL group is printed. Obviously this makes sense only if each time GENERATE executes, the DETAIL group is fed new data. In this program, the new data is provided by reading the sales file.

  Adding Features to the Report Program

  Let’s add some features to the Solace Sales Report program. Let’s change the program so that the report now shows the total sales for the sales agent, total sales for the state, and a final total for the country. The report should also show the base salary paid to sales agents in each state. To do this, the state table has to be modified to include the salary information. One final thing needs to change. If you look at the report in Example 18-4, you see that each line that prints a sales value also prints the state name and the sales agent number. This looks unsightly. The state name and the sales agent number should be suppressed after their first occurrence. Instead of this

  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

  488

  Chapter 18 ■ the COBOL repOrt Writer

  the report should print this:

  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

  You probably have realized by now that these specification changes are satisfied by the report shown in

  Example 18-1. Listing 18-2 is the program that produced that report.

  Listing 18-2. Program to Create a Report with Sales Totals

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing18-2.

  AUTHOR. Michael Coughlan.

  ENVIRONMENT DIVISION.

  INPUT-OUTPUT SECTION.

  FILE-CONTROL.

  SELECT SalesFile ASSIGN TO "Listing18-2-Sales.DAT"

  ORGANIZATION IS LINE SEQUENTIAL.

  SELECT PrintFile ASSIGN TO "Listing18-2.Rpt".

&
nbsp; 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(18) VALUE "1149Alabama".

  03 FILLER PIC X(18) VALUE "1536Alaska".

  03 FILLER PIC X(18) VALUE "1284Arizona".

  03 FILLER PIC X(18) VALUE "1064Arkansas".

  03 FILLER PIC X(18) VALUE "1459California".

  03 FILLER PIC X(18) VALUE "1508Colorado".

  03 FILLER PIC X(18) VALUE "1742Connecticut".

  03 FILLER PIC X(18) VALUE "1450Delaware".

  03 FILLER PIC X(18) VALUE "1328Florida".

  03 FILLER PIC X(18) VALUE "1257Georgia".

  489

  Chapter 18 ■ the COBOL repOrt Writer

  03 FILLER PIC X(18) VALUE "1444Hawaii".

  03 FILLER PIC X(18) VALUE "1126Idaho".

  03 FILLER PIC X(18) VALUE "1439Illinois".

  03 FILLER PIC X(18) VALUE "1203Indiana".

  03 FILLER PIC X(18) VALUE "1267Iowa".

  03 FILLER PIC X(18) VALUE "1295Kansas".

  03 FILLER PIC X(18) VALUE "1126Kentucky".

  03 FILLER PIC X(18) VALUE "1155Louisiana".

  03 FILLER PIC X(18) VALUE "1269Maine".

  03 FILLER PIC X(18) VALUE "1839Maryland".

  03 FILLER PIC X(18) VALUE "1698Massachusetts".

  03 FILLER PIC X(18) VALUE "1257Michigan".

  03 FILLER PIC X(18) VALUE "1479Minnesota".

  03 FILLER PIC X(18) VALUE "0999Mississippi".

  03 FILLER PIC X(18) VALUE "1236Missouri".

  03 FILLER PIC X(18) VALUE "1192Montana".

  03 FILLER PIC X(18) VALUE "1261Nebraska".

  03 FILLER PIC X(18) VALUE "1379Nevada".

  03 FILLER PIC X(18) VALUE "1571New Hampshire".

  03 FILLER PIC X(18) VALUE "1743New Jersey".

  03 FILLER PIC X(18) VALUE "1148New Mexico".

  03 FILLER PIC X(18) VALUE "1547New York".

  03 FILLER PIC X(18) VALUE "1237North Carolina".

  03 FILLER PIC X(18) VALUE "1290North Dakota".

  03 FILLER PIC X(18) VALUE "1256Ohio".

  03 FILLER PIC X(18) VALUE "1155Oklahoma".

  03 FILLER PIC X(18) VALUE "1309Oregon".

  03 FILLER PIC X(18) VALUE "1352Pennsylvania".

  03 FILLER PIC X(18) VALUE "1435Rhode Island".

  03 FILLER PIC X(18) VALUE "1172South Carolina".

  03 FILLER PIC X(18) VALUE "1206South Dakota".

  03 FILLER PIC X(18) VALUE "1186Tennessee".

  03 FILLER PIC X(18) VALUE "1244Texas".

  03 FILLER PIC X(18) VALUE "1157Utah".

  03 FILLER PIC X(18) VALUE "1374Vermont".

  03 FILLER PIC X(18) VALUE "1607Virginia".

  03 FILLER PIC X(18) VALUE "1487Washington".

  03 FILLER PIC X(18) VALUE "1062West Virginia".

  03 FILLER PIC X(18) VALUE "1393Wisconsin".

  03 FILLER PIC X(18) VALUE "1393Wyoming".

  02 FILLER REDEFINES StateNameValues.

  03 State OCCURS 50 TIMES.

  04 BaseSalary PIC 9(4).

  04 StateName PIC X(14).

  REPORT SECTION..

  RD SolaceSalesReport

  CONTROLS ARE FINAL

  StateNum

  SalesAgentNum

  490

  Chapter 18 ■ the COBOL repOrt Writer

  PAGE LIMIT IS 54

  FIRST DETAIL 3

  LAST DETAIL 42

  FOOTING 48.

  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) GROUP INDICATE.

  03 COLUMN 17 PIC ZZ9

  SOURCE SalesAgentNum GROUP INDICATE.

  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 2.

  03 COLUMN 15 PIC X(15) VALUE "Total sales for".

  03 COLUMN 31 PIC X(14) SOURCE StateName(StateNum).

  03 TotalStateSales COLUMN 45 PIC $$$$$,$$$.99 SUM TotalAgentSales.

  02 LINE IS PLUS 1.

  03 COLUMN 15 PIC X(15) VALUE "Base salary for".

  03 COLUMN 31 PIC X(14) SOURCE StateName(StateNum).

  03 COLUMN 48 PIC $$,$$$.99 SOURCE BaseSalary(StateNum).

  491

  Chapter 18 ■ the COBOL repOrt Writer

  02 LINE IS PLUS 1.

  03 COLUMN 1 PIC X(58) VALUE ALL "-".

  01 TotalSalesGrp TYPE IS CONTROL FOOTING FINAL.

  02 LINE IS PLUS 2.

  03 COLUMN 15 PIC X(11)

  VALUE "Total sales".

  03 COLUMN 46 PIC X VALUE "=".

  03 COLUMN 48 PIC $$,$$$,$$$.99 SUM TotalStateSales.

  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.

  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.

  Let’s look at the changes. The major differences between Listing 18-1 and Listing 18-2 are shown in bold. The PROCEDURE DIVISION has not changed. But there is a new control group: in order to print the final total for the country, a CONTROL FOOTING group is required. This control group is controlled by a special control item called FINAL. Note that now the CONTROLS ARE phrase in the report’s RD contains the word FINAL. The FINAL control-break item activates when the report is terminated. You should also take note of the order of the control-break items in the CONTROLS ARE

  phrase. FINAL is the major control break, StateNum is next in importance, and SalesAgentNum is the least important.

  A break on a major control item causes a break on all the subordinate control items.

  If you examine the report in Example 18-5, you’ll see that now StateName and SalesAgentNum are suppressed

  after their first occurrence. This is done by specifying the GROUP INDICATE clause for the data item.

  Another change to the program was requi
red to accumulate and print the total sales for the sales agent. The Report Writer has three ways of incrementing totals: subtotaling, rolling forward, and crossfooting. All these methods use the SUM clause but target different types of data item. Subtotaling targets data items in the FILE SECTION or the WORKING-STORAGE SECTION. Rolling forward targets data items in a subordinate CONTROL FOOTING group. Crossfooting targets data items in the same group. This example uses subtotaling and rolling forward.

  492

  Chapter 18 ■ the COBOL repOrt Writer

  Subtotaling is used in SalesAgentGrp to sum the sales made by the agent. A SUM clause is used that targets the ValueOfSale data item so that every time a GENERATE statement is executed, the current value of ValueOfSale is added to a sum counter. When the control break occurs, the CONTROL FOOTING group activates, and the value accumulated in the sum counter is printed.

  There is something else here that you note. As you have no doubt noticed, in the REPORT SECTION you don’t have to follow a level number with a data-item name or even the word FILLER. This saves a lot of unnecessary work. But you can include a name if you want to. In the SalesAgentGrp, I have included the name TotalAgentSales. The reason for naming this item is shown in the StateGrp CONTROL FOOTING group, where it is used as the target of the SUM clause.

  Every time there is a control break on SalesAgentNum, the current value of TotalAgentSales is added to the sum counter in StateGrp. When there is a control break on StateNum, the accumulated value of the sum counter is printed.

  This is an example of rolling forward.

  Rolling forward is also used with TotalStateSales in StateGrp. TotalStateSales is used as the target of the SUM clause in TotalSalesGrp to sum each state total into a final total. When the TERMINATE statement is executed, the final total is printed.

  Adding More Features to the Report Program

  In the specification at the start of this section, I mentioned that Solace sales agents are paid a base salary and a commission of 8% on the value of the products they sell. In Listing 18-3, each time the total sales for an agent are printed, the commission they have earned and their total salary (base salary + commission) should also be printed.

 

‹ Prev