' Employee database: search, sort by field, display
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM empId(1 TO 5) AS LONG
    DIM empFirst(1 TO 5) AS STRING
    DIM empLast(1 TO 5) AS STRING
    DIM empDept(1 TO 5) AS STRING
    DIM empSal(1 TO 5) AS LONG
    DIM empYear(1 TO 5) AS INTEGER
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL tmpId AS LONG
    LOCAL tmpSal AS LONG
    LOCAL tmpYear AS INTEGER
    LOCAL tmpFirst AS STRING
    LOCAL tmpLast AS STRING
    LOCAL tmpDept AS STRING
    LOCAL first AS STRING
    LOCAL last AS STRING
    LOCAL dpt AS STRING
    PRINT "=== Employee Database ==="
    PRINT
    empId(1) = 101
    empFirst(1) = "Alice"
    empLast(1) = "Smith"
    empDept(1) = "Engineering"
    empSal(1) = 85000
    empYear(1) = 2019
    empId(2) = 102
    empFirst(2) = "Bob"
    empLast(2) = "Johnson"
    empDept(2) = "Marketing"
    empSal(2) = 72000
    empYear(2) = 2020
    empId(3) = 103
    empFirst(3) = "Carol"
    empLast(3) = "Williams"
    empDept(3) = "Engineering"
    empSal(3) = 95000
    empYear(3) = 2018
    empId(4) = 104
    empFirst(4) = "David"
    empLast(4) = "Brown"
    empDept(4) = "Sales"
    empSal(4) = 68000
    empYear(4) = 2021
    empId(5) = 105
    empFirst(5) = "Eve"
    empLast(5) = "Davis"
    empDept(5) = "Engineering"
    empSal(5) = 88000
    empYear(5) = 2020
    PRINT "Employees (" & FORMAT$(5) & "):"
    FOR i = 1 TO 5
        last = TRIM$(empLast(i))
        first = TRIM$(empFirst(i))
        dpt = TRIM$(empDept(i))
        PRINT "  " & FORMAT$(empId(i)) & " " & last & ", " & first & "  " & dpt & " $" & FORMAT$(empSal(i)) & " " & FORMAT$(empYear(i))
    NEXT i
    PRINT
    PRINT "Find employee 103:"
    FOR i = 1 TO 5
        IF empId(i) = 103 THEN
            first = TRIM$(empFirst(i))
            last = TRIM$(empLast(i))
            dpt = TRIM$(empDept(i))
            PRINT "  " & first & " " & last & " - " & dpt
        END IF
    NEXT i
    PRINT
    PRINT "Engineering dept:"
    FOR i = 1 TO 5
        dpt = TRIM$(empDept(i))
        IF dpt = "Engineering" THEN
            last = TRIM$(empLast(i))
            first = TRIM$(empFirst(i))
            PRINT "  " & last & ", " & first
        END IF
    NEXT i
    PRINT
    PRINT "Sort by salary:"
    FOR i = 1 TO 4
        FOR j = i + 1 TO 5
            IF empSal(i) > empSal(j) THEN
                tmpId = empId(i)
                empId(i) = empId(j)
                empId(j) = tmpId
                tmpSal = empSal(i)
                empSal(i) = empSal(j)
                empSal(j) = tmpSal
                tmpYear = empYear(i)
                empYear(i) = empYear(j)
                empYear(j) = tmpYear
            END IF
        NEXT j
    NEXT i
    FOR i = 1 TO 5
        last = TRIM$(empLast(i))
        PRINT "  $" & FORMAT$(empSal(i)) & " " & last
    NEXT i
    PRINT
    PRINT "Employee DB tests complete."
    FUNCTION = 0
END FUNCTION
