' Inventory items — parallel arrays for quantity, price, category, sorting
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numItems  AS LONG
    DIM itemSKU(20) AS LONG
    DIM itemName(20) AS STRING
    DIM itemCat(20) AS LONG
    DIM itemQty(20) AS LONG
    DIM itemPrice(20) AS DOUBLE
    DIM itemReorder(20) AS LONG
    LOCAL s AS STRING
    LOCAL catName AS STRING
    LOCAL totalValue AS DOUBLE
    LOCAL avgPrice AS DOUBLE
    LOCAL lowStockCount AS LONG
    LOCAL catCount AS LONG
    LOCAL tmpSKU  AS LONG
    LOCAL tmpCat  AS LONG
    LOCAL tmpQty  AS LONG
    LOCAL tmpReorder  AS LONG
    LOCAL tmpName AS STRING
    LOCAL tmpPrice AS DOUBLE

    PRINT "=== Inventory Item Management ==="
    PRINT

    numItems = 8
    itemSKU(0) = 1001
    itemName(0) = "Widget Alpha           "
    itemCat(0) = 1
    itemQty(0) = 150
    itemPrice(0) = 12.99
    itemReorder(0) = 20

    itemSKU(1) = 1002
    itemName(1) = "Gadget Beta            "
    itemCat(1) = 2
    itemQty(1) = 45
    itemPrice(1) = 24.50
    itemReorder(1) = 15

    itemSKU(2) = 1003
    itemName(2) = "Bolt M8x20             "
    itemCat(2) = 3
    itemQty(2) = 500
    itemPrice(2) = 0.35
    itemReorder(2) = 100

    itemSKU(3) = 1004
    itemName(3) = "Sensor Module          "
    itemCat(3) = 2
    itemQty(3) = 12
    itemPrice(3) = 89.99
    itemReorder(3) = 10

    itemSKU(4) = 1005
    itemName(4) = "Steel Bracket          "
    itemCat(4) = 3
    itemQty(4) = 200
    itemPrice(4) = 3.75
    itemReorder(4) = 50

    itemSKU(5) = 1006
    itemName(5) = "Control Board          "
    itemCat(5) = 2
    itemQty(5) = 8
    itemPrice(5) = 145.00
    itemReorder(5) = 5

    itemSKU(6) = 1007
    itemName(6) = "Rubber Seal            "
    itemCat(6) = 3
    itemQty(6) = 1000
    itemPrice(6) = 0.89
    itemReorder(6) = 200

    itemSKU(7) = 1008
    itemName(7) = "Display Unit           "
    itemCat(7) = 2
    itemQty(7) = 25
    itemPrice(7) = 199.00
    itemReorder(7) = 5

    PRINT "--- Initial Inventory ---"
    totalValue = 0.0
    FOR i = 0 TO numItems - 1
        IF itemCat(i) = 1 THEN catName = "Raw Material"
        IF itemCat(i) = 2 THEN catName = "Electronics "
        IF itemCat(i) = 3 THEN catName = "Hardware    "
        s = TRIM$(itemName(i))
        PRINT "SKU" & STR$(itemSKU(i)) & ": " & s & " [" & catName & "] qty:" & STR$(itemQty(i)) & " @$" & FORMAT$(itemPrice(i))
        totalValue = totalValue + itemPrice(i) * itemQty(i)
    NEXT i
    PRINT "Total value: $" & FORMAT$(totalValue)
    PRINT

    PRINT "--- Sort by Price (ascending) ---"
    FOR i = 0 TO numItems - 2
        FOR j = 0 TO numItems - 2 - i
            IF itemPrice(j) > itemPrice(j + 1) THEN
                tmpSKU = itemSKU(j)
                tmpName = TRIM$(itemName(j))
                tmpCat = itemCat(j)
                tmpQty = itemQty(j)
                tmpPrice = itemPrice(j)
                tmpReorder = itemReorder(j)
                itemSKU(j) = itemSKU(j + 1)
                itemName(j) = itemName(j + 1)
                itemCat(j) = itemCat(j + 1)
                itemQty(j) = itemQty(j + 1)
                itemPrice(j) = itemPrice(j + 1)
                itemReorder(j) = itemReorder(j + 1)
                itemSKU(j + 1) = tmpSKU
                itemName(j + 1) = tmpName
                itemCat(j + 1) = tmpCat
                itemQty(j + 1) = tmpQty
                itemPrice(j + 1) = tmpPrice
                itemReorder(j + 1) = tmpReorder
            END IF
        NEXT j
    NEXT i
    FOR i = 0 TO numItems - 1
        s = TRIM$(itemName(i))
        PRINT "  $" & FORMAT$(itemPrice(i)) & " - " & s & " (SKU" & STR$(itemSKU(i)) & ")"
    NEXT i
    PRINT

    PRINT "--- Sort by Quantity (descending) ---"
    FOR i = 0 TO numItems - 2
        FOR j = 0 TO numItems - 2 - i
            IF itemQty(j) < itemQty(j + 1) THEN
                tmpSKU = itemSKU(j)
                tmpName = TRIM$(itemName(j))
                tmpCat = itemCat(j)
                tmpQty = itemQty(j)
                tmpPrice = itemPrice(j)
                tmpReorder = itemReorder(j)
                itemSKU(j) = itemSKU(j + 1)
                itemName(j) = itemName(j + 1)
                itemCat(j) = itemCat(j + 1)
                itemQty(j) = itemQty(j + 1)
                itemPrice(j) = itemPrice(j + 1)
                itemReorder(j) = itemReorder(j + 1)
                itemSKU(j + 1) = tmpSKU
                itemName(j + 1) = tmpName
                itemCat(j + 1) = tmpCat
                itemQty(j + 1) = tmpQty
                itemPrice(j + 1) = tmpPrice
                itemReorder(j + 1) = tmpReorder
            END IF
        NEXT j
    NEXT i
    FOR i = 0 TO numItems - 1
        s = TRIM$(itemName(i))
        PRINT "  " & STR$(itemQty(i)) & " x " & s & " (SKU" & STR$(itemSKU(i)) & ")"
    NEXT i
    PRINT

    PRINT "--- Low Stock Alert ---"
    lowStockCount = 0
    FOR i = 0 TO numItems - 1
        IF itemQty(i) <= itemReorder(i) THEN
            s = TRIM$(itemName(i))
            PRINT "  LOW: " & s & " (qty:" & STR$(itemQty(i)) & ", reorder at:" & STR$(itemReorder(i)) & ")"
            lowStockCount = lowStockCount + 1
        END IF
    NEXT i
    IF lowStockCount = 0 THEN
        PRINT "  All items above reorder point"
    END IF
    PRINT

    PRINT "--- Category Counts ---"
    catCount = 0
    FOR i = 0 TO numItems - 1
        IF itemCat(i) = 1 THEN catCount = catCount + 1
    NEXT i
    PRINT "  Raw Material:" & STR$(catCount)
    catCount = 0
    FOR i = 0 TO numItems - 1
        IF itemCat(i) = 2 THEN catCount = catCount + 1
    NEXT i
    PRINT "  Electronics:" & STR$(catCount)
    catCount = 0
    FOR i = 0 TO numItems - 1
        IF itemCat(i) = 3 THEN catCount = catCount + 1
    NEXT i
    PRINT "  Hardware:" & STR$(catCount)

    avgPrice = totalValue / numItems
    PRINT
    PRINT "Average price: $" & FORMAT$(avgPrice)
    PRINT "Inventory management complete."

    FUNCTION = 0
END FUNCTION
