' Person record CRUD — parallel arrays for name, address, phone fields
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numPeople  AS LONG
    LOCAL searchId  AS LONG
    LOCAL foundIdx  AS LONG
    LOCAL personId(20) AS LONG
    LOCAL personFirst(20) AS STRING
    LOCAL personLast(20) AS STRING
    LOCAL personStreet(20) AS STRING
    LOCAL personCity(20) AS STRING
    LOCAL personZip(20) AS STRING
    LOCAL personPhone(20) AS STRING
    LOCAL personAge(20) AS LONG
    LOCAL fn  AS STRING
    LOCAL ln  AS STRING
    LOCAL st  AS STRING
    LOCAL ct  AS STRING
    LOCAL zp  AS STRING
    LOCAL ph  AS STRING
    PRINT "=== Person Record CRUD Operations ==="
    PRINT

    numPeople = 6
    personId(0) = 1
    personFirst(0) = "Alice             "
    personLast(0) = "Johnson           "
    personStreet(0) = "123 Main Street                   "
    personCity(0) = "New York            "
    personZip(0) = "10001     "
    personPhone(0) = "212-555-0101 "
    personAge(0) = 32

    personId(1) = 2
    personFirst(1) = "Bob               "
    personLast(1) = "Smith             "
    personStreet(1) = "456 Oak Avenue                     "
    personCity(1) = "Los Angeles         "
    personZip(1) = "90001     "
    personPhone(1) = "310-555-0202 "
    personAge(1) = 45

    personId(2) = 3
    personFirst(2) = "Carol             "
    personLast(2) = "Williams          "
    personStreet(2) = "789 Pine Road                      "
    personCity(2) = "Chicago             "
    personZip(2) = "60601     "
    personPhone(2) = "312-555-0303 "
    personAge(2) = 28

    personId(3) = 4
    personFirst(3) = "David             "
    personLast(3) = "Brown             "
    personStreet(3) = "321 Elm Street                     "
    personCity(3) = "Houston             "
    personZip(3) = "77001     "
    personPhone(3) = "713-555-0404 "
    personAge(3) = 51

    personId(4) = 5
    personFirst(4) = "Eve               "
    personLast(4) = "Davis             "
    personStreet(4) = "654 Maple Lane                     "
    personCity(4) = "Phoenix             "
    personZip(4) = "85001     "
    personPhone(4) = "602-555-0505 "
    personAge(4) = 37

    personId(5) = 6
    personFirst(5) = "Frank             "
    personLast(5) = "Miller            "
    personStreet(5) = "987 Cedar Court                    "
    personCity(5) = "Philadelphia        "
    personZip(5) = "19101     "
    personPhone(5) = "215-555-0606 "
    personAge(5) = 29

    PRINT "--- READ: List all people ---"
    FOR i = 0 TO numPeople - 1
        fn = TRIM$(personFirst(i))
        ln = TRIM$(personLast(i))
        PRINT "ID" & STR$(personId(i)) & ": " & fn & " " & ln & ", " & STR$(personAge(i)) & " yrs"
    NEXT i
    PRINT

    PRINT "--- READ: Search by ID ---"
    searchId = 3
    foundIdx = -1
    FOR i = 0 TO numPeople - 1
        IF personId(i) = searchId THEN
            foundIdx = i
        END IF
    NEXT i
    IF foundIdx >= 0 THEN
        fn = TRIM$(personFirst(foundIdx))
        ln = TRIM$(personLast(foundIdx))
        st = TRIM$(personStreet(foundIdx))
        ct = TRIM$(personCity(foundIdx))
        zp = TRIM$(personZip(foundIdx))
        ph = TRIM$(personPhone(foundIdx))
        PRINT "Found ID " & STR$(searchId) & ": " & fn & " " & ln
        PRINT "  " & st
        PRINT "  " & ct & ", " & zp
        PRINT "  Phone: " & ph
    ELSE
        PRINT "ID " & STR$(searchId) & " not found"
    END IF
    PRINT

    PRINT "--- UPDATE: Change Bob Smith's age to 46 ---"
    FOR i = 0 TO numPeople - 1
        IF personId(i) = 2 THEN
            personAge(i) = 46
            fn = TRIM$(personFirst(i))
            PRINT "Updated: " & fn & " age now " & STR$(personAge(i))
        END IF
    NEXT i
    PRINT

    PRINT "--- UPDATE: Change Carol's phone ---"
    FOR i = 0 TO numPeople - 1
        IF personId(i) = 3 THEN
            personPhone(i) = "312-555-9999 "
            fn = TRIM$(personFirst(i))
            PRINT "Updated: " & fn & " phone changed"
        END IF
    NEXT i
    PRINT

    PRINT "--- DELETE: Remove Frank (ID 6) ---"
    FOR i = 0 TO numPeople - 1
        IF personId(i) = 6 THEN
            FOR j = i TO numPeople - 2
                personId(j) = personId(j + 1)
                personFirst(j) = personFirst(j + 1)
                personLast(j) = personLast(j + 1)
                personStreet(j) = personStreet(j + 1)
                personCity(j) = personCity(j + 1)
                personZip(j) = personZip(j + 1)
                personPhone(j) = personPhone(j + 1)
                personAge(j) = personAge(j + 1)
            NEXT j
            numPeople = numPeople - 1
        END IF
    NEXT i
    PRINT "Frank deleted. Remaining count:" & STR$(numPeople)
    PRINT

    PRINT "--- CREATE: Add Grace (new person) ---"
    personId(numPeople) = 7
    personFirst(numPeople) = "Grace             "
    personLast(numPeople) = "Wilson            "
    personStreet(numPeople) = "135 Birch Drive                    "
    personCity(numPeople) = "San Antonio         "
    personZip(numPeople) = "78201     "
    personPhone(numPeople) = "210-555-0707 "
    personAge(numPeople) = 41
    numPeople = numPeople + 1
    PRINT "Added Grace Wilson. Total:" & STR$(numPeople)
    PRINT

    PRINT "--- Final list ---"
    FOR i = 0 TO numPeople - 1
        fn = TRIM$(personFirst(i))
        ln = TRIM$(personLast(i))
        ph = TRIM$(personPhone(i))
        PRINT "ID" & STR$(personId(i)) & ": " & fn & " " & ln & " | " & ph & " | Age " & STR$(personAge(i))
    NEXT i

    PRINT
    PRINT "Person CRUD operations complete."

    FUNCTION = 0
END FUNCTION
