IDENTIFICATION DIVISION.
       PROGRAM-ID. CTRLBRK1.
       AUTHOR.     JIRO SUZUKI.
      *------------------------------------------------------------
      * コントロールブレイク サンプル
      * 購入日付・商品コードをキーにして金額を合計する。
      *------------------------------------------------------------
       ENVIRONMENT DIVISION.  
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
         SELECT  IN01  ASSIGN  TO  IN01NAME
                 ACCESS  MODE  IS  SEQUENTIAL.  
         SELECT  OUT1  ASSIGN  TO  OUT1NAME
                 ACCESS  MODE  IS  SEQUENTIAL.  
       DATA DIVISION.
       FILE SECTION.
       FD  IN01.
       COPY I-PRDCTDTL.
       FD  OUT1.
       COPY O-PRDCTTTL.
       WORKING-STORAGE SECTION.
       01 IN01NAME.
         03              PIC X(16) VALUE "/HOME/OWNER/DAT/".
         03 NAME         PIC X(12) VALUE "CTRLBRK1IN01".
         03              PIC X(04) VALUE ".DAT".
       01 OUT1NAME.
         03              PIC X(16) VALUE "/HOME/OWNER/DAT/".
         03 NAME         PIC X(12) VALUE "CTRLBRK1OUT1".
         03              PIC X(04) VALUE ".DAT".
       01 CNS-AREA.
         03 CNS-INIT-MSG. 
           05            PIC X(10) VALUE "----- PGM=".
           05            PIC X(08) VALUE "CTRLBRK1".
           05            PIC X(11) VALUE "START -----".
         03 CNS-TERM-MSG. 
           05            PIC X(10) VALUE "----- PGM=".
           05            PIC X(08) VALUE "CTRLBRK1".
           05            PIC X(11) VALUE "END   -----".
         03 CNS-SW-OFF   PIC 9(01) COMP-3 VALUE ZERO.
         03 CNS-SW-ON    PIC 9(01) COMP-3 VALUE 1.
       01 CNT-AREA.
         03 CNT-IN01     PIC 9(12) COMP-3 VALUE ZERO.
         03 CNT-OUT1     PIC 9(12) COMP-3 VALUE ZERO.
       01 SW-AREA.
         03 SW-ABEND     PIC 9(01) COMP-3 VALUE ZERO.
       01 WORK-AREA.
         03 W-KEY-OLD.
           05 W-OLD-PCHASEDATE     PIC X(08).
           05 W-OLD-PRDCTCD        PIC X(08).
         03 W-KEY-NEW.
           05 W-NEW-PCHASEDATE     PIC X(08).
           05 W-NEW-PRDCTCD        PIC X(08).
         03 W-QUANTITY-SUM         PIC 9(09) COMP-3.
         03 W-TTLAMOUNT            PIC 9(13) COMP-3.
      *------------------------------------------------------------
       PROCEDURE DIVISION.
       MAIN SECTION.            *>全体処理
         PERFORM INIT-PROC THRU INIT-EXIT.
         PERFORM MAIN-PROC THRU MAIN-EXIT.
         PERFORM TERM-PROC THRU TERM-EXIT.
         STOP RUN.
      *------------------------------------------------------------
       INIT-PROC.               *>初期処理
         DISPLAY CNS-INIT-MSG.
         OPEN INPUT  IN01
              OUTPUT OUT1.
         MOVE LOW-VALUE TO W-KEY-OLD W-KEY-NEW.
       INIT-EXIT.
      *------------------------------------------------------------
       MAIN-PROC.               *>主処理
         PERFORM READ-IN01.
         PERFORM UNTIL W-KEY-NEW = HIGH-VALUE
           EVALUATE TRUE 
             WHEN W-KEY-NEW = W-KEY-OLD
               PERFORM SUM-TOTAL-PROC
               PERFORM READ-IN01
             WHEN W-KEY-NEW > W-KEY-OLD
               PERFORM WRITE-OUT1
               PERFORM FIRST-KEY-PROC
               PERFORM READ-IN01
             WHEN OTHER
               PERFORM ABEND-PROC
           END-EVALUATE
         END-PERFORM.
       MAIN-EXIT.
      *------------------------------------------------------------
       TERM-PROC.               *>終了処理
         IF SW-ABEND  = CNS-SW-OFF
           IF CNT-IN01 NOT = 0
             PERFORM WRITE-OUT1
           END-IF
         END-IF.
      *
         CLOSE IN01
               OUT1.
         DISPLAY "IN01=" CNT-IN01.
         DISPLAY "OUT1=" CNT-OUT1.
         DISPLAY CNS-TERM-MSG.
       TERM-EXIT.
      *------------------------------------------------------------
       READ-IN01.               *>IN01読込処理
         READ IN01 INTO I-PRDCTDTL
           AT END
             MOVE HIGH-VALUE TO W-KEY-NEW
           NOT AT END
             MOVE I-PCHASEDATE TO W-NEW-PCHASEDATE
             MOVE I-PRDCTCD    TO W-NEW-PRDCTCD
             ADD 1 TO CNT-IN01
         END-READ.
      *------------------------------------------------------------
       WRITE-OUT1.              *>OUT1書込処理
         IF W-KEY-OLD NOT = LOW-VALUE
           INITIALIZE O-PRDCTTTL
           MOVE SPACE TO O-PRDCTTTL
      *
           MOVE W-OLD-PRDCTCD     TO O-PRDCTCD
           MOVE ZERO              TO O-PRICE
           MOVE W-QUANTITY-SUM    TO O-QUANTITY
           MOVE W-OLD-PCHASEDATE  TO O-PCHASEDATE
           MOVE W-TTLAMOUNT       TO O-TTLAMOUNT
           WRITE O-PRDCTTTL 
           ADD 1 TO CNT-OUT1
         END-IF.        
      *------------------------------------------------------------
       FIRST-KEY-PROC.          *>同一キー1件目処理
      *
         MOVE W-KEY-NEW TO W-KEY-OLD.
         MOVE ZERO TO W-QUANTITY-SUM W-TTLAMOUNT.
      *
         MOVE I-QUANTITY TO W-QUANTITY-SUM.
         COMPUTE W-TTLAMOUNT = I-PRICE * I-QUANTITY.
      *------------------------------------------------------------
       SUM-TOTAL-PROC.          *>金額合計処理
      *
         COMPUTE W-QUANTITY-SUM = W-QUANTITY-SUM + I-QUANTITY.
         COMPUTE W-TTLAMOUNT = W-TTLAMOUNT + ( I-PRICE * I-QUANTITY ).
      *------------------------------------------------------------
       ABEND-PROC.              *>異常発生時処理
         DISPLAY "----- IN01 DATA NOT SORTED -----". 
         DISPLAY "      THIS PROCEDURE ENDED ABNORMALY." .
         MOVE CNS-SW-ON TO SW-ABEND.