We will now look at modifying the newly created FILEIO2 OO COBOL program into a more sophisticated OO COBOL program. We will use the FILEIO3.PRJ project as a reference for the following discussion.
When the original FILEIO program was modified to make it a simple OO COBOL program, the intent was to make a few changes as possible to it to accomplish the task of making it a valid OO COBOL program.
As we can see from executing the TESTFILEIO2 program, the behavior of the FILEIO2 OO COBOL program is the same as the behavior of the original FILEIO program.
In some cases when moving existing legacy applications into the OO COBOL world (remember that creating COM modules in COBOL 97 requires that they be OO COBOL programs), making the minimal set of changes will accomplish the task.
In the following example, however, we are going to make some changes to FILEIO2’s actual structure that lends better to object- oriented methodology. Specifically, we are going to get rid of the Evaluate loop logic and break each of the three paragraphs out into Separate Methods - one to OPEN the file, another method to READ the file, and finally a third method to CLOSE the file.
We will then modify the TESTFILEIO2 program to call these three separate methods.
Figure 2.6 shows the FILEIO2 program modified as previously described. The new version of this program has been named FILEIO3.
Identification Division. Class-ID. FILEIO3 Inherits FJBASE. Environment Division. Configuration Section. Repository. Class FJBASE. Object. Environment Division. Input-Output Section. File-Control. Select Infile Assign To "test.txt" Organization is Line Sequential File Status is Infile-Status. Data Division. File Section. FD Infile. 01 Infile-Record Pic X(132). Working-Storage Section. 01 Infile-Status Pic 99. 01 File-Opened-Flag Pic 9 Value 0. Procedure Division. Method-ID. OPEN-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division Using File-Operation Return-Status Return-Record. Open-File. If File-Opened-Flag Not = 0 *> File is already open, so return error Move 99 To Return-Status Else Open Input Infile Move Infile-Status To Return-Status If Infile-Status = 0 Move 1 To File-Opened-Flag End-If End-If. Open-File-Exit. Exit. End Method OPEN-FILE. Method-ID. CLOSE-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division Using File-Operation Return-Status Return-Record. Close-File. If File-Opened-Flag = 0 *> File is not open, so return error Move 88 To Return-Status Else Close Infile Move Infile-Status To Return-Status Move 0 To File-Opened-Flag End-If. Close-File-Exit. Exit. End Method CLOSE-FILE. Method-ID. READ-FILE. Data Division. Linkage Section. 01 File-Operation Pic X(5). 01 Return-Status Pic 99. 01 Return-Record Pic X(132). Procedure Division Using File-Operation Return-Status Return-Record. Read-File. If File-Opened-Flag = 0 *> File is not open, so return error Move 88 To Return-Status Else Read Infile Move Infile-Status to Return-Status Move Infile-Record to Return-Record End-If. Read-File-Exit. Exit. End Method READ-FILE. End Object. End Class FILEIO3.
Figure 2.6 The FILEIO3 program
If you compare the FILEIO3 program shown in Figure 2.6 to the FILEIO2 program shown in Figure 2.4, you will notice the following modifications:
The Class-ID has been changed to FILEIO3.
The main Evaluate loop logic has been removed and we have deleted the CALL_FILEIO2 method and create three new methods named “OPEN-FILE”, “READ-FILE” and “CLOSE-FILE”.
Specifically, if you examine the three new methods, you’ll note they contain identical LINKAGE SECTIONS and PROCEDURE DIVISION USING… statements. The original procedural logic contained in these three new methods (which used to be 3 separate paragraphs) is identical. We have simply placed some code around each of the individual paragraphs to turn them into separate methods.
We now need to modify the TESTFILEIO2 program to call these new methods. The converted program is now named TESTFILEIO3 and is shown in Figure 2.7.
Identification Division. Program-ID. TESTFILEIO3. Environment Division. Configuration Section. Repository. Class FILEIO3. Data Division. Working-Storage Section. 01 FILEIO-PTR Object Reference. 01 File-Operation Pic X(5). 01 Return-Status Pic 99 Value 0. 01 Return-Record Pic X(132). Procedure Division. Invoke FILEIO3 "NEW" Returning FILEIO-PTR. Invoke FILEIO-PTR "OPEN-FILE" Using File-Operation Return-Status Return-Record. If Return-Status = 0 Perform Until Return-Status Not = 0 Invoke FILEIO-PTR "READ-FILE" Using File-Operation Return-Status Return-Record If Return-Status = 0 Display Return-Record End-If End-Perform Invoke FILEIO-PTR "CLOSE-FILE" Using File-Operation Return-Status Return-Record End-If. Set FILEIO-PTR To Null. Exit Program.
Figure 2.7 The TESTFILEIO3 program
Note that the only modifications to the TESTFILEIO3 program are:
We no longer need to specify the File-Operation parameter. We could, in fact remove this parameter completely from the program, but we will leave it in for now, so we do not have to modify the LINKAGE SECTIONS of the three methods in the FILEIO3 program. We no longer need this parameter because we are calling specific methods to OPEN, READ, and CLOSE the file.
We have removed the commented-out CALL statements for clarity.
We have changed the Invoke statement to the “CALL_FILEIO2” method to instead Invoke the 3 specific methods in FILEIO3.
We now have a much more object-oriented FILEIO3 program and we can see that even though we have made FILEIO3 a bit larger in number of lines of source code than the previous FILEIO2 program, the TESTFILEIO3 program that calls it is much simpler. This is on of the benefits of using Object-Oriented COBOL.
In the final example we will clean up the application even more and create a final polished and well-structured object-oriented FILEIO4 program.