Writing Data With COBOL
2024-11-8 15:44:30 Author: hackernoon.com(查看原文) 阅读量:0 收藏

Hey devs! I am Delmi Pitta from Dominican Republic and I am trying to learn about COBOL and I have to say that this has been a beautiful journey. COBOL is easy to learn, maybe if you have information about better process than this please let me know.

This program shows how we can add indexed information to a file, ‘data.dat’. This is the file type I have used on this COBOL program and this was written on opeCobolIDE from gnuCobol.

    IDENTIFICATION DIVISION.
       PROGRAM-ID. Filing.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
*> THIS PART CONNECT US WITH FILES 'DATA.DAT'
        SELECT EMPLOYEES ASSIGN TO 'DATA.DAT'
            ORGANIZATION IS INDEXED
            ACCESS MODE IS DYNAMIC
            RECORD KEY IS FS-ID.

       DATA DIVISION.
       FILE SECTION.
*> THIS PART MODIFY THE 'DATA.DAT' ABOVE
       FD EMPLOYEES.
       01 EMPLOYEES-RECORD.
        05 FS-ID              PIC 9(11).
        05 FS-NAME            PIC X(10).
        05 FS-LAST-NAME       PIC X(10).
        05 FS-PHONE-NUM       PIC 9(11).
        05 FS-SALARY          PIC 9(4).
        05 FS-SOCIAL-NUMBER   PIC 9(11).
        05 FS-OCCUPATION      PIC X(30).
        05 FS-SCHEDULE        PIC X(30).
        05 FS-EMAIL           PIC X(30).

       WORKING-STORAGE SECTION.
       01 WS-IF-PROCEDURE         PIC X(1).
       01 WS-PERFORM-PROCEDURE     PIC X(1).
       01 WS-ERROR-PROCEDURE       PIC X(50).
       01 FILE-STATUS              PIC XX.

       PROCEDURE DIVISION.

       BEGIN.
       DISPLAY 'PLEASE CHOOSE AN OPTION BELOW: WRITE(W) READ(R)'.
       DISPLAY 'WHAT DO YOU WANT TO DO? DELETE(D) MODIFY(M)'.
       ACCEPT WS-IF-PROCEDURE.

       IF WS-IF-PROCEDURE = 'W' OR WS-IF-PROCEDURE = 'w' THEN
        PERFORM UNTIL WS-PERFORM-PROCEDURE = 'N'
        OPEN I-O EMPLOYEES
            DISPLAY 'WHAT IS THE EMPLOYEE ID NUMBER?'
            ACCEPT FS-ID
            *> VerifY IF THE ID ALREADY EXIST OPENING A READ TAG
               READ EMPLOYEES
                   INVALID KEY

            *> Prompt user for additional information if ID is available
            DISPLAY 'WHAT IS THE EMPLOYEE NAME?'
            ACCEPT FS-NAME
            DISPLAY 'WHAT IS THE EMPLOYEE LAST NAME?'
            ACCEPT FS-LAST-NAME
            DISPLAY 'WHAT IS THE EMPLOYEE PHONE NUMBER?'
            ACCEPT FS-PHONE-NUM
            DISPLAY 'WHAT IS THE EMPLOYEE EMAIL ADDRESS?'
            ACCEPT FS-EMAIL
            DISPLAY 'WHAT IS THE EMPLOYEE SALARY?'
            ACCEPT FS-SALARY
            DISPLAY 'WHAT IS THE EMPLOYEE SOCIAL NUMBER?'
            ACCEPT FS-SOCIAL-NUMBER
            DISPLAY 'WHAT IS THE EMPLOYEE OCCUPATION?'
            ACCEPT FS-OCCUPATION
            DISPLAY 'WHAT IS THE EMPLOYEE SCHEDULE?'
            ACCEPT FS-SCHEDULE

            *> Write the new record to the file
            
            WRITE EMPLOYEES-RECORD
      *>       PROTECT THE CURRENT INFORMATION FRON NEWEST
                INVALID KEY
        MOVE 'ERROR: RECORD COULD NOT BE WRITTEN' TO WS-ERROR-PROCEDURE
                    DISPLAY WS-ERROR-PROCEDURE
                NOT INVALID KEY
                    DISPLAY 'Record successfully added!'
             NOT INVALID KEY
      *>                  IF THE ID ALREADY EXIST WILL SHOW THIS
          MOVE 'ERROR: EMPLOYEE ID ALREADY EXISTS' TO WS-ERROR-PROCEDURE
                       DISPLAY WS-ERROR-PROCEDURE
      *>                  CLOSE THE READ PROCESS
           END-READ

      *>      TO REPEAT THE BUCLE
            DISPLAY 'WOULD YOU LIKE TO ADD ANOTHER RECORD? (Y/N)'
            ACCEPT WS-PERFORM-PROCEDURE
            
      *>      IT IS MANDATORY TO CLOSE THE FILE TO CONTINUE WITH PROGRAM
            CLOSE EMPLOYEES
        END-PERFORM
       END-IF.

       STOP RUN.

       END PROGRAM Filing.

If you execute this program on windows it will show you different question about employees you will be able to fill and save.

In the next program I will show you how to read that saved information on your console.

####Remember this program works on the console or terminal.

READ THE COMMENT ARE WITH ‘*>’.

PLEASE FOLLOW ME, RESPOND AND SHARE THIS.


文章来源: https://hackernoon.com/writing-data-with-cobol?source=rss
如有侵权请联系:admin#unsafe.sh