       IDENTIFICATION DIVISION.
       program-id. Program3 as "Program3".
       AUTHOR. WYATT WANG DONGLIN LI
       DATE-WRITTEN.  7TH DEC 2020

       environment division.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT INDEXED-STUDENT-FILE-IN
           ASSIGN TO "C:\Users\Administrator\Downloads\STUFILE4.TXT"
               ORGANIZATION IS INDEXED
               ACCESS MODE IS SEQUENTIAL
               RECORD KEY IS IND-STUDENT-NUMBER
               ALTERNATE RECORD KEY IS IND-TUITION-OWED WITH            
               DUPLICATES.
       SELECT PROGRAM-FILE-IN
           ASSIGN TO "C:\Users\Administrator\Downloads\PROGRAM.txt"
               ORGANIZATION IS LINE SEQUENTIAL.
       SELECT STUDENT-REPORT-OUT
           ASSIGN TO 
           "C:\Users\Administrator\Downloads\STUDENT-REPORT.TXT"
           ORGANIZATION IS LINE SEQUENTIAL.
        
       configuration section.
       
       data division.
       FILE SECTION.
       *>STUDENT-FILE IS AN INDEXED INPUT FILE
       FD INDEXED-STUDENT-FILE-IN.
       01 INDEXED-FILE-RECORD.
           05 IND-STUDENT-NUMBER  PIC 9(6).
           05 IND-TUITION-OWED    PIC 9(4)V99.
           05 IND-STUDENT-NAME    PIC X(40).
           05 IND-PROGRAM-OF-STUDY    PIC X(5).
           05 IND-COURSE-CODE-1   PIC X(7).
           05 IND-AVERAGE-1       PIC 9(3).
           05 IND-COURSE-CODE-2   PIC X(7).
           05 IND-AVERAGE-2       PIC 9(3).
           05 IND-COURSE-CODE-3   PIC X(7).
           05 IND-AVERAGE-3       PIC 9(3).
           05 IND-COURSE-CODE-4   PIC X(7).
           05 IND-AVERAGE-4       PIC 9(3).
           05 IND-COURSE-CODE-5   PIC X(7).
           05 IND-AVERAGE-5       PIC 9(3).
       *>PROGRAM-FILE-IN IS AN INPUT FILE
       FD PROGRAM-FILE-IN.
       01 PROGRAM-RECORD.
         05 PORGRAM-CODE-IN PIC X(5).
         05 PROGRAM-NAME-IN PIC X(20).
       *>STUDENT-REPORT-OUT IS A OUTPUT FILR
       FD STUDENT-REPORT-OUT.
       01 STUDENT-REPORT-RECORD-OUT PIC X(81).
       
       working-storage section.
       *>LOCAL FEILD RESERVED FOR STUDENT-REPORT CONTENTS, FILLER, COLUMHEADER AND FLAG
       01 STATUS-FIELD PIC X(2).
       01 STUDENT-REPORT-RECORD.
         05 STUDENT-NAME-OUT PIC X(40).
         05 FILLER PIC X(2) VALUE SPACES.
         05 STUDENT-AVERAGE-OUT PIC 9(3).
         05 FILLER PIC X(4) VALUE SPACES.
         05 PROGRAM-NAME-OUT PIC X(20).
         05 FILLER PIC X(4) VALUE SPACES.
         05 TUITION-OWED-OUT PIC Z,ZZ9.99.
       01 COLUMN-HEADER.
         05 FILLER PIC X(12) VALUE "STUDENT NAME".
         05 FILLER PIC X(19) VALUE SPACES.
         05 FILLER PIC X(7) VALUE "AVERAGE".
         05 FILLER PIC X(1) VALUE SPACES.
         05 FILLER PIC X(12) VALUE "PROGRAM NAME".
         05 FILLER PIC X(8) VALUE SPACES.
         05 FILLER PIC X(12) VALUE "TUITION OWED".
       01 FLAG-COUNTERS.
         05 EOF-FLAG PIC X(3) VALUE "NO".
         05 EOF-FLAG-TBL PIC X(3) VALUE "NO".
         05 READ-COUNTER PIC 9(3) VALUE ZERO.
         05 WRITE-COUNTER PIC 9(3) VALUE ZERO.
         05 SUB PIC 9(2).
         05 FOUND-FLAG PIC X(3) VALUE "NO".
       01 PROGRAM-NAMES-RECORDS.
         05 PROGRAM-TBL-RECORD OCCURS 20 TIMES.
           10 PROGRAM-CODE-TBL PIC X(5).
           10 PROGRAM-NAME-TBL PIC X(20).
       

       procedure division.
       *>100 ROUTINE
       100-PRODUCE-STUDENT-REPORTS.
           PERFORM 201-INIT-STUDENT-REPORTS.
           PERFORM 202-PRODUCE-ONE-STUDENT-REPORT
               UNTIL EOF-FLAG = "YES".
           PERFORM 203-TERMINATE-STUDENT-REPORT.
           STOP RUN.
       *>200 ROUTINES
       *>INITIAL STUDNET-REPORTS FILR, LOAD THE PROGRAM TABLE, READ AND WRITE CONTENT INTO FILE
       201-INIT-STUDENT-REPORTS.
           PERFORM 301-OPEN-FILES.
           PERFORM 302-LOAD-PROGRAM-TBL
               VARYING SUB FROM 1 BY 1
                UNTIL EOF-FLAG-TBL = "YES"
                OR SUB > 20.
           PERFORM 305-READ-STUDENT-RECORD.
           PERFORM 304-WRITE-COLUMN-HDR.
       *>PRODUCE PARTICULAR STUDENT REPORT BY CALCULATE AVERAGE AND FETCHING PROGRAM NAME, WRITE CONTENT INTO OUTPUT FILE
       202-PRODUCE-ONE-STUDENT-REPORT.
           MOVE "NO" TO FOUND-FLAG.
           PERFORM 306-CALCULATE-AVERAGE.
           PERFORM 307-SEARCH-PROGRAM-NAME
               VARYING SUB FROM 1 BY 1
                UNTIL SUB > 20 OR FOUND-FLAG = "YES".
           PERFORM 308-WRITE-STUDENT-REPORT.
           PERFORM 305-READ-STUDENT-RECORD.
       *>TERMINATE PROGRAM
       203-TERMINATE-STUDENT-REPORT.
           PERFORM 309-CLOSE-FILES.
           PERFORM 310-DISPLAY-COUNTERS.
       *>300 ROUTINES
       *>INITAL I/O FILE AND PROMPT
       301-OPEN-FILES.
           OPEN INPUT INDEXED-STUDENT-FILE-IN
                      PROGRAM-FILE-IN.
           OPEN OUTPUT STUDENT-REPORT-OUT.
           DISPLAY "OPEN SUCCESS!".
       *>READ PROGRAM-FILE-IN INTO LOAD-PROGRAM-TBL TABLE  
       302-LOAD-PROGRAM-TBL.
           READ PROGRAM-FILE-IN
               AT END MOVE "YES" TO EOF-FLAG-TBL
                      NOT AT END MOVE PROGRAM-RECORD
                                   TO PROGRAM-TBL-RECORD(SUB).
       *>WRITE COLUM HEADER
       304-WRITE-COLUMN-HDR.
           WRITE STUDENT-REPORT-RECORD-OUT FROM COLUMN-HEADER.
       *>READ & DISPLAY INDEXED-STUDENT-FILE-IN CONTENT
       305-READ-STUDENT-RECORD.
           READ INDEXED-STUDENT-FILE-IN
               AT END MOVE "YES" TO EOF-FLAG
                      NOT AT END ADD 1 TO READ-COUNTER
                           DISPLAY IND-STUDENT-NUMBER 
                           IND-PROGRAM-OF-STUDY.
       *>CALCULATE STUDENT GRADE AVERAGE
       306-CALCULATE-AVERAGE.
           COMPUTE STUDENT-AVERAGE-OUT
             = (IND-AVERAGE-1 + IND-AVERAGE-2 + IND-AVERAGE-3 + 
             IND-AVERAGE-4 + IND-AVERAGE-5) / 5.
       *>FECTH PRORAM NAME
       307-SEARCH-PROGRAM-NAME.
           
           IF IND-PROGRAM-OF-STUDY = PROGRAM-CODE-TBL(SUB)
               MOVE "YES" TO FOUND-FLAG
               MOVE PROGRAM-NAME-TBL(SUB) TO PROGRAM-NAME-OUT
           ELSE
               MOVE "NO" TO FOUND-FLAG
           END-IF.
       *>WRITE STUDENT-REPORT AND INCREASE COUNTER
       308-WRITE-STUDENT-REPORT.
           MOVE IND-STUDENT-NAME TO STUDENT-NAME-OUT.
           MOVE IND-TUITION-OWED TO TUITION-OWED-OUT.
           WRITE STUDENT-REPORT-RECORD-OUT FROM STUDENT-REPORT-RECORD.
           ADD 1 TO WRITE-COUNTER.
       *>CLOSE I/P FILES
       309-CLOSE-FILES.
           CLOSE INDEXED-STUDENT-FILE-IN
                 PROGRAM-FILE-IN
                 STUDENT-REPORT-OUT.
       *>DISPLAY COUNTER VALUE FOR I/O
       310-DISPLAY-COUNTERS.
           DISPLAY READ-COUNTER WRITE-COUNTER.

       end program Program3.
