Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Projects and Practice

Project: Updating an Account Master

Build the classic COBOL master and transaction update - sequential balance-line matching with HIGH-VALUES, add, rename, deposit, withdraw and close, an error report, and the indexed-file alternative.

The second project is the heart of batch banking and billing: applying a day's transactions to a master file. An old master file holds every account; a transaction file holds the day's changes; the program produces a new master and an error report. Because both inputs are sorted by account number, the program can process them in a single pass without random access, which is why this "sequential update" pattern scales to hundreds of millions of records.

The pattern combines sequential files, error handling and the sort order guaranteed by SORT. At the end we compare it with the indexed-file approach.

The files

Old master (oldmast.dat), sorted by account number, 29 bytes:

COBOL
       FD  OLD-MASTER.
       01  OM-REC.
           05  OM-KEY       PIC 9(5).
           05  OM-NAME      PIC X(15).
           05  OM-BALANCE   PIC 9(7)V99.

Transactions (trans.dat), sorted by account number (and kept in time order within an account), 29 bytes:

COBOL
       FD  TRANS-FILE.
       01  TR-REC.
           05  TR-KEY       PIC 9(5).
           05  TR-TYPE      PIC X.
               88  TR-NEW       VALUE "N".
               88  TR-RENAME    VALUE "R".
               88  TR-CLOSE     VALUE "X".
               88  TR-DEPOSIT   VALUE "D".
               88  TR-WITHDRAW  VALUE "W".
           05  TR-NAME      PIC X(15).
           05  TR-AMOUNT    PIC 9(7)V99.

The program writes newmast.dat (same layout as the old master) and errors.rpt.

Type Meaning Rules
N New account Key must not exist; name and opening deposit given
R Rename Account must exist
D Deposit Account must exist
W Withdrawal Account must exist and balance must cover it
X Close Account must exist; it is not copied to the new master

The matching algorithm

The program reads one record from each file and compares the keys. Whichever is lower is processed:

flowchart TD
    S["Compare TR-KEY with OM-KEY"] --> L{"TR-KEY < OM-KEY"}
    L -->|"yes"| N["New account? write it.<br/>Anything else: error 'no such account'.<br/>Read next transaction"]
    L -->|"no"| E{"TR-KEY = OM-KEY"}
    E -->|"yes"| A["Apply the transaction to the master record.<br/>Read next transaction"]
    E -->|"no (TR-KEY > OM-KEY)"| W["No more changes for this account:<br/>write master unless closed.<br/>Read next master"]

Three cases only. When a file ends, its key is set to HIGH-VALUES, so the other file drains naturally.

The trick that makes this work is HIGH-VALUES, a figurative constant meaning the highest character in the collating sequence. When a file reaches end of file you set its key to HIGH-VALUES; the other file's keys are always lower, so it keeps being processed. The loop ends when both keys are HIGH-VALUES. This removes a pile of special cases for "one file ended first".

COBOL
       READ-MASTER.
           READ OLD-MASTER
               AT END MOVE HIGH-VALUES TO OM-KEY-X
           END-READ.

(Because OM-KEY is numeric, a common approach is to treat the key as alphanumeric for the sentinel, for instance by redefining it as PIC X(5), or by using 99999 as the highest valid key. Use whichever your standards prefer; the idea is the same.)

The main loop

COBOL
       MAIN.
           OPEN INPUT OLD-MASTER TRANS-FILE
                OUTPUT NEW-MASTER ERROR-REPORT
           PERFORM READ-MASTER
           PERFORM READ-TRANS
           PERFORM UNTIL OM-KEY = HIGH-KEY AND TR-KEY = HIGH-KEY
               EVALUATE TRUE
                   WHEN TR-KEY < OM-KEY
                       PERFORM NO-MATCH
                       PERFORM READ-TRANS
                   WHEN TR-KEY = OM-KEY
                       PERFORM APPLY-TRANS
                       PERFORM READ-TRANS
                   WHEN TR-KEY > OM-KEY
                       PERFORM FINISH-MASTER
                       PERFORM READ-MASTER
               END-EVALUATE
           END-PERFORM
           CLOSE OLD-MASTER TRANS-FILE NEW-MASTER ERROR-REPORT
           STOP RUN.

HIGH-KEY is a Working-Storage constant, 99999, used as the sentinel (valid account numbers are below it). The paragraphs:

COBOL
       NO-MATCH.
           IF TR-NEW
               MOVE TR-KEY    TO NM-KEY
               MOVE TR-NAME   TO NM-NAME
               MOVE TR-AMOUNT TO NM-BALANCE
               WRITE NM-REC
           ELSE
               MOVE "NO SUCH ACCOUNT" TO ER-TEXT
               PERFORM WRITE-ERROR
           END-IF.

       APPLY-TRANS.
           EVALUATE TRUE
               WHEN TR-DEPOSIT
                   ADD TR-AMOUNT TO OM-BALANCE
               WHEN TR-WITHDRAW
                   IF TR-AMOUNT > OM-BALANCE
                       MOVE "INSUFFICIENT FUNDS" TO ER-TEXT
                       PERFORM WRITE-ERROR
                   ELSE
                       SUBTRACT TR-AMOUNT FROM OM-BALANCE
                   END-IF
               WHEN TR-RENAME
                   MOVE TR-NAME TO OM-NAME
               WHEN TR-CLOSE
                   SET ACCOUNT-CLOSED TO TRUE
               WHEN TR-NEW
                   MOVE "DUPLICATE ACCOUNT" TO ER-TEXT
                   PERFORM WRITE-ERROR
           END-EVALUATE.

       FINISH-MASTER.
           IF ACCOUNT-CLOSED
               SET ACCOUNT-OPEN TO TRUE
           ELSE
               WRITE NM-REC FROM OM-REC
           END-IF.

Everything that applies to one account takes place in OM-REC while the program stays on that master; the changed record is written when the next transaction key is greater than the master key (or the transaction file is exhausted). This allows several transactions for one account in a row: deposit, then withdraw.

A subtle limitation: an N transaction followed by a deposit to the same new account in the same run would hit the "no such account" branch, because the new account never became the current master. Real systems handle it with a rule that adds are applied in a pre-processing step, or by merging new accounts into the master stream. That is a good extension exercise.

Running it with a stand-in

The Python code below models the same three-case match with HIGH-VALUES represented by a large number. It is a labelled stand-in, with the same records on both sides, so you can follow every decision.

Python Stand-in: master and transaction update with an error report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The new master lists accounts 10001 (Ada, 1,200.00), 10002 (Bob Builder, 100.00), 10003 (Cyrus Young, 250.50), 10006 (Fay Hill, 20.00) and 10007 (Gus Gray, 0.00). Account 10005 was closed. The error report has three lines: the 1,500.00 withdrawal from 10001, the deposit to the non-existent 10004 and the 5.00 withdrawal from 10007.

Notice that the output is in key order without any sorting: new accounts are written at the moment they are met in the key sequence, which is exactly why the new master is also sorted.

Python Stand-in: the same update done with an indexed file (dictionary)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both models reach the same result. The dictionary version has an advantage: transactions need no ordering and a new account can be used straight away.

Sequential update or indexed update?

Sequential (balance line) Indexed (VSAM KSDS)
Inputs must be sorted Yes No
Touches every master record Yes (writes a new copy) Only the records that change
Best when Most accounts change each run, or masters are huge and run once a night A small fraction changes, or users need updates immediately
Recovery Easy: the old master is unchanged, so rerun Needs backups or logs; a failed run may leave half-updated data
Typical use Nightly billing, statements Online account maintenance

The old master surviving untouched is a real advantage: if tonight's run is wrong you rerun it from the same input. Many banks keep generations of masters (GDGs on the mainframe) for exactly this reason. Both styles coexist in most installations.

Extensions

  1. Write a control report: counts of records read and written, number of each transaction type and errors, with total balance before and after, so operators can prove the run balanced.
  2. Validate the sequence: stop with an error if a file is out of order (key lower than the previous one). This is the most common cause of a corrupt update.
  3. Support a transaction date and keep the day's transactions in a history file.
  4. Process N and following transactions for the same new account correctly.
  5. Convert the program to the indexed approach with REWRITE and DELETE, then rerun the same test data to compare results.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are the old master and the transaction file both sorted by key?

    Matching keys by comparing the current records only works when both files are in the same order.
  2. 2. What does HIGH-VALUES do at end of file?

    The sentinel removes special cases: the loop simply runs until both keys are HIGH-VALUES.
  3. 3. A transaction key is lower than the current master key. What does that mean?

    No master record carries that key, so an N transaction creates an account and any other type is an error.
  4. 4. What is a benefit of writing a NEW master instead of updating in place?

    Because the input is never changed, recovery is simply to run the job again.

Summary

  • The sequential update matches a sorted master and sorted transactions in one pass with three cases: transaction key lower, equal or higher.
  • HIGH-VALUES (or a high sentinel) at end of file ends the loop cleanly.
  • Apply all transactions for a key to the master record, then write it when the key moves on; closed accounts are not written.
  • Errors go to a report; control totals and sequence checks make the run trustworthy.
  • Indexed updates are simpler for small changes, sequential updates are safer and faster for bulk work.

Next: Practice problems and interview questions