Category >
COBOL
|| Published on :
Friday, May 8, 2015 || Views:
4825
||
Indexed Files Relative Files Print Files READ START Tables DIVIDE COBOL Example COBOL Program
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. TopSupplierRpt.
AUTHOR. MICHAEL COUGHLAN.
*DP291-90-EXAM.
*Originally written for VAX COBOL 1991
*Converted to Microfocus NetExpress 2002
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT VIDEO-DETAILS-FILE ASSIGN TO "VDF.DAT"
ORGANIZATION IS INDEXED
FILE STATUS IS VDF-STATUS
ACCESS MODE IS DYNAMIC
RECORD KEY IS VDF-VIDEO-NUM
ALTERNATE RECORD KEY IS VDF-VIDEO-CODE
WITH DUPLICATES.
SELECT VIDEO-FILE ASSIGN TO "VIDEO.DAT"
ORGANIZATION IS INDEXED
FILE STATUS IS VIDEO-STATUS
ACCESS MODE IS DYNAMIC
RECORD KEY IS VIDEO-CODE
ALTERNATE RECORD KEY IS VIDEO-TITLE
ALTERNATE RECORD KEY IS VIDEO-SUPPLIER-CODE
WITH DUPLICATES.
SELECT SUPPLIER-FILE ASSIGN TO "SUPPLIER.DAT"
ORGANIZATION IS RELATIVE
FILE STATUS IS SUPPLIER-STATUS
ACCESS MODE IS RANDOM
RELATIVE KEY IS SUPPLIER-KEY.
SELECT REPORT-FILE ASSIGN TO "TopSuppliers.Rpt".
DATA DIVISION.
FILE SECTION.
FD VIDEO-DETAILS-FILE.
01 VDF-RECORD.
02 VDF-VIDEO-NUM PIC X(5).
02 VDF-VIDEO-CODE PIC 9(5).
02 VDF-RENTAL-EARNINGS PIC 9(4)V99.
02 VDF-PURCHASE-PRICE PIC 999V99.
FD VIDEO-FILE.
01 VIDEO-RECORD.
02 VIDEO-CODE PIC 9(5).
02 VIDEO-TITLE PIC X(30).
02 VIDEO-SUPPLIER-CODE PIC 99.
FD SUPPLIER-FILE.
01 SUPPLIER-RECORD.
02 SUPPLIER-CODE PIC 99.
02 SUPPLIER-NAME PIC X(20).
02 SUPPLIER-ADDRESS PIC X(60).
FD REPORT-FILE.
01 PRINT-LINE PIC X(74).
WORKING-STORAGE SECTION.
01 FILE-STATUS-IDENTIFIERS.
02 VDF-STATUS PIC X(2).
02 VIDEO-STATUS PIC X(2).
02 SUPPLIER-STATUS PIC X(2).
01 END-OF-FILE-FLAGS.
02 FILLER PIC 9 VALUE 1.
88 VIDEO-FILE-END VALUE 0.
02 FILLER PIC 9 VALUE 1.
88 VDF-FILE-END VALUE 0.
88 NOT-VDF-END VALUE 1.
01 REPORT-HEADING.
02 FILLER PIC X(31) VALUE SPACES.
02 FILLER PIC X(22)
VALUE "TOP SUPPLIERS REPORT".
01 UNDER-LINE.
02 FILLER PIC X(30) VALUE SPACES.
02 FILLER PIC X(24) VALUE ALL "-".
01 ITEM-HEADING1.
02 FILLER PIC X(6) VALUE SPACES.
02 FILLER PIC X(8) VALUE "SUPPLIER".
02 FILLER PIC X(10) VALUE SPACES.
02 FILLER PIC X(5) VALUE "TOTAL".
02 FILLER PIC X(19) VALUE SPACES.
02 FILLER PIC X(4) VALUE "BEST".
02 FILLER PIC X(14) VALUE SPACES.
02 FILLER PIC X(7) VALUE "AVERAGE".
01 ITEM-HEADING2.
02 FILLER PIC X(8) VALUE SPACES.
02 FILLER PIC X(4) VALUE "NAME".
02 FILLER PIC X(11) VALUE SPACES.
02 FILLER PIC X(8) VALUE "EARNINGS".
02 FILLER PIC X(13) VALUE SPACES.
02 FILLER PIC X(12) VALUE "VIDEO TITLE".
02 FILLER PIC X(10) VALUE SPACES.
02 FILLER PIC X(8) VALUE "EARNINGS".
01 SUPPLIER-LINE.
02 PRN-SUPPLIER-NAME PIC X(20).
02 PRN-SUPPLIER-EARNINGS PIC B$$$$,$$9.99.
02 PRN-VIDEO-TITLE PIC BBX(30).
02 PRN-AVERAGE-EARNINGS PIC B$$,$$9.99.
01 REPORT-FOOTING.
02 FILLER PIC X(24).
02 FILLER PIC X(27)
VALUE "**** END OF REPORT ****".
01 TOP-SUPPLIERS.
02 TOP-SUPPLIER OCCURS 4 TIMES.
03 TOP-SUPP-CODE PIC 99.
03 TOP-SUPP-EARNINGS PIC 999999V99.
03 TOP-VIDEO-CODE PIC 9(5).
03 TOP-VIDEO-TITLE PIC X(30).
03 TOP-VIDEO-EARNINGS PIC 9999V99.
01 HOLD-SUPP-REC.
02 HOLD-SUPP-CODE PIC 99.
02 HOLD-SUPP-EARNINGS PIC 999999V99.
02 HOLD-VIDEO-CODE PIC 9(5).
02 HOLD-VIDEO-TITLE PIC X(30).
02 HOLD-VIDEO-EARNINGS PIC 9999V99.
01 COUNTERS.
02 TOTAL-VIDEO-EARNINGS PIC 9(5)V99.
02 AVERAGE-VIDEO-EARNINGS PIC 9(4)V99.
02 VIDEO-COUNT PIC 99.
02 SUPP-RANK PIC 9.
02 SUPPLIER-KEY PIC 99.
02 FILLER PIC 9.
88 CONTINUE-CHECKING VALUE 0.
88 STOP-CHECKING VALUE 1.
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT VIDEO-DETAILS-FILE.
OPEN INPUT VIDEO-FILE.
OPEN INPUT SUPPLIER-FILE.
OPEN OUTPUT REPORT-FILE.
PERFORM PRINT-HEADINGS.
MOVE ZEROS TO TOP-SUPPLIERS.
MOVE ZEROS TO VIDEO-SUPPLIER-CODE.
START VIDEO-FILE
KEY IS GREATER THAN VIDEO-SUPPLIER-CODE
INVALID KEY DISPLAY "START VIDEO-FILE STATUS :- " VIDEO-STATUS
END-START.
READ VIDEO-FILE NEXT RECORD
AT END SET VIDEO-FILE-END TO TRUE
END-READ.
PERFORM FIND-TOP-SUPPLIERS UNTIL VIDEO-FILE-END.
PERFORM PRINT-TOP-SUPPLIER-DETAILS
WRITE PRINT-LINE FROM REPORT-FOOTING
AFTER ADVANCING 4 LINES.
CLOSE VIDEO-DETAILS-FILE,
VIDEO-FILE,
SUPPLIER-FILE,
REPORT-FILE.
STOP RUN.
PRINT-HEADINGS.
MOVE SPACES TO PRINT-LINE
WRITE PRINT-LINE AFTER ADVANCING PAGE.
WRITE PRINT-LINE FROM REPORT-HEADING
AFTER ADVANCING 1 LINE
WRITE PRINT-LINE FROM UNDER-LINE
AFTER ADVANCING 1 LINE
WRITE PRINT-LINE FROM ITEM-HEADING1
AFTER ADVANCING 2 LINES
WRITE PRINT-LINE FROM ITEM-HEADING2
AFTER ADVANCING 1 LINE.
PRINT-TOP-SUPPLIER-DETAILS.
PERFORM VARYING SUPP-RANK FROM 1 BY 1 UNTIL SUPP-RANK GREATER THAN 3
MOVE TOP-SUPP-CODE(SUPP-RANK) TO SUPPLIER-KEY
READ SUPPLIER-FILE
INVALID KEY DISPLAY "PTSD FS= " SUPPLIER-STATUS
END-READ
MOVE SUPPLIER-NAME TO PRN-SUPPLIER-NAME
MOVE TOP-SUPP-EARNINGS(SUPP-RANK) TO PRN-SUPPLIER-EARNINGS
MOVE TOP-VIDEO-TITLE(SUPP-RANK) TO PRN-VIDEO-TITLE
MOVE TOP-VIDEO-EARNINGS(SUPP-RANK) TO PRN-AVERAGE-EARNINGS
WRITE PRINT-LINE FROM SUPPLIER-LINE
AFTER ADVANCING 2 LINES
END-PERFORM.
FIND-TOP-SUPPLIERS.
MOVE VIDEO-SUPPLIER-CODE TO HOLD-SUPP-CODE
MOVE ZEROS TO HOLD-VIDEO-EARNINGS, HOLD-SUPP-EARNINGS
PERFORM SUM-SUPPLIER-EARNINGS
UNTIL VIDEO-SUPPLIER-CODE NOT EQUAL TO HOLD-SUPP-CODE
OR VIDEO-FILE-END
DISPLAY HOLD-SUPP-CODE " SUPPLIER EARNINGS = " HOLD-SUPP-EARNINGS
SET CONTINUE-CHECKING TO TRUE
PERFORM VARYING SUPP-RANK FROM 3 BY -1 UNTIL SUPP-RANK LESS THAN 1
OR STOP-CHECKING
IF HOLD-SUPP-EARNINGS GREATER THAN TOP-SUPP-EARNINGS(SUPP-RANK)
MOVE TOP-SUPPLIER(SUPP-RANK) TO
TOP-SUPPLIER(SUPP-RANK + 1)
MOVE HOLD-SUPP-REC TO TOP-SUPPLIER(SUPP-RANK)
ELSE
SET STOP-CHECKING TO TRUE
END-IF
END-PERFORM.
SUM-SUPPLIER-EARNINGS.
MOVE VIDEO-CODE TO VDF-VIDEO-CODE
READ VIDEO-DETAILS-FILE
KEY IS VDF-VIDEO-CODE
INVALID KEY DISPLAY "SUM-SUPP-EARNINGS FS = " VDF-STATUS
END-READ
SET NOT-VDF-END TO TRUE
MOVE ZEROS TO TOTAL-VIDEO-EARNINGS, VIDEO-COUNT
PERFORM SUM-TITLE-EARNINGS UNTIL
VDF-VIDEO-CODE NOT EQUAL TO VIDEO-CODE
OR VDF-FILE-END
DIVIDE TOTAL-VIDEO-EARNINGS BY VIDEO-COUNT
GIVING AVERAGE-VIDEO-EARNINGS ROUNDED
DISPLAY VIDEO-TITLE " TOTAL EARNINGS = " TOTAL-VIDEO-EARNINGS
DISPLAY VIDEO-TITLE " AVERAGE EARNINGS = " AVERAGE-VIDEO-EARNINGS
IF AVERAGE-VIDEO-EARNINGS GREATER THAN HOLD-VIDEO-EARNINGS
MOVE AVERAGE-VIDEO-EARNINGS TO HOLD-VIDEO-EARNINGS
MOVE VIDEO-TITLE TO HOLD-VIDEO-TITLE
MOVE VIDEO-CODE TO HOLD-VIDEO-CODE
END-IF
READ VIDEO-FILE NEXT RECORD
AT END SET VIDEO-FILE-END TO TRUE
END-READ.
SUM-TITLE-EARNINGS.
ADD 1 TO VIDEO-COUNT
ADD VDF-RENTAL-EARNINGS TO TOTAL-VIDEO-EARNINGS, HOLD-SUPP-EARNINGS.
READ VIDEO-DETAILS-FILE NEXT RECORD
AT END SET VDF-FILE-END TO TRUE
END-READ.