In this case study, we will develop a COBOL program to manage an inventory system. This system will handle basic operations such as adding new items, updating existing items, deleting items, and displaying the inventory list. This case study will help you apply the concepts learned throughout the course in a practical scenario.
Objectives
- Understand the requirements of an inventory management system.
- Design the data structures needed for the system.
- Implement file handling for storing and retrieving inventory data.
- Develop COBOL programs to perform CRUD (Create, Read, Update, Delete) operations.
- Test the system to ensure it meets the requirements.
Requirements
- Add New Item: The system should allow adding new items to the inventory.
- Update Item: The system should allow updating the details of existing items.
- Delete Item: The system should allow deleting items from the inventory.
- Display Inventory: The system should display the list of all items in the inventory.
Data Structure
We will use a sequential file to store the inventory data. Each record in the file will represent an item with the following fields:
- Item ID (PIC X(5))
- Item Name (PIC X(20))
- Quantity (PIC 9(4))
- Price (PIC 9(5)V99)
Example Record Layout
| Field | Data Type | Description |
|---|---|---|
| Item ID | X(5) | Unique identifier |
| Item Name | X(20) | Name of the item |
| Quantity | 9(4) | Quantity in stock |
| Price | 9(5)V99 | Price of the item |
Program Structure
We will create a main program that provides a menu to the user for performing different operations. Each operation will be implemented as a separate paragraph in the COBOL program.
Main Program
IDENTIFICATION DIVISION.
PROGRAM-ID. InventoryManagement.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT InventoryFile ASSIGN TO 'inventory.dat'
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-FileStatus.
DATA DIVISION.
FILE SECTION.
FD InventoryFile.
01 InventoryRecord.
05 ItemID PIC X(5).
05 ItemName PIC X(20).
05 Quantity PIC 9(4).
05 Price PIC 9(5)V99.
WORKING-STORAGE SECTION.
01 WS-FileStatus PIC XX.
01 WS-Choice PIC 9.
01 WS-Continue PIC X VALUE 'Y'.
PROCEDURE DIVISION.
Main-Menu.
PERFORM Display-Menu
ACCEPT WS-Choice
EVALUATE WS-Choice
WHEN 1
PERFORM Add-New-Item
WHEN 2
PERFORM Update-Item
WHEN 3
PERFORM Delete-Item
WHEN 4
PERFORM Display-Inventory
WHEN OTHER
DISPLAY 'Invalid choice. Please try again.'
END-EVALUATE
DISPLAY 'Do you want to continue? (Y/N)'
ACCEPT WS-Continue
IF WS-Continue = 'Y' OR WS-Continue = 'y'
GO TO Main-Menu
ELSE
STOP RUN.
Display-Menu.
DISPLAY 'Inventory Management System'
DISPLAY '1. Add New Item'
DISPLAY '2. Update Item'
DISPLAY '3. Delete Item'
DISPLAY '4. Display Inventory'
DISPLAY 'Enter your choice: '.
Add-New-Item.
OPEN OUTPUT InventoryFile
DISPLAY 'Enter Item ID: '
ACCEPT ItemID
DISPLAY 'Enter Item Name: '
ACCEPT ItemName
DISPLAY 'Enter Quantity: '
ACCEPT Quantity
DISPLAY 'Enter Price: '
ACCEPT Price
WRITE InventoryRecord
CLOSE InventoryFile
DISPLAY 'Item added successfully.'.
Update-Item.
DISPLAY 'Update Item functionality not implemented yet.'.
Delete-Item.
DISPLAY 'Delete Item functionality not implemented yet.'.
Display-Inventory.
OPEN INPUT InventoryFile
PERFORM UNTIL WS-FileStatus = '10'
READ InventoryFile
AT END
MOVE '10' TO WS-FileStatus
NOT AT END
DISPLAY 'Item ID: ' ItemID
DISPLAY 'Item Name: ' ItemName
DISPLAY 'Quantity: ' Quantity
DISPLAY 'Price: ' Price
END-PERFORM
CLOSE InventoryFile.Explanation
- Main Program: The main program provides a menu to the user and performs the selected operation.
- Display-Menu: Displays the menu options to the user.
- Add-New-Item: Prompts the user to enter the details of a new item and writes the record to the inventory file.
- Update-Item: Placeholder for the update item functionality.
- Delete-Item: Placeholder for the delete item functionality.
- Display-Inventory: Reads and displays all records from the inventory file.
Exercises
- Implement Update-Item: Modify the program to allow updating the details of an existing item.
- Implement Delete-Item: Modify the program to allow deleting an item from the inventory.
- Enhance Display-Inventory: Format the output to display the inventory in a tabular format.
Exercise Solutions
Implement Update-Item
Update-Item.
OPEN I-O InventoryFile
DISPLAY 'Enter Item ID to update: '
ACCEPT ItemID
PERFORM UNTIL WS-FileStatus = '10'
READ InventoryFile
AT END
MOVE '10' TO WS-FileStatus
NOT AT END
IF ItemID = InventoryRecord-ItemID
DISPLAY 'Enter new Item Name: '
ACCEPT ItemName
DISPLAY 'Enter new Quantity: '
ACCEPT Quantity
DISPLAY 'Enter new Price: '
ACCEPT Price
REWRITE InventoryRecord
DISPLAY 'Item updated successfully.'
MOVE '10' TO WS-FileStatus
END-IF
END-PERFORM
CLOSE InventoryFile.Implement Delete-Item
Delete-Item.
OPEN I-O InventoryFile
DISPLAY 'Enter Item ID to delete: '
ACCEPT ItemID
PERFORM UNTIL WS-FileStatus = '10'
READ InventoryFile
AT END
MOVE '10' TO WS-FileStatus
NOT AT END
IF ItemID = InventoryRecord-ItemID
DELETE InventoryRecord
DISPLAY 'Item deleted successfully.'
MOVE '10' TO WS-FileStatus
END-IF
END-PERFORM
CLOSE InventoryFile.Enhance Display-Inventory
Display-Inventory.
OPEN INPUT InventoryFile
DISPLAY 'Item ID Item Name Quantity Price'
DISPLAY '----------------------------------------------'
PERFORM UNTIL WS-FileStatus = '10'
READ InventoryFile
AT END
MOVE '10' TO WS-FileStatus
NOT AT END
DISPLAY ItemID ' ' ItemName ' ' Quantity ' ' Price
END-PERFORM
CLOSE InventoryFile.Conclusion
In this case study, we developed a basic inventory management system using COBOL. We covered the essential operations such as adding, updating, deleting, and displaying inventory items. By completing the exercises, you will gain hands-on experience in implementing and enhancing COBOL programs for real-world applications. This case study serves as a practical application of the concepts learned throughout the course and prepares you for more complex COBOL programming tasks.
