In this case study, we will build a simple Inventory Management System using RPG. This project will help you understand how to apply the concepts learned in previous modules to a real-world application. We will cover the following steps:

  1. Requirements Analysis
  2. Database Design
  3. Program Design
  4. Implementation
  5. Testing

  1. Requirements Analysis

Before we start coding, we need to understand the requirements of our Inventory Management System. Here are the key features we want to implement:

  • Add New Items: Ability to add new items to the inventory.
  • Update Item Information: Ability to update existing item details.
  • Delete Items: Ability to remove items from the inventory.
  • View Inventory: Ability to view all items in the inventory.

  1. Database Design

We will use a simple database table to store our inventory items. The table will have the following structure:

Field Name Data Type Description
ITEM_ID CHAR(10) Unique identifier for the item
ITEM_NAME CHAR(50) Name of the item
QUANTITY INT Quantity of the item in stock
PRICE DEC(7,2) Price of the item

  1. Program Design

We will create a modular program with the following procedures:

  • AddItem: Adds a new item to the inventory.
  • UpdateItem: Updates the details of an existing item.
  • DeleteItem: Deletes an item from the inventory.
  • ViewInventory: Displays all items in the inventory.

  1. Implementation

4.1 AddItem Procedure

// AddItem Procedure
P AddItem         B
D AddItem         PI
D   itemId        CHAR(10)
D   itemName      CHAR(50)
D   quantity      INT(10)
D   price         DEC(7,2)

D sqlStmt         S   CHAR(200)

 /free
    sqlStmt = 'INSERT INTO INVENTORY (ITEM_ID, ITEM_NAME, QUANTITY, PRICE) ' +
              'VALUES (' + itemId + ', ' + itemName + ', ' + %char(quantity) + ', ' + %char(price) + ')';
    EXEC SQL EXECUTE IMMEDIATE :sqlStmt;
 /end-free
P AddItem         E

4.2 UpdateItem Procedure

// UpdateItem Procedure
P UpdateItem      B
D UpdateItem      PI
D   itemId        CHAR(10)
D   itemName      CHAR(50)
D   quantity      INT(10)
D   price         DEC(7,2)

D sqlStmt         S   CHAR(200)

 /free
    sqlStmt = 'UPDATE INVENTORY SET ITEM_NAME = ' + itemName + ', QUANTITY = ' + %char(quantity) + ', PRICE = ' + %char(price) +
              ' WHERE ITEM_ID = ' + itemId;
    EXEC SQL EXECUTE IMMEDIATE :sqlStmt;
 /end-free
P UpdateItem      E

4.3 DeleteItem Procedure

// DeleteItem Procedure
P DeleteItem      B
D DeleteItem      PI
D   itemId        CHAR(10)

D sqlStmt         S   CHAR(200)

 /free
    sqlStmt = 'DELETE FROM INVENTORY WHERE ITEM_ID = ' + itemId;
    EXEC SQL EXECUTE IMMEDIATE :sqlStmt;
 /end-free
P DeleteItem      E

4.4 ViewInventory Procedure

// ViewInventory Procedure
P ViewInventory   B
D ViewInventory   PI

D sqlStmt         S   CHAR(200)
D itemId          S   CHAR(10)
D itemName        S   CHAR(50)
D quantity        S   INT(10)
D price           S   DEC(7,2)

 /free
    sqlStmt = 'SELECT ITEM_ID, ITEM_NAME, QUANTITY, PRICE FROM INVENTORY';
    EXEC SQL DECLARE C1 CURSOR FOR :sqlStmt;
    EXEC SQL OPEN C1;

    DOU SQLCOD <> 0;
        EXEC SQL FETCH C1 INTO :itemId, :itemName, :quantity, :price;
        if SQLCOD = 0;
            dsply ('Item ID: ' + itemId + ' Name: ' + itemName + ' Quantity: ' + %char(quantity) + ' Price: ' + %char(price));
        endif;
    ENDDO;

    EXEC SQL CLOSE C1;
 /end-free
P ViewInventory   E

  1. Testing

To ensure our Inventory Management System works correctly, we need to test each procedure:

  1. AddItem: Test by adding new items and verifying they appear in the database.
  2. UpdateItem: Test by updating existing items and verifying the changes.
  3. DeleteItem: Test by deleting items and ensuring they are removed from the database.
  4. ViewInventory: Test by viewing the inventory and ensuring all items are displayed correctly.

Conclusion

In this case study, we designed and implemented a simple Inventory Management System using RPG. We covered the entire process from requirements analysis to testing. This project demonstrates how to apply RPG programming concepts to build a functional application. In the next section, we will explore another real-world application to further solidify your understanding of RPG programming.

© Copyright 2024. All rights reserved