ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

COBOL Indexed File Access for Record Updates

COBOL

Goal -- WPM

Ready
Exercise Algorithm Area
1IDENTIFICATION DIVISION.
2PROGRAM-ID. INDEXED-FILE-UPDATER.
3AUTHOR. Admin.
4
5ENVIRONMENT DIVISION.
6INPUT-OUTPUT SECTION.
7FILE-CONTROL.
8SELECT CUSTOMER-FILE ASSIGN TO 'customers.idx'
9ORGANIZATION IS INDEXED
10ACCESS MODE IS RANDOM
11RECORD KEY IS CUST-ID
12FILE STATUS IS WS-FILE-STATUS.
13SELECT UPDATE-TRANSACTIONS ASSIGN TO 'updates.dat'
14ORGANIZATION IS SEQUENTIAL.
15
16DATA DIVISION.
17FILE SECTION.
18FD CUSTOMER-FILE.
1901 CUSTOMER-RECORD.
2005 CUST-ID PIC X(8).
2105 CUST-NAME PIC X(30).
2205 CUST-ADDRESS PIC X(50).
2305 CUST-CREDIT-LIMIT PIC S9(7)V2.
2405 CUST-BALANCE PIC S9(9)V2.
25
26FD UPDATE-TRANSACTIONS.
2701 UPDATE-RECORD.
2805 UPD-CUST-ID PIC X(8).
2905 UPD-OPERATION PIC X(1).
3005 UPD-NEW-VALUE PIC X(10).
31
32WORKING-STORAGE SECTION.
3301 WS-FILE-STATUS PIC XX.
3488 SUCCESSFUL-OPERATION VALUE '00'.
3588 RECORD-NOT-FOUND VALUE '10'.
3688 DUPLICATE-KEY VALUE '23'.
3701 WS-EOF-UPDATES PIC X VALUE 'N'.
3888 END-OF-UPDATE-FILE VALUE 'Y'.
3901 WS-UPDATE-COUNT PIC 9(9) VALUE 0.
4001 WS-SUCCESS-COUNT PIC 9(9) VALUE 0.
4101 WS-FAIL-COUNT PIC 9(9) VALUE 0.
42
43PROCEDURE DIVISION.
44MAIN-PROCEDURE.
45PERFORM 1000-INITIALIZE.
46PERFORM 2000-PROCESS-UPDATES UNTIL END-OF-UPDATE-FILE.
47PERFORM 3000-TERMINATE.
48STOP RUN.
49
501000-INITIALIZE.
51OPEN I-O CUSTOMER-FILE.
52IF NOT SUCCESSFUL-OPERATION
53DISPLAY 'Error opening CUSTOMER-FILE. Status: ' WS-FILE-STATUS
54MOVE 1 TO WS-FAIL-COUNT *> Indicate failure to open
55MOVE 'Y' TO WS-EOF-UPDATES *> Prevent processing
56END-IF.
57OPEN INPUT UPDATE-TRANSACTIONS.
58IF NOT SUCCESSFUL-OPERATION
59DISPLAY 'Error opening UPDATE-TRANSACTIONS. Status: ' WS-FILE-STATUS
60MOVE 1 TO WS-FAIL-COUNT
61MOVE 'Y' TO WS-EOF-UPDATES
62END-IF.
63DISPLAY 'Customer File Updater Started.'
64READ UPDATE-TRANSACTIONS
65AT END MOVE 'Y' TO WS-EOF-UPDATES.
66
672000-PROCESS-UPDATES.
68ADD 1 TO WS-UPDATE-COUNT.
69MOVE UPD-CUST-ID TO CUST-ID.
70MOVE SPACES TO CUSTOMER-RECORD.
71MOVE UPD-CUST-ID TO CUST-ID.
72
73*> Read the record to be updated
74READ CUSTOMER-FILE KEY IS CUST-ID
75INVALID KEY
76DISPLAY 'Update failed: Customer ID ' UPD-CUST-ID ' not found.'
77ADD 1 TO WS-FAIL-COUNT
78NOT INVALID KEY
79*> Record found, now apply update based on operation code
80PERFORM 4000-APPLY-UPDATE
81IF SUCCESSFUL-OPERATION
82DISPLAY 'Update successful for Customer ID: ' UPD-CUST-ID
83ADD 1 TO WS-SUCCESS-COUNT
84ELSE
85DISPLAY 'Update failed for Customer ID: ' UPD-CUST-ID '. Status: ' WS-FILE-STATUS
86ADD 1 TO WS-FAIL-COUNT
87END-IF
88END-READ.
89
90*> Read next transaction
91READ UPDATE-TRANSACTIONS
92AT END MOVE 'Y' TO WS-EOF-UPDATES.
93
943000-TERMINATE.
95CLOSE CUSTOMER-FILE.
96CLOSE UPDATE-TRANSACTIONS.
97DISPLAY '----------------------------------------'
98DISPLAY 'Update process finished.'
99DISPLAY 'Total updates attempted: ' WS-UPDATE-COUNT
100DISPLAY 'Successful updates: ' WS-SUCCESS-COUNT
101DISPLAY 'Failed updates: ' WS-FAIL-COUNT
102DISPLAY '----------------------------------------'
103
1044000-APPLY-UPDATE.
105*> Based on UPD-OPERATION, modify the appropriate field
106IF UPD-OPERATION = 'C' THEN *> Change Credit Limit
107MOVE UPD-NEW-VALUE TO CUST-CREDIT-LIMIT
108REWRITE CUSTOMER-RECORD
109ELSE IF UPD-OPERATION = 'B' THEN *> Update Balance
110MOVE UPD-NEW-VALUE TO CUST-BALANCE
111REWRITE CUSTOMER-RECORD
112ELSE
113DISPLAY 'Invalid operation code: ' UPD-OPERATION
114MOVE 'XX' TO WS-FILE-STATUS *> Simulate failure
115END-IF.
Algorithm description viewbox

COBOL Indexed File Access for Record Updates

Algorithm description:

This COBOL program updates records in an indexed customer file. It reads update transactions from a sequential file, identifies the target customer record by ID, and modifies fields like credit limit or balance based on the transaction type. It includes robust error handling for non-existent records and invalid operations, reporting the success or failure of each update.

Algorithm explanation:

The program uses `ACCESS MODE IS RANDOM` on an indexed file (`CUSTOMER-FILE`) for efficient record retrieval and modification. It reads update instructions from `UPDATE-TRANSACTIONS`. For each transaction, it reads the target `CUSTOMER-RECORD` using the `CUST-ID`. The `INVALID KEY` clause of the `READ` statement handles cases where the customer ID is not found. If the record is found, the `4000-APPLY-UPDATE` routine modifies the relevant field (`CUST-CREDIT-LIMIT` or `CUST-BALANCE`) based on `UPD-OPERATION` and then uses `REWRITE` to save the modified record back to the indexed file. The `REWRITE` operation itself can fail, and its success is checked via `WS-FILE-STATUS`. Time complexity for each update is O(log N) due to indexed file access. Total time is O(M log N), where M is the number of updates and N is the number of records in the customer file. Space complexity is O(1). Edge cases include file opening errors, missing records, invalid operation codes, and potential issues during the `REWRITE` operation.

Pseudocode:

INITIALIZE:
  Open CUSTOMER-FILE for I/O.
  Open UPDATE-TRANSACTIONS for input.
  Check file opening statuses.
  Read the first update transaction.

PROCESS_UPDATES:
  Increment update count.
  Set CUSTOMER-FILE key to the customer ID from the update transaction.
  READ the CUSTOMER-FILE record.
  IF record not found THEN
    Display 'not found' error.
    Increment fail count.
  ELSE
    APPLY the update based on the operation code.
    IF update was successful THEN
      Display success message.
      Increment success count.
    ELSE
      Display update failure message.
      Increment fail count.
    END IF.
  END IF.
  Read the next update transaction.

TERMINATE:
  Close all files.
  Display summary counts (total, success, fail).

APPLY_UPDATE:
  Based on the operation code in the update transaction:
    If 'C' (Change Credit Limit):
      Move new value to CUST-CREDIT-LIMIT.
      REWRITE CUSTOMER-RECORD.
    If 'B' (Update Balance):
      Move new value to CUST-BALANCE.
      REWRITE CUSTOMER-RECORD.
    Else (Invalid Operation):
      Display invalid operation error.
      Simulate file status failure.