Capstone: Writing a Complete Batch Processing Program

COBOL Fundamentals

Chapter 10 · Capstone: Writing a Complete Batch Processing Program

Nine chapters built individual real pieces — divisions, PICTURE clauses, arithmetic, control flow, tables, string handling, file I/O, subprograms. Real COBOL work is almost always exactly this shape combined into one program: a nightly batch job that reads a transaction file, updates an in-memory customer table, and reports what happened. This capstone builds one, TXNBATCH, using every real piece from Chapters 1 through 9.

The Scenario

TXNBATCH reads a real sequential transaction file — one record per deposit or withdrawal, each carrying a customer ID, a transaction code, an amount, and a composite reference/branch string — validates each amount, applies it to an in-memory table of customer balances, and prints a real, running report line for every transaction processed.

File & Data Declarations

IDENTIFICATION DIVISION. PROGRAM-ID. TXNBATCH. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO "TXNFILE.DAT" ORGANIZATION IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE LABEL RECORDS ARE STANDARD. 01 TRANSACTION-RECORD. 05 TXN-CUSTOMER-ID PIC 9(6). 05 TXN-CODE PIC X(1). 05 TXN-AMOUNT PIC S9(7)V99. 05 TXN-DETAIL PIC X(20). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2). 01 WS-EOF-FLAG PIC X(1) VALUE "N". 01 WS-VALID-FLAG PIC X(1). 01 WS-REF-NUMBER PIC X(10). 01 WS-BRANCH-CODE PIC X(10). 01 WS-REPORT-LINE PIC X(60). 01 WS-GRAND-TOTAL PIC S9(9)V99 VALUE 0. 01 WS-DEPOSIT-COUNT PIC 9(5) VALUE 0. 01 WS-WITHDRAW-COUNT PIC 9(5) VALUE 0. 01 WS-CUSTOMER-TABLE. 05 WS-CUSTOMER-ENTRY OCCURS 100 TIMES ASCENDING KEY IS WS-CUST-ID INDEXED BY WS-CUST-IDX. 10 WS-CUST-ID PIC 9(6). 10 WS-CUST-NAME PIC X(30). 10 WS-CUST-BALANCE PIC S9(7)V99.

WS-CUSTOMER-TABLE is Chapter 6's own real OCCURS/INDEXED BY/ASCENDING KEY table, assumed loaded from a customer master file before TXNBATCH's own transaction loop begins — the real focus here is what happens once it's populated.

The Main Processing Loop

PROCEDURE DIVISION. OPEN INPUT TRANSACTION-FILE. IF WS-FILE-STATUS NOT = "00" DISPLAY "ERROR OPENING TRANSACTION FILE: " WS-FILE-STATUS STOP RUN END-IF. READ TRANSACTION-FILE AT END MOVE "Y" TO WS-EOF-FLAG END-READ. PERFORM UNTIL WS-EOF-FLAG = "Y" UNSTRING TXN-DETAIL DELIMITED BY "-" INTO WS-REF-NUMBER WS-BRANCH-CODE END-UNSTRING SEARCH ALL WS-CUSTOMER-ENTRY AT END DISPLAY "UNKNOWN CUSTOMER: " TXN-CUSTOMER-ID WHEN WS-CUST-ID(WS-CUST-IDX) = TXN-CUSTOMER-ID CALL "BALCHECK" USING TXN-AMOUNT WS-VALID-FLAG IF WS-VALID-FLAG = "Y" EVALUATE TXN-CODE WHEN "D" ADD TXN-AMOUNT TO WS-CUST-BALANCE(WS-CUST-IDX) ADD 1 TO WS-DEPOSIT-COUNT WHEN "W" SUBTRACT TXN-AMOUNT FROM WS-CUST-BALANCE(WS-CUST-IDX) ADD 1 TO WS-WITHDRAW-COUNT WHEN OTHER DISPLAY "UNKNOWN TRANSACTION CODE: " TXN-CODE END-EVALUATE COMPUTE WS-GRAND-TOTAL = WS-GRAND-TOTAL + TXN-AMOUNT STRING WS-CUST-NAME(WS-CUST-IDX) DELIMITED BY SPACE " [" DELIMITED BY SIZE TXN-CODE DELIMITED BY SIZE "] BRANCH " DELIMITED BY SIZE WS-BRANCH-CODE(1:2) DELIMITED BY SIZE INTO WS-REPORT-LINE END-STRING DISPLAY WS-REPORT-LINE ELSE DISPLAY "REJECTED - NEGATIVE AMOUNT: " TXN-CUSTOMER-ID END-IF END-SEARCH READ TRANSACTION-FILE AT END MOVE "Y" TO WS-EOF-FLAG END-READ END-PERFORM. CLOSE TRANSACTION-FILE. DISPLAY "BATCH COMPLETE". DISPLAY "DEPOSITS PROCESSED: " WS-DEPOSIT-COUNT. DISPLAY "WITHDRAWALS PROCESSED: " WS-WITHDRAW-COUNT. DISPLAY "GRAND TOTAL MOVED: " WS-GRAND-TOTAL. GOBACK.

Chapter-Attribution Table

ChapterWhat TXNBATCH Draws From It
Ch. 2 — Four DivisionsThe genuine ENVIRONMENT/DATA/PROCEDURE division structure the whole program is built on
Ch. 3 — Data Types & PICTUREEvery field's real PICTURE clause, and level numbers grouping TRANSACTION-RECORD and WS-CUSTOMER-ENTRY
Ch. 4 — MOVE, COMPUTE & ArithmeticThe real ADD/SUBTRACT/COMPUTE statements applying each transaction and tracking WS-GRAND-TOTAL
Ch. 5 — IF, EVALUATE & PERFORMThe real EVALUATE TXN-CODE dispatch, and PERFORM UNTIL driving the whole batch loop
Ch. 6 — OCCURS & TablesWS-CUSTOMER-TABLE itself, its real INDEXED BY, and the real SEARCH ALL binary-search lookup
Ch. 7 — STRING, UNSTRING & Reference ModificationSplitting TXN-DETAIL with real UNSTRING, building WS-REPORT-LINE with real STRING, and WS-BRANCH-CODE(1:2) as a real substring
Ch. 8 — Sequential FilesThe real SELECT/FD, OPEN, the priming-read PERFORM UNTIL pattern, WRITE-free reading, and CLOSE
Ch. 9 — Subprograms & CALLThe real CALL "BALCHECK", reused unchanged from Chapter 9, validating each amount before it's applied

Hands-On Exercises

Exercise 1

Using this chapter's own real TXNBATCH program, explain in your own words why SEARCH ALL, rather than a plain sequential SEARCH, is the genuinely correct choice for finding a customer by TXN-CUSTOMER-ID in WS-CUSTOMER-TABLE — and what real, silent risk this chapter's own Chapter 6 warning would apply here if the customer table were ever loaded out of order.

📄 View solution
Exercise 2

Using this chapter's own real TXNBATCH program, explain in your own words what genuinely happens to a real transaction record whose TXN-CUSTOMER-ID doesn't match any entry in WS-CUSTOMER-TABLE, and separately, what happens to one whose TXN-CODE is neither "D" nor "W".

📄 View solution
Exercise 3

Using this chapter's own real TXNBATCH program, explain in your own words why CALL "BALCHECK" USING TXN-AMOUNT WS-VALID-FLAG works correctly here even though BALCHECK's own LINKAGE SECTION, back in Chapter 9, was written using the names LS-BALANCE and LS-VALID-FLAG, and was originally called with WS-BALANCE rather than TXN-AMOUNT.

📄 View solution

Chapter 10 Quick Reference — TXNBATCH's Own Real Pieces

  • File I/O — real SELECT/FD, OPEN INPUT, priming-read PERFORM UNTIL, CLOSE
  • Table lookup — WS-CUSTOMER-TABLE with real INDEXED BY and SEARCH ALL
  • Subprogram validation — CALL "BALCHECK" reused unchanged from Chapter 9, matched purely by real position
  • Control flow — real EVALUATE dispatch on TXN-CODE, nested IF for validation results
  • String handling — real UNSTRING splitting TXN-DETAIL, STRING building the report line, reference modification for a real 2-character branch substring
  • Arithmetic — real ADD/SUBTRACT applying each transaction, COMPUTE tracking the running grand total
Course Complete COBOL Fundamentals is now complete, 10/10 chapters. Real COBOL work rarely stops at fixed-format sequential files, though — COBOL Intermediate/Advanced picks up directly from here with indexed and relative file organization, copybooks, JCL and batch scheduling, embedded SQL against a real database, sort/merge operations, and reading and maintaining genuine legacy code.