Teach Yourself COBOL in 21 days, Second Edition

Previous chapterNext chapterContents


- Day 13 -
Deleting Records and Other Indexed File Operations

Master a few more types of access to an indexed file and you have the basics of keeping indexed files updated. Today, you learn about the following topics:

A New COBOL Shell

All the programs that you have worked with have used files. It is time for a new COBOL shell program that makes room for physical and logical file descriptions. You can use this shell or template as the basic starting point for any programs that use files, and it saves a lot of coding. Just copy it to the new COBOL program name and start editing.

Code Listing 13.1 and keep it handy. You'll use this shell for several more days.

TYPE: Listing 13.1. A shell with space for files and comments.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COBSHL03.
000300*------------------------------------------------
000400* Comments go here.
000500*------------------------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000 DATA DIVISION.
001100 FILE SECTION.
001200
001300 WORKING-STORAGE SECTION.
001400
001500 PROCEDURE DIVISION.
001600 PROGRAM-BEGIN.
001700
001800 PROGRAM-DONE.
001900     STOP RUN.
002000

Deleting Records in an Indexed File

As time passes, you will need to delete records from a file. You might delete vendors with whom you no longer do business. A bills file might retire records for paid bills from the active file by deleting the records from the main file and writing them to a history file.

The command to delete a record is DELETE file-name, and it uses INVALID KEY to trap file-error conditions. Here is the syntax:

DELETE file-name RECORD
INVALID KEY
do something

The following is an example of DELETE file-name:

DELETE VENDOR-FILE RECORD
INVALID KEY
DISPLAY "ERROR DURING DELETE"

You must read a record successfully before you can delete it. As with REWRITE, some large systems require that the record is read WITH LOCK or WITH HOLD before the DELETE command will succeed.

Listing 13.2 is similar in organization to vndchg01.cbl. The problem with deleting a record is similar to the problem with changing a record. Get the user to identify the record and then read it and delete it.

TYPE: Listing 13.2. Deleting vendor records.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VNDDEL01.
000300*--------------------------------
000400* Delete records in Vendor File.
000500*--------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000     COPY "SLVND01.CBL".
001100
001200 DATA DIVISION.
001300 FILE SECTION.
001400
001500     COPY "FDVND02.CBL".
001600
001700 WORKING-STORAGE SECTION.
001800
001900 77  OK-TO-DELETE        PIC X.
002000 77  RECORD-FOUND        PIC X.
002100
002200 77  VENDOR-NUMBER-FIELD PIC Z(5).
002300
002400 PROCEDURE DIVISION.
002500 PROGRAM-BEGIN.
002600     OPEN I-O VENDOR-FILE.
002700     PERFORM GET-VENDOR-RECORD.
002800     PERFORM DELETE-RECORDS
002900        UNTIL VENDOR-NUMBER = ZEROES.
003000     CLOSE VENDOR-FILE.
003100
003200 PROGRAM-DONE.
003300     STOP RUN.
003400
003500*--------------------------------
003600* TO GET A VENDOR RECORD, ASK FOR
003700* VENDOR NUMBER, AND THEN TRY TO
003800* READ THE RECORD.
003900*--------------------------------
004000 GET-VENDOR-RECORD.
004100     PERFORM INIT-VENDOR-RECORD.
004200     PERFORM ENTER-VENDOR-NUMBER.
004300     MOVE "N" TO RECORD-FOUND.
004400     PERFORM FIND-VENDOR-RECORD
004500         UNTIL RECORD-FOUND = "Y" OR
004600               VENDOR-NUMBER = ZEROES.
004700
004800 INIT-VENDOR-RECORD.
004900     MOVE SPACE TO VENDOR-RECORD.
005000     MOVE ZEROES TO VENDOR-NUMBER.
005100
005200 ENTER-VENDOR-NUMBER.
005300     DISPLAY " ".
005400     DISPLAY "ENTER VENDOR NUMBER OF THE VENDOR" .
005500     DISPLAY "TO DELETE (1-99999)".
005600     DISPLAY "ENTER 0 TO STOP ENTRY".
005700     ACCEPT VENDOR-NUMBER-FIELD.
005800*OR  ACCEPT VENDOR-NUMBER-FIELD WITH CONVERSION.
005900
006000     MOVE VENDOR-NUMBER-FIELD TO VENDOR-NUMBER.
006100
006200 FIND-VENDOR-RECORD.
006300     PERFORM READ-VENDOR-RECORD.
006400     IF RECORD-FOUND = "N"
006500         DISPLAY "RECORD NOT FOUND"
006600         PERFORM ENTER-VENDOR-NUMBER.
006700
006800 READ-VENDOR-RECORD.
006900     MOVE "Y" TO RECORD-FOUND.
007000     READ VENDOR-FILE RECORD
007100       INVALID KEY
007200          MOVE "N" TO RECORD-FOUND.
007300
007400*or  READ VENDOR-FILE RECORD WITH LOCK
007500*      INVALID KEY
007600*         MOVE "N" TO RECORD-FOUND.
007700
007800*or  READ VENDOR-FILE RECORD WITH HOLD
007900*      INVALID KEY
008000*         MOVE "N" TO RECORD-FOUND.
008100
008200 DELETE-RECORDS.
008300     PERFORM DISPLAY-ALL-FIELDS.
008400     MOVE "X" TO OK-TO-DELETE.
008500
008600     PERFORM ASK-TO-DELETE
008700        UNTIL OK-TO-DELETE = "Y" OR "N".
008800
008900     IF OK-TO-DELETE = "Y"
009000         PERFORM DELETE-VENDOR-RECORD.
009100
009200     PERFORM GET-VENDOR-RECORD.
009300
009400*--------------------------------
009500* DISPLAY ALL FIELDS WITH BLANK
009600* LINES ABOVE AND BELOW.
009700*--------------------------------
009800 DISPLAY-ALL-FIELDS.
009900     DISPLAY " ".
010000     PERFORM DISPLAY-VENDOR-NUMBER.
010100     PERFORM DISPLAY-VENDOR-NAME.
010200     PERFORM DISPLAY-VENDOR-ADDRESS-1.
010300     PERFORM DISPLAY-VENDOR-ADDRESS-2.
010400     PERFORM DISPLAY-VENDOR-CITY.
010500     PERFORM DISPLAY-VENDOR-STATE.
010600     PERFORM DISPLAY-VENDOR-ZIP.
010700     PERFORM DISPLAY-VENDOR-CONTACT.
010800     PERFORM DISPLAY-VENDOR-PHONE.
010900     DISPLAY " ".
011000
011100 DISPLAY-VENDOR-NUMBER.
011200     DISPLAY "   VENDOR NUMBER: " VENDOR-NUMBER.
011300
011400 DISPLAY-VENDOR-NAME.
011500     DISPLAY "1. VENDOR NAME: " VENDOR-NAME.
011600
011700 DISPLAY-VENDOR-ADDRESS-1.
011800     DISPLAY "2. VENDOR ADDRESS-1: " VENDOR-ADDRESS-1.
011900
012000 DISPLAY-VENDOR-ADDRESS-2.
012100     DISPLAY "3. VENDOR ADDRESS-2: " VENDOR-ADDRESS-2.
012200
012300 DISPLAY-VENDOR-CITY.
012400     DISPLAY "4. VENDOR CITY: " VENDOR-CITY.
012500
012600 DISPLAY-VENDOR-STATE.
012700     DISPLAY "5. VENDOR STATE: " VENDOR-STATE.
012800
012900 DISPLAY-VENDOR-ZIP.
013000     DISPLAY "6. VENDOR ZIP: " VENDOR-ZIP.
013100
013200 DISPLAY-VENDOR-CONTACT.
013300     DISPLAY "7. VENDOR CONTACT: " VENDOR-CONTACT.
013400
013500 DISPLAY-VENDOR-PHONE.
013600     DISPLAY "8. VENDOR PHONE: " VENDOR-PHONE.
013700
013800 ASK-TO-DELETE.
013900     DISPLAY "DELETE THIS RECORD (Y/N)?".
014000     ACCEPT OK-TO-DELETE.
014100     IF OK-TO-DELETE= "y"
014200         MOVE "Y" TO OK-TO-DELETE.
014300     IF OK-TO-DELETE= "n"
014400         MOVE "N" TO OK-TO-DELETE.
014500     IF OK-TO-DELETE NOT = "Y" AND
014600        OK-TO-DELETE NOT = "N"
014700         DISPLAY "YOU MUST ENTER YES OR NO".
014800
014900 DELETE-VENDOR-RECORD.
015000     DELETE VENDOR-FILE RECORD
015100         INVALID KEY
015200         DISPLAY "ERROR DELETING VENDOR RECORD".
015300

OUTPUT:

ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
VENDOR NUMBER: 00001
1. VENDOR NAME: JOHNSON AND SON
2. VENDOR ADDRESS-1: 1212 MAIN ST
3. VENDOR ADDRESS-2: # 455
4. VENDOR CITY: LOS ANGELES
5. VENDOR STATE: CA
6. VENDOR ZIP: 91042
7. VENDOR CONTACT: LINDA MARTIN
8. VENDOR PHONE: (213) 555-1234
DELETE THIS RECORD (Y/N)?
N
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
VENDOR NUMBER: 00001
1. VENDOR NAME: JOHNSON AND SON
2. VENDOR ADDRESS-1: 1212 MAIN ST
3. VENDOR ADDRESS-2: # 455
4. VENDOR CITY: LOS ANGELES
5. VENDOR STATE: CA
6. VENDOR ZIP: 91042
7. VENDOR CONTACT: LINDA MARTIN
8. VENDOR PHONE: (213) 555-1234
DELETE THIS RECORD (Y/N)?
Y
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
RECORD NOT FOUND
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY

ANALYSIS: The primary difference between vnddel01.cbl and vndchg01.cbl is in DELETE-RECORDS at line 008200. At this point in vndchg01.cbl, the user is asked which field to change and data entry is accepted for the changed field. In vnddel01.cbl, instead of asking the user for a field, the program asks the user whether this record should be deleted and then deletes the record if the user answers yes.

This gives the user a chance to see the record to make sure that it is the one to be deleted, rather than just deleting it.

One piece of coding you have not seen before appears at line 008700 as a test: UNTIL OK-TO-DELETE = "Y" OR "N". This is equivalent to OK-TO-DELETE = "Y" OR OK-TO-DELETE ="N".


DO/DON'T:
DO
give the user a chance to back out, in case the wrong record was chosen for deletion.

DON'T code programs that let the user find out too late that an irrevocable decision has been made.


Code, compile, and test this program carefully. It is important in a deletion program to ensure that the record is deleted only when the user answers yes. It can be disconcerting, to say the least, to have records disappear for no apparent reason.


DO/DON'T:
DO
test deletion programs carefully, and ensure that they delete records only when the user explicitly gives permission for a delete.

DON'T let programs carry out unexpected deletions that destroy the integrity of the data in a file.


To test this program, run it and specify a vendor number. When the record is displayed, enter N for no to the OK TO DELETE question. Then request that record number again. If the record has been deleted accidentally, you will receive a RECORD NOT FOUND message.

Enter a vendor number and specify Y for yes to the OK TO DELETE question. Then request that record number again. This time you should receive the RECORD NOT FOUND message.

The output of vnddel01.cbl illustrates this series of tests.

Displaying Records in an Indexed File

The inquiry program, vndinq01.cbl (shown in Listing 13.3), is almost identical to vnddel01.cbl. The step of asking the user whether to delete the record is dropped completely out of the logic, and the user is prompted for another vendor number.

New Term: Displaying a single record in an indexed file usually is called an inquiry. The user asks for the data on a specific vendor, and the information is displayed on the screen for reference. The program does not allow any changes and is used only for lookup actions.

TYPE: Listing 13.3. Looking up records in a file.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VNDINQ01.
000300*--------------------------------
000400* Look Up records in Vendor File.
000500*--------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000     COPY "SLVND01.CBL".
001100
001200 DATA DIVISION.
001300 FILE SECTION.
001400
001500     COPY "FDVND02.CBL".
001600
001700 WORKING-STORAGE SECTION.
001800
001900 77  RECORD-FOUND        PIC X.
002000
002100 77  VENDOR-NUMBER-FIELD PIC Z(5).
002200
002300 PROCEDURE DIVISION.
002400 PROGRAM-BEGIN.
002500     OPEN I-O VENDOR-FILE.
002600     PERFORM GET-VENDOR-RECORD.
002700     PERFORM INQUIRE-RECORDS
002800        UNTIL VENDOR-NUMBER = ZEROES.
002900     CLOSE VENDOR-FILE.
003000
003100 PROGRAM-DONE.
003200     STOP RUN.
003300
003400*--------------------------------
003500* TO GET A VENDOR RECORD, ASK FOR
003600* VENDOR NUMBER, AND THEN TRY TO
003700* READ THE RECORD.
003800*--------------------------------
003900 GET-VENDOR-RECORD.
004000     PERFORM INIT-VENDOR-RECORD.
004100     PERFORM ENTER-VENDOR-NUMBER.
004200     MOVE "N" TO RECORD-FOUND.
004300     PERFORM FIND-VENDOR-RECORD
004400         UNTIL RECORD-FOUND = "Y" OR
004500               VENDOR-NUMBER = ZEROES.
004600
004700 INIT-VENDOR-RECORD.
004800     MOVE SPACE TO VENDOR-RECORD.
004900     MOVE ZEROES TO VENDOR-NUMBER.
005000
005100 ENTER-VENDOR-NUMBER.
005200     DISPLAY " ".
005300     DISPLAY "ENTER VENDOR NUMBER OF THE VENDOR" .
005400     DISPLAY "TO DISPLAY (1-99999)".
005500     DISPLAY "ENTER 0 TO STOP ENTRY".
005600     ACCEPT VENDOR-NUMBER-FIELD.
005700*OR  ACCEPT VENDOR-NUMBER-FIELD WITH CONVERSION.
005800
005900     MOVE VENDOR-NUMBER-FIELD TO VENDOR-NUMBER.
006000
006100 FIND-VENDOR-RECORD.
006200     PERFORM READ-VENDOR-RECORD.
006300     IF RECORD-FOUND = "N"
006400         DISPLAY "RECORD NOT FOUND"
006500         PERFORM ENTER-VENDOR-NUMBER.
006600
006700 READ-VENDOR-RECORD.
006800     MOVE "Y" TO RECORD-FOUND.
006900     READ VENDOR-FILE RECORD
007000       INVALID KEY
007100          MOVE "N" TO RECORD-FOUND.
007200
007300*or  READ VENDOR-FILE RECORD WITH LOCK
007400*      INVALID KEY
007500*         MOVE "N" TO RECORD-FOUND.
007600
007700*or  READ VENDOR-FILE RECORD WITH HOLD
007800*      INVALID KEY
007900*         MOVE "N" TO RECORD-FOUND.
008000
008100 INQUIRE-RECORDS.
008200     PERFORM DISPLAY-ALL-FIELDS.
008300
008400     PERFORM GET-VENDOR-RECORD.
008500
008600*--------------------------------
008700* DISPLAY ALL FIELDS WITH BLANK
008800* LINES ABOVE AND BELOW.
008900*--------------------------------
009000 DISPLAY-ALL-FIELDS.
009100     DISPLAY " ".
009200     PERFORM DISPLAY-VENDOR-NUMBER.
009300     PERFORM DISPLAY-VENDOR-NAME.
009400     PERFORM DISPLAY-VENDOR-ADDRESS-1.
009500     PERFORM DISPLAY-VENDOR-ADDRESS-2.
009600     PERFORM DISPLAY-VENDOR-CITY.
009700     PERFORM DISPLAY-VENDOR-STATE.
009800     PERFORM DISPLAY-VENDOR-ZIP.
009900     PERFORM DISPLAY-VENDOR-CONTACT.
010000     PERFORM DISPLAY-VENDOR-PHONE.
010100     DISPLAY " ".
010200
010300 DISPLAY-VENDOR-NUMBER.
010400     DISPLAY "   VENDOR NUMBER: " VENDOR-NUMBER.
010500
010600 DISPLAY-VENDOR-NAME.
010700     DISPLAY "1. VENDOR NAME: " VENDOR-NAME.
010800
010900 DISPLAY-VENDOR-ADDRESS-1.
011000     DISPLAY "2. VENDOR ADDRESS-1: " VENDOR-ADDRESS-1.
011100
011200 DISPLAY-VENDOR-ADDRESS-2.
011300     DISPLAY "3. VENDOR ADDRESS-2: " VENDOR-ADDRESS-2.
011400
011500 DISPLAY-VENDOR-CITY.
011600     DISPLAY "4. VENDOR CITY: " VENDOR-CITY.
011700
011800 DISPLAY-VENDOR-STATE.
011900     DISPLAY "5. VENDOR STATE: " VENDOR-STATE.
012000
012100 DISPLAY-VENDOR-ZIP.
012200     DISPLAY "6. VENDOR ZIP: " VENDOR-ZIP.
012300
012400 DISPLAY-VENDOR-CONTACT.
012500     DISPLAY "7. VENDOR CONTACT: " VENDOR-CONTACT.
012600
012700 DISPLAY-VENDOR-PHONE.
012800     DISPLAY "8. VENDOR PHONE: " VENDOR-PHONE.
012900

The output of vndinq01.cbl follows. I asked for a display of vendor number 00001, but I had deleted it in the previous example while testing vnddel01.cbl. The result was a RECORD NOT FOUND message. Then I requested a display of vendor 00002.

OUTPUT:

ENTER VENDOR NUMBER OF THE VENDOR
TO DISPLAY (1-99999)
ENTER 0 TO STOP ENTRY
1
RECORD NOT FOUND
ENTER VENDOR NUMBER OF THE VENDOR
TO DISPLAY (1-99999)
ENTER 00000 TO STOP ENTRY
2
VENDOR NUMBER: 00002
1. VENDOR NAME: ABC PRINTING
2. VENDOR ADDRESS-1: 1624 FOOTHILL BLVD
3. VENDOR ADDRESS-2: SUITE 34
4. VENDOR CITY: LOS ANGELES
5. VENDOR STATE: CA
6. VENDOR ZIP: 91042
7. VENDOR CONTACT: CHARLES JOHANSSEN
8. VENDOR PHONE: (818) 555-4321
ENTER VENDOR NUMBER OF THE VENDOR
TO DISPLAY (1-99999)
ENTER 0 TO STOP ENTRY
0
C>
C>

Notice that the delete program, vnddel01.cbl, is very much like the change program, vndchg01.cbl, only shorter. The inquiry program, vndinq01.cbl, is very much like the delete program but is shorter still.

This is a common relationship. The change program displays all fields and allows changes to individual fields, the delete program displays all fields and allows the whole record to be deleted, and the inquiry program displays all fields and allows no changes.

New Term: The four types of programs covered in Day 11, "Indexed File I/O," Day 12, "More on Indexed Files," and today usually are called maintenance programs. Programs that allow records to be added, changed, deleted, and looked up in an indexed (or other) file make it possible to maintain the file.

New Term: The process of adding, changing, deleting or looking up a record in a data file by entering the data at a terminal is called transaction processing. It is also sometimes called online processing or real-time processing.

A Better Way of Adding Records

When you wrote the first versions of vndnew01.cbl through vndnew03.cbl, you hadn't yet used the READ command to look up a record.

One of the failings of vndnew01, vndnew02, and vndnew03 is that these programs ask the user to enter all the fields for the vendor before letting the user know that a record with that vendor number exists. When a record with that number exists, the user has to select a new number and start entering all over again.

It would be much better to check as soon as possible and let the user correct the VENDOR-NUMBER before going on. Listing 13.4, vndadd01.cbl, uses this approach. The program name now is more in keeping with the four types of maintenance programs: add, change, delete, and inquire (or look up).

TYPE: Listing 13.4. A better way of adding records by checking sooner if the vendor is on file.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VNDADD01.
000300*------------------------------------------------
000400* Add a record to an indexed Vendor File.
000500*------------------------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000     COPY "SLVND01.CBL".
001100
001200 DATA DIVISION.
001300 FILE SECTION.
001400
001500     COPY "FDVND02.CBL".
001600
001700 WORKING-STORAGE SECTION.
001800
001900 77  RECORD-FOUND                         PIC X.
002000 77  VENDOR-NUMBER-FIELD                  PIC Z(5).
002100
002200 PROCEDURE DIVISION.
002300 PROGRAM-BEGIN.
002400     OPEN I-O VENDOR-FILE.
002500     PERFORM GET-NEW-VENDOR-NUMBER.
002600     PERFORM ADD-RECORDS
002700        UNTIL VENDOR-NUMBER = ZEROES.
002800     CLOSE VENDOR-FILE.
002900
003000 PROGRAM-DONE.
003100     STOP RUN.
003200
003300 GET-NEW-VENDOR-NUMBER.
003400     PERFORM INIT-VENDOR-RECORD.
003500     PERFORM ENTER-VENDOR-NUMBER.
003600     MOVE "Y" TO RECORD-FOUND.
003700     PERFORM FIND-NEW-VENDOR-RECORD
003800         UNTIL RECORD-FOUND = "N" OR
003900               VENDOR-NUMBER = ZEROES.
004000
004100 INIT-VENDOR-RECORD.
004200     MOVE SPACE TO VENDOR-RECORD.
004300     MOVE ZEROES TO VENDOR-NUMBER.
004400
004500 ENTER-VENDOR-NUMBER.
004600     DISPLAY " ".
004700     DISPLAY "ENTER VENDOR NUMBER OF THE VENDOR" .
004800     DISPLAY "TO ADD (1-99999)".
004900     DISPLAY "ENTER 0 TO STOP ENTRY".
005000     ACCEPT VENDOR-NUMBER-FIELD.
005100*OR  ACCEPT VENDOR-NUMBER-FIELD WITH CONVERSION.
005200
005300     MOVE VENDOR-NUMBER-FIELD TO VENDOR-NUMBER.
005400
005500 FIND-NEW-VENDOR-RECORD.
005600     PERFORM READ-VENDOR-RECORD.
005700     IF RECORD-FOUND = "Y"
005800         DISPLAY "RECORD ALREADY ON FILE"
005900         PERFORM ENTER-VENDOR-NUMBER.
006000
006100 READ-VENDOR-RECORD.
006200     MOVE "Y" TO RECORD-FOUND.
006300     READ VENDOR-FILE RECORD
006400       INVALID KEY
006500          MOVE "N" TO RECORD-FOUND.
006600
006700
006800 ADD-RECORDS.
006900     PERFORM ENTER-REMAINING-FIELDS.
007000     PERFORM WRITE-VENDOR-RECORD.
007100     PERFORM GET-NEW-VENDOR-NUMBER.
007200
007300 WRITE-VENDOR-RECORD.
007400     WRITE VENDOR-RECORD
007500         INVALID KEY
007600         DISPLAY "RECORD ALREADY ON FILE".
007700
007800 ENTER-REMAINING-FIELDS.
007900     PERFORM ENTER-VENDOR-NAME.
008000     PERFORM ENTER-VENDOR-ADDRESS-1.
008100     PERFORM ENTER-VENDOR-ADDRESS-2.
008200     PERFORM ENTER-VENDOR-CITY.
008300     PERFORM ENTER-VENDOR-STATE.
008400     PERFORM ENTER-VENDOR-ZIP.
008500     PERFORM ENTER-VENDOR-CONTACT.
008600     PERFORM ENTER-VENDOR-PHONE.
008700
008800 ENTER-VENDOR-NAME.
008900     DISPLAY "ENTER VENDOR NAME".
009000     ACCEPT VENDOR-NAME.
009100
009200 ENTER-VENDOR-ADDRESS-1.
009300     DISPLAY "ENTER VENDOR ADDRESS-1".
009400     ACCEPT VENDOR-ADDRESS-1.
009500
009600 ENTER-VENDOR-ADDRESS-2.
009700     DISPLAY "ENTER VENDOR ADDRESS-2".
009800     ACCEPT VENDOR-ADDRESS-2.
009900
010000 ENTER-VENDOR-CITY.
010100     DISPLAY "ENTER VENDOR CITY".
010200     ACCEPT VENDOR-CITY.
010300
010400 ENTER-VENDOR-STATE.
010500     DISPLAY "ENTER VENDOR STATE".
010600     ACCEPT VENDOR-STATE.
010700
010800 ENTER-VENDOR-ZIP.
010900     DISPLAY "ENTER VENDOR ZIP".
011000     ACCEPT VENDOR-ZIP.
011100
011200 ENTER-VENDOR-CONTACT.
011300     DISPLAY "ENTER VENDOR CONTACT".
011400     ACCEPT VENDOR-CONTACT.
011500
011600 ENTER-VENDOR-PHONE.
011700     DISPLAY "ENTER VENDOR PHONE".
011800     ACCEPT VENDOR-PHONE.
011900

The following is the output:

OUTPUT:

ENTER VENDOR NUMBER OF THE
VENDOR TO ADD (1-99999)
ENTER 0 TO STOP ENTRY
00002
RECORD ALREADY ON FILE
ENTER VENDOR NUMBER OF THE
VENDOR TO ADD (1-99999)
ENTER 0 TO STOP ENTRY

ANALYSIS: At line 003300, the GET-NEW-VENDOR-NUMBER routine has been modified to include testing for an existing record. The FIND-NEW-VENDOR-RECORD logic at lines 005500 through 005900 is the reverse of the FIND-VENDOR-RECORD logic used in vndchg01.cbl, vnddel01.cbl, and vndinq01.cbl, and it produces an error message when a record is found, rather than when a record is not found. Compare GET-NEW-VENDOR-NUMBER and GET-NEW-VENDOR-RECORD in vndadd01.cbl with GET-VENDOR-RECORD and FIND-VENDOR-RECORD in vnddel01.cbl or vndinq01.cbl.

Code, compile, and run vndadd01.cbl; then enter a vendor number that is already on file. You are warned about the record-on-file condition immediately.

Printing Records in an Indexed File

The complete set of programs for a data file includes add, change, delete, inquire (or look up), possibly a display program such as vnddsp01.cbl, and a printing program that allows you to report on the contents of the file.

You have already written the printing program, but you might not know it yet. The display program, vnddsp01.cbl, was designed as a program to print records to the screen, as if the screen were a scrolling printer.

Perform a little minor surgery on vnddsp01.cbl and you have the printing program vndrpt01.cbl, shown in Listing 13.5.

TYPE: Listing 13.5. A report program for the vendor file.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VNDRPT01.
000300*--------------------------------
000400* Report on the Vendor File.
000500*--------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000     COPY "SLVND01.CBL".
001100
001200     SELECT PRINTER-FILE
001300         ASSIGN TO PRINTER
001400         ORGANIZATION IS LINE SEQUENTIAL.
001500
001600 DATA DIVISION.
001700 FILE SECTION.
001800
001900     COPY "FDVND02.CBL".
002000
002100 FD  PRINTER-FILE
002200     LABEL RECORDS ARE OMITTED.
002300 01  PRINTER-RECORD             PIC X(80).
002400
002500 WORKING-STORAGE SECTION.
002600
002700 01  DETAIL-LINE.
002800     05  PRINT-NUMBER      PIC 9(5).
002900     05  FILLER            PIC X     VALUE SPACE.
003000     05  PRINT-NAME        PIC X(30).
003100     05  FILLER            PIC X     VALUE SPACE.
003200     05  PRINT-CONTACT     PIC X(30).
003300
003400 01  CITY-STATE-DETAIL.
003500     05  PRINT-CITY        PIC X(20).
003600     05  FILLER            PIC X VALUE SPACE.
003700     05  PRINT-STATE       PIC X(2).
003800
003900 01  COLUMN-LINE.
004000     05  FILLER         PIC X(2)  VALUE "NO".
004100     05  FILLER         PIC X(4) VALUE SPACE.
004200     05  FILLER         PIC X(12) VALUE "NAME-ADDRESS".
004300     05  FILLER         PIC X(19) VALUE SPACE.
004400     05  FILLER         PIC X(17) VALUE "CONTACT-PHONE-ZIP".
004500
004600 01  TITLE-LINE.
004700     05  FILLER              PIC X(25) VALUE SPACE.
004800     05  FILLER              PIC X(11)
004900         VALUE "VENDOR LIST".
005000     05  FILLER              PIC X(15) VALUE SPACE.
005100     05  FILLER              PIC X(5) VALUE "PAGE:".
005200     05  FILLER              PIC X(1) VALUE SPACE.
005300     05  PRINT-PAGE-NUMBER PIC ZZZZ9.
005400
005500 77  FILE-AT-END             PIC X.
005600 77  LINE-COUNT              PIC 999 VALUE ZERO.
005700 77  PAGE-NUMBER             PIC 99999 VALUE ZERO.
005800 77  MAXIMUM-LINES           PIC 999 VALUE 55.
005900
006000 PROCEDURE DIVISION.
006100 PROGRAM-BEGIN.
006200
006300     PERFORM OPENING-PROCEDURE.
006400     MOVE ZEROES TO LINE-COUNT
006500                    PAGE-NUMBER.
006600
006700     PERFORM START-NEW-PAGE.
006800
006900     MOVE "N" TO FILE-AT-END.
007000     PERFORM READ-NEXT-RECORD.
007100     IF FILE-AT-END = "Y"
007200         MOVE "NO RECORDS FOUND" TO PRINTER-RECORD
007300         PERFORM WRITE-TO-PRINTER
007400     ELSE
007500         PERFORM PRINT-VENDOR-FIELDS
007600             UNTIL FILE-AT-END = "Y".
007700
007800     PERFORM CLOSING-PROCEDURE.
007900
008000 PROGRAM-DONE.
008100     STOP RUN.
008200
008300 OPENING-PROCEDURE.
008400     OPEN I-O VENDOR-FILE.
008500     OPEN OUTPUT PRINTER-FILE.
008600
008700 CLOSING-PROCEDURE.
008800     CLOSE VENDOR-FILE.
008900     PERFORM END-LAST-PAGE.
009000     CLOSE PRINTER-FILE.
009100
009200 PRINT-VENDOR-FIELDS.
009300     IF LINE-COUNT > MAXIMUM-LINES
009400         PERFORM START-NEXT-PAGE.
009500     PERFORM PRINT-THE-RECORD.
009600     PERFORM READ-NEXT-RECORD.
009700
009800 PRINT-THE-RECORD.
009900     PERFORM PRINT-LINE-1.
010000     PERFORM PRINT-LINE-2.
010100     PERFORM PRINT-LINE-3.
010200     PERFORM PRINT-LINE-4.
010300     PERFORM LINE-FEED.
010400
010500 PRINT-LINE-1.
010600     MOVE SPACE TO DETAIL-LINE.
010700     MOVE VENDOR-NUMBER TO PRINT-NUMBER.
010800     MOVE VENDOR-NAME TO PRINT-NAME.
010900     MOVE VENDOR-CONTACT TO PRINT-CONTACT.
011000     MOVE DETAIL-LINE TO PRINTER-RECORD.
011100     PERFORM WRITE-TO-PRINTER.
011200
011300 PRINT-LINE-2.
011400     MOVE SPACE TO DETAIL-LINE.
011500     MOVE VENDOR-ADDRESS-1 TO PRINT-NAME.
011600     MOVE VENDOR-PHONE TO PRINT-CONTACT.
011700     MOVE DETAIL-LINE TO PRINTER-RECORD.
011800     PERFORM WRITE-TO-PRINTER.
011900
012000 PRINT-LINE-3.
012100     MOVE SPACE TO DETAIL-LINE.
012200     MOVE VENDOR-ADDRESS-2 TO PRINT-NAME.
012300     IF VENDOR-ADDRESS-2 NOT = SPACE
012400         MOVE DETAIL-LINE TO PRINTER-RECORD
012500         PERFORM WRITE-TO-PRINTER.
012600
012700 PRINT-LINE-4.
012800     MOVE SPACE TO DETAIL-LINE.
012900     MOVE VENDOR-CITY TO PRINT-CITY.
013000     MOVE VENDOR-STATE TO PRINT-STATE.
013100     MOVE CITY-STATE-DETAIL TO PRINT-NAME.
013200     MOVE VENDOR-ZIP TO PRINT-CONTACT.
013300     MOVE DETAIL-LINE TO PRINTER-RECORD.
013400     PERFORM WRITE-TO-PRINTER.
013500
013600 READ-NEXT-RECORD.
013700     READ VENDOR-FILE NEXT RECORD
013800         AT END MOVE "Y" TO FILE-AT-END.
013900
014000 WRITE-TO-PRINTER.
014100     WRITE PRINTER-RECORD BEFORE ADVANCING 1.
014200     ADD 1 TO LINE-COUNT.
014300
014400 LINE-FEED.
014500     MOVE SPACE TO PRINTER-RECORD.
014600     PERFORM WRITE-TO-PRINTER.
014700
014800 START-NEXT-PAGE.
014900
015000     PERFORM END-LAST-PAGE.
015100     PERFORM START-NEW-PAGE.
015200
015300 START-NEW-PAGE.
015400     ADD 1 TO PAGE-NUMBER.
015500     MOVE PAGE-NUMBER TO PRINT-PAGE-NUMBER.
015600     MOVE TITLE-LINE TO PRINTER-RECORD.
015700     PERFORM WRITE-TO-PRINTER.
015800     PERFORM LINE-FEED.
015900     MOVE COLUMN-LINE TO PRINTER-RECORD.
016000     PERFORM WRITE-TO-PRINTER.
016100     PERFORM LINE-FEED.
016200
016300 END-LAST-PAGE.
016400     PERFORM FORM-FEED.
016500     MOVE ZERO TO LINE-COUNT.
016600
016700 FORM-FEED.
016800     MOVE SPACE TO PRINTER-RECORD.
016900     WRITE PRINTER-RECORD BEFORE ADVANCING PAGE.
017000

This output illustrates a sample page of the report created by vndrpt01.cbl:

OUTPUT:

VENDOR LIST               PAGE:     1
NO    NAME-ADDRESS                   CONTACT-PHONE-ZIP
00001 AERIAL SIGNS                   HENRIETTA MARKSON
BURBANK AIRPORT                (818) 555-6066
HANGAR 405
BURBANK              CA        90046
00002 ABC PRINTING                   CHARLES JOHANSSEN
1624 FOOTHILL BLVD             (818) 555-4321
SUITE 34
LOS ANGELES          CA        91042
00003 CHARLES SMITH AND SONS         MARTHA HARRISON
1435 SOUTH STREET              (213) 555-4432
LOS ANGELES          CA        90064
00014 RANIER GRAPHICS                JULIA SIMPSON
4433 WASHINGTOn ST             (213) 555-6789
LOS ANGELES          CA        90032
01176 ABERCROMBIE AND OTHERS
1234 45TH ST.                  (213) 555-6543
SUITE 17
LOS ANGELES          CA        92345

ANALYSIS: You can create this program by copying vnddsp01.cbl and then modifying it. Compare this new program to vnddsp01.cbl:

Code, compile, and run this program to get a printed report of the information in your vendor file.

Summary

The basics of keeping indexed files updated include deleting, displaying, adding, and printing records. Today, you learned the following:

Q&A

Q In Listing 13.4, vndadd01.cbl, at lines 007300 through 007700, the WRITE-VENDOR-RECORD paragraph includes a RECORD ALREADY ON FILE message if an attempt is made to write a record that fails. Before this write occurs, the program already has read the record and established that it is not there in READ-VENDOR-RECORD at lines 006100 through 006500. Why does this extra check exist?

A It is good practice when writing COBOL programs to assume that other users will be accessing the same files. Most COBOL programs run on large multi-user systems. In the vndadd01.cbl program, it is possible for a user to enter a vendor number, establish that the record is not on file, start adding the new fields to the file, and then go on a coffee break before pressing Enter on the final field, causing the new record to be written. If someone adds a record with that primary key in the meantime, the user is in for a surprise upon returning. Rather than let the program crash at this point, an effort is made to trap the file-error condition with a message.

Workshop

Quiz

1. Which modes in maintenance programs are usually similar?

a. Add and change.

b. Add and delete.

c. Change, inquire (look up), and delete.

d. Change, inquire (look up), and print.

2. Describe the similarities between change, inquire, and delete modes.

Exercise

Copy vnddsp01.cbl to vnddsp02.cbl; then modify it so that it uses COPY "SLVND01.CBL" and COPY "FDVND02.CBL".


Previous chapterNext chapterContents


© Copyright, Macmillan Computer Publishing. All rights reserved.