Accumulating Running Totals



     1 IDENTIFICATION DIVISION.
     2 PROGRAM-ID.    RUNNING-TOTALS.
     3 AUTHOR.        BILL ROGERS.
     4
     5 ENVIRONMENT DIVISION.
     6 INPUT-OUTPUT SECTION.
     7 FILE-CONTROL.
     8     SELECT INPUT-DATA
     9         ASSIGN TO 'A:\METER.TXT'
    10         ORGANIZATION IS LINE SEQUENTIAL.
    11     SELECT REPORT-FILE
    12         ASSIGN TO 'A:\REPORT.TXT'
    13         ORGANIZATION IS LINE SEQUENTIAL.
    14
    15 DATA DIVISION.
    16 FILE SECTION.
    17 FD INPUT-DATA
    18    DATA RECORD IS INPUT-RECORD.
    19 01 INPUT-RECORD.
    20    02 INPUT-CUST-ID-NUMBER   PIC X(5).
    21    02 INPUT-CUST-NAME        PIC X(25).
    22    02 INPUT-KILOWATT-HOURS   PIC 9(4).
    23 FD REPORT-FILE
    24    DATA RECORD IS REPORT-LINE.
    25 01 REPORT-LINE PIC X(80).
    26
    27 WORKING-STORAGE SECTION.
    28 01 NO-MORE-DATA-SWITCH       PIC X           VALUE SPACE.
    29

ENERGY-RATE is a fixed value of .08. WS-ENERGY-CHARGE is calculated for each input record by multiplying the fixed rate by the number of kilowatt hours. Notice that WS-ENERGY-CHARGE must be a numeric field because arithmetic is performed by the COMPUTE statement in line 88. Then in line 90 the numeric field WS-ENERGY-CHARGE will be MOVEd to DETAIL-ENERGY-CHARGE, an edited numeric field (line 54), for printing.

    30 01 ENERGY-RATE               PIC V9(2)       VALUE .08.
    31 01 WS-ENERGY-CHARGE          PIC 9(4)V99.
    32

These next three items (TOTAL-NUMBER-OF-RECORDS, TOTAL-KILOWATT-HOURS, and TOTAL-ENERGY-CHARGE) are the accumulators, the running totals. Notice that they must be initialized to zero before arithmetic can be done (i.e., the ADD statements on lines 93, 94, and 95. These running totals, of course, are numeric fields. In the TOTAL-ROUTINE paragraph (lines 106-108) they are moved to corresponding edited numeric fields in the working-storage item TOTAL-LINE (lines 60, 63, 66).

    33 01 TOTAL-NUMBER-OF-RECORDS   PIC 9(4)        VALUE ZERO.
    34 01 TOTAL-KILOWATT-HOURS      PIC 9(8)        VALUE ZERO.
    35 01 TOTAL-ENERGY-CHARGE       PIC 9(8)V99     VALUE ZERO.
    36

The header line is printed at the top of the report. It consists of filler with literal values: space and column titles.

    37 01 HEADER-LINE.
    38    02 FILLER                 PIC X(10)       VALUE 'CUST#'.
    39    02 FILLER                 PIC X(28)       VALUE
    40       'CUSTOMER NAME'.
    41    02 FILLER                 PIC X(6)        VALUE
    42       'KW HRS'.
    43    02 FILLER                 PIC X(3)        VALUE SPACES.
    44    02 FILLER                 PIC X(10)       VALUE
    45       'AMT BILLED'.
    46
    
The DETAIL-LINE is the formatted output line. Customer information is MOVEd to it from the input record (lines 85-87). Calculated information is also MOVEd to it (line 90).

    47 01 DETAIL-LINE.
    48    02 DETAIL-CUST-ID-NUMBER  PIC X(5).
    49    02 FILLER                 PIC X(5)       VALUE SPACES.
    50    02 DETAIL-CUST-NAME       PIC X(25).
    51    02 FILLER                 PIC X(5)        VALUE SPACES.
    52    02 DETAIL-KILOWATT-HOURS  PIC ZZZ9.
    53    02 FILLER                 PIC X(5)        VALUE SPACES.
    54    02 DETAIL-ENERGY-CHARGE   PIC Z,ZZ9.99.
    55

The TOTAL-LINE prints at the bottom of the report to show the final values for the running totals. It consists of filler with literal values to space out the information and provide captions in addition to edited numeric fields to display the total values. (lines 106-110)

    56 01 TOTAL-LINE.
    57    02 FILLER                 PIC X(10)       VALUE 'TOTALS->'.
    58    02 FILLER                 PIC X(11)       VALUE
    59       'CUSTOMERS: '.
    60    02 TL-NUMBER-OF-RECORDS   PIC ZZZ9.
    61    02 FILLER                 PIC X(13)       VALUE
    62       '   KW HOURS: '.
    63    02 TL-KILOWATT-HOURS      PIC ZZ,ZZZ,ZZ9.
    64    02 FILLER                 PIC X(18)       VALUE
    65       '   AMOUNT BILLED: '.
    66    02 TL-ENERGY-CHARGE       PIC ZZ,ZZZ,ZZ9.99.
    67

STARTUP-PARAGRAPH: opens the files and does the initial read of the input file.
PRINT-HEADER: prints the header line once at the top of the report.
PROCESS-RECORDS: a loop that executes once for each input record. For each input record a detail line is printed and values are added to the running totals.
TOTAL-ROUTINE: When all the records have been processed the total line is printed at the bottom.
CLEANUP-PARAGRAPH: closes the files.

    68 PROCEDURE DIVISION.
    69 MAIN-PARAGRAPH.
    70     PERFORM STARTUP-PARAGRAPH.
    71     PERFORM PRINT-HEADER.
    72     PERFORM PROCESS-RECORDS
    73         UNTIL NO-MORE-DATA-SWITCH = 'Y'.
    74     PERFORM TOTAL-ROUTINE.
    75     PERFORM CLEANUP-PARAGRAPH.
    76     STOP RUN.
    77
    78 STARTUP-PARAGRAPH.
    79     OPEN INPUT INPUT-DATA.
    80     OPEN OUTPUT REPORT-FILE.
    81     READ INPUT-DATA
    82          AT END MOVE 'Y' TO NO-MORE-DATA-SWITCH.
    83

The PROCESS-RECORDS paragraph is performed as a loop, once for each input record until all records in the input file have been processed.

    84 PROCESS-RECORDS.
set up the detail line
    85     MOVE INPUT-CUST-ID-NUMBER TO DETAIL-CUST-ID-NUMBER.
    86     MOVE INPUT-CUST-NAME      TO DETAIL-CUST-NAME.
    87     MOVE INPUT-KILOWATT-HOURS TO DETAIL-KILOWATT-HOURS.
    88     COMPUTE WS-ENERGY-CHARGE ROUNDED =
    89             ENERGY-RATE * INPUT-KILOWATT-HOURS.
    90     MOVE WS-ENERGY-CHARGE TO DETAIL-ENERGY-CHARGE.
move the completed detail line to the output record
    91     MOVE DETAIL-LINE TO REPORT-LINE.
write the output record
    92     WRITE REPORT-LINE.
add to the running totals
    93     ADD 1 TO TOTAL-NUMBER-OF-RECORDS.
    94     ADD WS-ENERGY-CHARGE TO TOTAL-ENERGY-CHARGE.
    95     ADD INPUT-KILOWATT-HOURS TO TOTAL-KILOWATT-HOURS.
read the next record
    96     READ INPUT-DATA
    97          AT END MOVE 'Y' TO NO-MORE-DATA-SWITCH.
    98

Notice the pattern: you MOVE a formatted line from working-storage to the output record, then you WRITE the output record.

    99 PRINT-HEADER.
   100     MOVE HEADER-LINE TO REPORT-LINE.
   101     WRITE REPORT-LINE AFTER ADVANCING PAGE.
   102     MOVE SPACES TO REPORT-LINE.
   103     WRITE REPORT-LINE.
   104
   105 TOTAL-ROUTINE.
   106     MOVE TOTAL-NUMBER-OF-RECORDS TO TL-NUMBER-OF-RECORDS.
   107     MOVE TOTAL-KILOWATT-HOURS    TO TL-KILOWATT-HOURS.
   108     MOVE TOTAL-ENERGY-CHARGE     TO TL-ENERGY-CHARGE.
   109     MOVE TOTAL-LINE TO REPORT-LINE.
   110     WRITE REPORT-LINE AFTER 2.
   111
   112 CLEANUP-PARAGRAPH.
   113     CLOSE INPUT-DATA.
   114     CLOSE REPORT-FILE.


Cobol Home