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

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

COBOL Migration Utility for Data Format Conversion

COBOL

Goal -- WPM

Ready
Exercise Algorithm Area
1IDENTIFICATION DIVISION.
2PROGRAM-ID. DATA-MIGRATION-UTILITY.
3AUTHOR. Admin.
4
5ENVIRONMENT DIVISION.
6INPUT-OUTPUT SECTION.
7FILE-CONTROL.
8SELECT LEGACY-DATA ASSIGN TO 'legacy.dat'
9ORGANIZATION IS SEQUENTIAL.
10SELECT MODERN-DATA ASSIGN TO 'modern.dat'
11ORGANIZATION IS SEQUENTIAL.
12
13DATA DIVISION.
14FILE SECTION.
15FD LEGACY-DATA.
1601 LEGACY-RECORD.
1705 LEGACY-ID PIC X(5).
1805 LEGACY-NAME PIC X(20).
1905 LEGACY-AMOUNT PIC 9(7)V2.
2005 FILLER PIC X(8).
21
22FD MODERN-DATA.
2301 MODERN-RECORD.
2405 MODERN-CUSTOMER-ID PIC X(10).
2505 MODERN-FULL-NAME PIC X(40).
2605 MODERN-TRANSACTION-AMT PIC S9(9)V2 COMP-3.
2705 MODERN-STATUS-CODE PIC X(2) VALUE 'OK'.
28
29WORKING-STORAGE SECTION.
3001 WS-EOF-LEGACY PIC X VALUE 'N'.
3188 END-OF-LEGACY-FILE VALUE 'Y'.
3201 WS-RECORD-COUNT PIC 9(9) VALUE 0.
33
34PROCEDURE DIVISION.
35MAIN-PROCEDURE.
36PERFORM 1000-INITIALIZE.
37PERFORM 2000-PROCESS-RECORDS UNTIL END-OF-LEGACY-FILE.
38PERFORM 3000-TERMINATE.
39STOP RUN.
40
411000-INITIALIZE.
42OPEN INPUT LEGACY-DATA.
43OPEN OUTPUT MODERN-DATA.
44DISPLAY 'Starting data migration...' .
45READ LEGACY-DATA
46AT END MOVE 'Y' TO WS-EOF-LEGACY.
47
482000-PROCESS-RECORDS.
49ADD 1 TO WS-RECORD-COUNT.
50PERFORM 4000-MAP-AND-CONVERT.
51WRITE MODERN-RECORD.
52READ LEGACY-DATA
53AT END MOVE 'Y' TO WS-EOF-LEGACY.
54
553000-TERMINATE.
56CLOSE LEGACY-DATA.
57CLOSE MODERN-DATA.
58DISPLAY 'Data migration complete.' .
59DISPLAY 'Total records processed: ' WS-RECORD-COUNT.
60
614000-MAP-AND-CONVERT.
62*> Map LEGACY-ID to MODERN-CUSTOMER-ID
63MOVE LEGACY-ID TO MODERN-CUSTOMER-ID.
64
65*> Map LEGACY-NAME to MODERN-FULL-NAME
66MOVE LEGACY-NAME TO MODERN-FULL-NAME.
67
68*> Map LEGACY-AMOUNT to MODERN-TRANSACTION-AMT
69*> Ensure sufficient space for sign and decimal places
70MOVE LEGACY-AMOUNT TO MODERN-TRANSACTION-AMT.
71
72*> MODERN-STATUS-CODE is initialized to 'OK' in record definition.
73
74*> Example of padding LEGACY-ID if it's shorter than MODERN-CUSTOMER-ID
75*> (assuming LEGACY-ID is always 5 chars and MODERN-CUSTOMER-ID is 10)
76*> If LEGACY-ID was shorter, we might pad it:
77*> MOVE SPACES TO MODERN-CUSTOMER-ID(6:5).
78*> MOVE LEGACY-ID TO MODERN-CUSTOMER-ID(1:5).
79
80*> Example of potential data transformation if needed:
81*> IF LEGACY-AMOUNT < 0 THEN
82*> MOVE 'DR' TO MODERN-STATUS-CODE
83*> END-IF.
Algorithm description viewbox

COBOL Migration Utility for Data Format Conversion

Algorithm description:

This COBOL program serves as a data migration utility, transforming records from a legacy format to a modern one. It reads data from 'legacy.dat', reorders fields, potentially converts data types (e.g., to packed decimal), and writes the transformed records to 'modern.dat'. This is essential when upgrading systems or integrating with new platforms that require different data structures.

Algorithm explanation:

The program uses sequential file processing to read from `LEGACY-DATA` and write to `MODERN-DATA`. The `4000-MAP-AND-CONVERT` paragraph is the core of the migration logic. It directly moves data from fields in `LEGACY-RECORD` to corresponding fields in `MODERN-RECORD`. For example, `LEGACY-ID` is moved to `MODERN-CUSTOMER-ID`, and `LEGACY-AMOUNT` (a zoned decimal) is moved to `MODERN-TRANSACTION-AMT` (a packed decimal, `COMP-3`), which handles the type conversion. The `MODERN-STATUS-CODE` is pre-initialized. The time complexity is O(N), where N is the number of records in the legacy file, as each record is processed once. Space complexity is O(1) for working storage. Edge cases include an empty legacy file, records with unexpected data (though not explicitly handled here for simplicity), and ensuring the target fields are large enough to accommodate the migrated data. Correctness is achieved by accurate field mapping and type conversion.

Pseudocode:

INITIALIZE:
  Open legacy input file.
  Open modern output file.
  Read the first record from the legacy file.

PROCESS_RECORDS:
  Increment record count.
  MAP_AND_CONVERT the legacy record to the modern format.
  Write the modern record to the output file.
  Read the next record from the legacy file.

TERMINATE:
  Close legacy input file.
  Close modern output file.
  Display completion message and record count.

MAP_AND_CONVERT:
  Move legacy ID to modern customer ID.
  Move legacy name to modern full name.
  Move legacy amount to modern transaction amount (handling type conversion).
  Set modern status code (e.g., to 'OK').