Exercises — Chapter 12: Indexed File Processing (VSAM KSDS)
Exercise 12.1: Building a Student Records KSDS
Difficulty: Beginner
Create a COBOL program that manages a student records file using indexed organization.
Requirements:
-
Define a STUDENT-FILE with the following layout: - STUDENT-ID (PIC X(08)) — primary key - STUDENT-NAME (PIC X(30)) - STUDENT-MAJOR (PIC X(20)) - STUDENT-GPA (PIC 9V99) - STUDENT-CREDITS (PIC 9(03)) - STUDENT-STATUS (PIC X(01)) — 'A' active, 'G' graduated, 'W' withdrawn
-
Write a loader program that: - Opens the file for OUTPUT - Writes at least 10 records with varying data - Checks file status after every WRITE - Displays a count of records written - Closes the file
-
Write a separate inquiry program that: - Opens the file for INPUT - Accepts a student ID from ACCEPT statement - Reads the record by key - Displays all fields in a formatted layout - Handles the "not found" case gracefully
Deliverables: Two programs — STUD-LOAD.cbl and STUD-INQ.cbl
Exercise 12.2: Range Browsing
Difficulty: Intermediate
Extend Exercise 12.1 by writing a browse program.
Requirements:
- Accept a starting student ID and ending student ID
- Use START to position at the beginning of the range
- Use READ NEXT to iterate through the range
- Display each record found within the range
- After browsing, display: - Total records found - Average GPA of students in range - Count of active vs. graduated vs. withdrawn
Challenge: Add a "filter" option — browse only students with GPA above a user-specified threshold.
Exercise 12.3: Full CRUD Operations
Difficulty: Intermediate
Build a complete account maintenance program modeled on the ACCT-MAINT example from Section 12.9.
Requirements:
- Use the student records file from Exercise 12.1
- Read transaction commands from a sequential input file
- Support operations: - 'A' — Add new student - 'U' — Update student (change major, GPA, credits, or status) - 'D' — Withdraw student (logical delete — set status to 'W') - 'I' — Inquiry (display student record)
- Write all results to a report file
- Track counters for each operation type
- Write a summary at end of processing
Test Data: Create a transaction file with at least 15 transactions including: - 3 adds (one duplicate to test error handling) - 4 updates (one for non-existent student) - 2 deletes (one for non-existent student) - 6 inquiries
Exercise 12.4: Alternate Key Access
Difficulty: Intermediate-Advanced
Add alternate key access to the student records system.
Requirements:
- Add ALTERNATE RECORD KEY on STUDENT-MAJOR WITH DUPLICATES
-
Write a program that: - Browses all students in a user-specified major - Counts students per major - Calculates average GPA per major - Displays results sorted by major
-
Add a second alternate key on STUDENT-NAME WITH DUPLICATES
- Write a name-lookup function that finds students by name
Question to consider: What performance impact would three alternate keys have on WRITE operations? How would you evaluate the trade-off for a file with 100,000 records that gets 500 inserts/day and 10,000 lookups/day?
Exercise 12.5: Error Handling Drill
Difficulty: Intermediate
This exercise focuses on defensive programming with status codes.
Requirements:
-
Write a program that intentionally triggers each of the following status codes, then handles them: - '00' — Successful operation - '10' — End of file - '22' — Duplicate key - '23' — Record not found - '35' — File not found on OPEN
-
For each status code: - Display a descriptive message - Log the error to an error file - Continue processing (for recoverable errors) or abend gracefully (for fatal errors)
-
Create a reusable CHECK-FILE-STATUS paragraph that can be called after any I/O operation. It should accept the operation name and handle all known status codes.
Exercise 12.6: Production-Quality Loader
Difficulty: Advanced
Write a production-quality file loader that reads customer data from a sequential extract file and loads it into a VSAM KSDS.
Requirements:
- Input: Sequential file with pipe-delimited fields
- Output: VSAM KSDS customer file
-
Processing: - Validate all input fields before writing - Reject records with invalid data (write to reject file with reason) - Handle duplicate keys (write to duplicate file) - Checkpoint every 1,000 records (display progress message) - Write processing statistics at end of job
-
Validation rules: - Customer ID must be numeric - Name must not be blank - State must be a valid 2-letter code - Balance must be numeric with valid sign
Deliverables: The loader program, sample input data, expected output
Exercise 12.7: Multi-Mode Access Program
Difficulty: Advanced
Write a program that demonstrates all three access modes (SEQUENTIAL, RANDOM, DYNAMIC) by processing the same file in different ways within a single execution.
Requirements:
- Use DYNAMIC access mode
- Phase 1: Load records sequentially (WRITE in key order)
- Phase 2: Random lookups of specific records (READ with key)
- Phase 3: Browse from a starting point (START + READ NEXT)
- Phase 4: Update specific records (READ + REWRITE)
- Phase 5: Delete specific records (DELETE)
- Phase 6: Final sequential browse to display all remaining records
Display timing information (ACCEPT FROM TIME) at the start and end of each phase.
Exercise 12.8: GlobalBank Mini-Project
Difficulty: Advanced (Project)
Build a simplified version of GlobalBank's account system with the following components:
-
Account Master File (VSAM KSDS): - Account number (key), name, type, balance, status, branch
-
Programs to build: - ACCT-LOAD: Initial file loader - ACCT-INQ: Single account inquiry - ACCT-BRW: Range browse - ACCT-UPD: Balance update (deposit/withdrawal) - ACCT-RPT: Full account listing report
-
Requirements: - All programs share a COPYBOOK for the record layout - All programs include comprehensive file status checking - ACCT-UPD must validate sufficient balance for withdrawals - ACCT-RPT must include subtotals by account type
This exercise prepares you for the full banking system capstone in Chapter 43.