skip to main content

Object-oriented Programming : Sample application : Account class

Account class
The source code for the account class is illustrated below. The class has two factory methods:
The account class also has six instance methods:
CLASS-ID. Account.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
IDENTIFICATION DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  number-of-accounts     PIC 9(5VALUE ZERO.
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
METHOD-ID. addAccount as "addAccount".
PROCEDURE DIVISION.
    ADD 1 TO number-of-accounts.
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. removeAccount as "removeAccount".
PROCEDURE DIVISION.
    SUBTRACT 1 FROM number-of-accounts.
END METHOD.
END FACTORY.
IDENTIFICATION DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  account-balance        PIC S9(9)V99.
01  account-number         PIC X(9).
01  the-date               PIC 9(8).
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
METHOD-ID. newAccount as "new".
DATA DIVISION.
PROCEDURE DIVISION.
    INVOKE SELF "initializeAccount" USING BY CONTENT number-of-accounts.
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. displayUI as "displayUI".
DATA DIVISION.
WORKING-STORAGE SECTION.
01  in-data.
    03 action-type         PIC X.
    03 in-amount           PIC S9(9)V99.
    03 in-wrk              PIC X(12).
PROCEDURE DIVISION.
    DISPLAY "Enter D for Deposit, B for Balance or W for Withdrawal"
    ACCEPT in-data
    EVALUATE action-type
    WHEN "D"
         PERFORM get-amount
         INVOKE SELF "deposit" USING in-amount
    WHEN "W"
         PERFORM get-amount
         INVOKE SELF "withdraw" USING in-amount
    WHEN "B"
         INVOKE SELF "balance"
    WHEN OTHER
         DISPLAY "Enter valid transaction type."
    END-EVALUATE
    GOBACK
    DISPLAY "Enter amount 9(9).99"
    ACCEPT in-wrk
    COMPUTE in-amount = FUNCTION NUMVAL (in-wrk)
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. balance as "balance".
DATA DIVISION.
WORKING-STORAGE SECTION.
01  display-balance        PIC $ZZZ,ZZZ,ZZ9.99.
PROCEDURE DIVISION.
    MOVE account-balance to display-balance
    DISPLAY "Your Account Balance is:" display-balance
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. deposit as "deposit".
DATA DIVISION.
LINKAGE SECTION.
01  in-deposit             PIC S9(9)V99.
PROCEDURE DIVISION USING in-deposit.
    ADD in-deposit TO account-balance
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. withdraw as "withdraw".
DATA DIVISION.
LINKAGE SECTION.
01  in-withdraw            PIC S9(9)V99.
PROCEDURE DIVISION USING in-withdraw.
    IF account-balance >= in-withdraw
       SUBTRACT in-withdraw FROM account-balance
       DISPLAY "Your Balance is Inadequate"
    END-IF
END METHOD.
IDENTIFICATION DIVISION.
METHOD-ID. initializeAccount as "initializeAccount".
DATA DIVISION.
LINKAGE SECTION.
01  new-account-number     PIC 9(5).
PROCEDURE DIVISION USING new-account-number.
    MOVE ZERO TO account-balance
    MOVE new-account-number TO account-number
    ACCEPT the-date FROM CENTURY-DATE
END METHOD.
END OBJECT.
The following statements from the above code
INVOKE SELF "initializeAccount" USING BY CONTENT number-of-accounts
INVOKE SELF "deposit" USING in-amount
INVOKE SELF "withdraw" USING in-amount
INVOKE SELF "balance"
could also be written as
SELF:>initializeAccount (number-of-accounts)
SELF:>deposit (in-amount)
SELF:>withdraw (in-amount)
SELF:>balance ()
The main advantages in using the object:>method syntax instead of INVOKE are:
INVOKE myObj "method1" 
  ON EXCEPTION
     DISPLAY MESSAGE exception-object:>getMessage()
INVOKE myObj "method2" 
  ON EXCEPTION
     DISPLAY MESSAGE exception-object:>getMessage()
can be written as
   myObj:>method1();
   myObj:>method2();
CATCH EXCETPION
   DISPLAY MESSAGE exception-object:>getMessage()
INVOKE objParent "getChild" GIVING objChild.
INVOKE objChild "doSomething".
With the object:>method syntax, instead, the objective can be achieved with one single statement:
objParent:>getChild:>doSomething().
Note - object:>method could be considered as a parameter of a previous COBOL statement if
If it’s not possible to close the previous statement with a dot, use two semicolons.
For example, the following code is not accurate and leads to compiler errors and unwanted runtime behaviors:
if test-var = 1
   move 0 to test-var
   myObj:>myMethod1()
display test-var
myObj:>myMethod2()
myObj:>myMethod1 would be considered as a second parameter of the previous MOVE while the result of myObj:>myMethod2 would be displayed along with test-var.
The above code can be corrected as follows:
if test-var = 1
   move 0 to test-var;;
   myObj:>myMethod1()
display test-var.
myObj:>myMethod2()

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left