Skip to content
LumenaryAcademy
Professional 75 min

Project: a weekly payroll report

A complete batch program in professional style, from specification to balanced control totals, and where to go next.

Code tested · 2AI-draftedAwaiting review by a computer scientist
MotivationH

The specification

Every lesson so far has taught one part of the language. This one puts them together the way a working COBOL programmer would: starting from a specification written by the business, designing the data and the program structure, writing the code, and showing that the run was complete and correct.

Here is the specification, in the form a payroll department might hand it over:

Weekly payroll run. Each week the time-recording system delivers a file of timesheets, one record per employee: employee number (5 characters), name (18), department (8), hourly rate (5 digits, 2 of them decimals) and hours worked (3 digits, 1 decimal). For each valid timesheet, compute:

  • Gross pay: hours up to 40 at the hourly rate; hours above 40 at 1.5 times the rate.
  • Tax: 10% of the first 600.00 of gross pay and 22% of the rest.
  • Pension: 5% of gross pay.
  • Net pay: gross minus tax minus pension.

Round each amount to the cent, half away from zero. Reject, with a reason, any timesheet whose rate or hours are not numeric, whose hours exceed 80.0 or whose rate exceeds 150.00. Print a report in department order, by name within department, with a subtotal per department and the run's control totals: records read, accepted, rejected and paid, and total gross, tax, pension and net. The run must show that every record is accounted for and that net + tax + pension = gross.

The tax scale is invented for this project; real ones are longer and change every year, which is exactly why the rules belong in one subprogram. Everything else is typical: fixed-width records, decimal money, validation, a sort, control breaks and totals that must balance.

IntuitionH

The design

Before writing code, a COBOL programmer designs the data and the flow.

The data comes straight from the specification. The timesheet record is 39 characters: X(5), X(18), X(8), 9(3)V99, 99V9. The sort record holds the same fields with the sort keys (department, then name) first. Money in working storage is S9(7)V99 per employee and S9(9)V99 for totals, with room to spare. The tax rules, rates and limits are named constants, not numbers buried in formulas.

The flow is one SORT statement with two procedures around it:

timesheetsfile1000-validateINPUTPROCEDURESORTdept, name2000-printOUTPUT PROC.CALL calc-payreportREJECTED linescontrol totals
The payroll run. The input procedure validates and releases good records to the sort; the output procedure receives them in department order, calls calc-pay and prints. Rejected records and control totals are printed too.
  • 1000-validate-timesheets, the input procedure, reads the timesheets, checks each one, and RELEASEs the valid ones into the sort. Invalid ones are reported and counted; they never reach the arithmetic.
  • SORT orders the released records by department and name.
  • 2000-print-payroll, the output procedure, RETURNs the sorted records one by one, calls the pay subprogram, prints detail lines, and handles the department control break.
  • calc-pay is a separate program holding every pay rule. When the tax scale changes, only it changes, and it can be tested on its own.
  • 3000-print-control-totals prints the counts and totals and checks that they balance.

Because the program must run anywhere, its first paragraph writes eight sample timesheets, two of them deliberately bad. In production that paragraph would not exist: the file would arrive from another system.

ComputationC

The program

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. weekly-payroll.
*> Reads a week's timesheets, validates them, sorts them by department,
*> computes pay through the calc-pay subprogram and prints a payroll
*> report with department subtotals and balancing control totals.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT timesheet-file ASSIGN TO "timesheets.dat"
        ORGANIZATION IS LINE SEQUENTIAL
        FILE STATUS IS ws-ts-status.
    SELECT sort-file ASSIGN TO "payroll-sort.tmp".
DATA DIVISION.
FILE SECTION.
FD timesheet-file.
01 ts-record.
   05 ts-emp-id             PIC X(5).
   05 ts-name               PIC X(18).
   05 ts-dept               PIC X(8).
   05 ts-rate               PIC 9(3)V99.
   05 ts-hours              PIC 99V9.
SD sort-file.
01 sort-record.
   05 sr-dept               PIC X(8).
   05 sr-name               PIC X(18).
   05 sr-emp-id             PIC X(5).
   05 sr-rate               PIC 9(3)V99.
   05 sr-hours              PIC 99V9.
WORKING-STORAGE SECTION.
01 ws-ts-status             PIC XX.
01 ws-flags.
   05 ws-ts-eof             PIC X VALUE "N".
      88 end-of-timesheets        VALUE "Y".
   05 ws-sort-eof           PIC X VALUE "N".
      88 end-of-sorted            VALUE "Y".
   05 ws-valid-flag         PIC X.
      88 record-is-valid          VALUE "Y".
      88 record-is-invalid        VALUE "N".
01 ws-reject-reason         PIC X(26).
01 ws-limits.
   05 ws-max-hours          PIC 99V9   VALUE 80.0.
   05 ws-max-rate           PIC 9(3)V99 VALUE 150.00.
01 ws-pay.
   05 ws-gross              PIC S9(7)V99.
   05 ws-tax                PIC S9(7)V99.
   05 ws-pension            PIC S9(7)V99.
   05 ws-net                PIC S9(7)V99.
01 ws-counts.
   05 ws-read-count         PIC 9(5) VALUE 0.
   05 ws-accepted-count     PIC 9(5) VALUE 0.
   05 ws-rejected-count     PIC 9(5) VALUE 0.
   05 ws-paid-count         PIC 9(5) VALUE 0.
01 ws-current-dept          PIC X(8).
01 ws-dept-totals.
   05 ws-dept-gross         PIC S9(9)V99 VALUE 0.
   05 ws-dept-net           PIC S9(9)V99 VALUE 0.
01 ws-grand-totals.
   05 ws-total-gross        PIC S9(9)V99 VALUE 0.
   05 ws-total-tax          PIC S9(9)V99 VALUE 0.
   05 ws-total-pension      PIC S9(9)V99 VALUE 0.
   05 ws-total-net          PIC S9(9)V99 VALUE 0.
01 ws-check-sum             PIC S9(9)V99.
01 heading-line-1.
   05 FILLER                PIC X(40) VALUE "WEEKLY PAYROLL".
   05 FILLER                PIC X(30) VALUE "               WEEK 47 OF 2026".
01 heading-line-2.
   05 FILLER                PIC X(9)  VALUE "DEPT".
   05 FILLER                PIC X(19) VALUE "EMPLOYEE".
   05 FILLER                PIC X(6)  VALUE "HOURS".
   05 FILLER                PIC X(36) VALUE
          "    GROSS      TAX  PENSION      NET".
01 detail-line.
   05 dl-dept               PIC X(9).
   05 dl-name               PIC X(19).
   05 dl-hours              PIC ZZ9.9.
   05 FILLER                PIC X     VALUE SPACE.
   05 dl-gross              PIC ZZ,ZZ9.99.
   05 dl-tax                PIC ZZ,ZZ9.99.
   05 dl-pension            PIC ZZ,ZZ9.99.
   05 dl-net                PIC ZZ,ZZ9.99.
01 dept-total-line.
   05 FILLER                PIC X(2)  VALUE SPACES.
   05 dt-label              PIC X(31).
   05 dt-gross              PIC ZZZ,ZZ9.99.
   05 FILLER                PIC X(17) VALUE SPACES.
   05 dt-net                PIC ZZZ,ZZ9.99.
01 total-line.
   05 tl-label              PIC X(30).
   05 tl-amount             PIC ZZZ,ZZ9.99.
01 count-line.
   05 cl-label              PIC X(30).
   05 cl-count              PIC ZZ,ZZ9.

PROCEDURE DIVISION.
0000-main.
    PERFORM 0100-create-sample-timesheets
    SORT sort-file
        ON ASCENDING KEY sr-dept sr-name
        INPUT PROCEDURE  1000-validate-timesheets
        OUTPUT PROCEDURE 2000-print-payroll
    PERFORM 3000-print-control-totals
    STOP RUN.

*> Test data. In production this file arrives from the time-recording
*> system; here the program writes it so that it can run anywhere.
0100-create-sample-timesheets.
    OPEN OUTPUT timesheet-file
    MOVE "10001Amina Yusuf       Kitchen 01850400" TO ts-record
    WRITE ts-record
    MOVE "10002Kenji Watanabe    Front   02200385" TO ts-record
    WRITE ts-record
    MOVE "10003Lucia Fernandez   Kitchen 02475460" TO ts-record
    WRITE ts-record
    MOVE "10004Olu Adeyemi       Delivery01600525" TO ts-record
    WRITE ts-record
    MOVE "10005Priya Raman       Front   02740400" TO ts-record
    WRITE ts-record
    MOVE "10006Mateus Costa      Delivery016003X0" TO ts-record
    WRITE ts-record
    MOVE "10007Hana Kim          Kitchen 01925320" TO ts-record
    WRITE ts-record
    MOVE "10008Sofia Rossi       Front   02110950" TO ts-record
    WRITE ts-record
    CLOSE timesheet-file.

*> SORT input procedure: read every timesheet, release the valid ones.
1000-validate-timesheets.
    OPEN INPUT timesheet-file
    IF ws-ts-status NOT = "00"
        DISPLAY "cannot open timesheets, status " ws-ts-status
        MOVE 16 TO RETURN-CODE
        STOP RUN
    END-IF
    PERFORM 1100-read-timesheet
    PERFORM UNTIL end-of-timesheets
        PERFORM 1200-check-timesheet
        IF record-is-valid
            MOVE ts-dept    TO sr-dept
            MOVE ts-name    TO sr-name
            MOVE ts-emp-id  TO sr-emp-id
            MOVE ts-rate    TO sr-rate
            MOVE ts-hours   TO sr-hours
            RELEASE sort-record
            ADD 1 TO ws-accepted-count
        ELSE
            DISPLAY "REJECTED " ts-emp-id " " ts-name " "
                    FUNCTION TRIM(ws-reject-reason)
            ADD 1 TO ws-rejected-count
        END-IF
        PERFORM 1100-read-timesheet
    END-PERFORM
    CLOSE timesheet-file.

1100-read-timesheet.
    READ timesheet-file
        AT END SET end-of-timesheets TO TRUE
        NOT AT END ADD 1 TO ws-read-count
    END-READ.

1200-check-timesheet.
    SET record-is-valid TO TRUE
    EVALUATE TRUE
        WHEN ts-rate IS NOT NUMERIC OR ts-hours IS NOT NUMERIC
            SET record-is-invalid TO TRUE
            MOVE "hours or rate not numeric" TO ws-reject-reason
        WHEN ts-hours > ws-max-hours
            SET record-is-invalid TO TRUE
            MOVE "hours over weekly limit" TO ws-reject-reason
        WHEN ts-rate > ws-max-rate
            SET record-is-invalid TO TRUE
            MOVE "rate over limit" TO ws-reject-reason
    END-EVALUATE.

*> SORT output procedure: receive records in department order and print.
2000-print-payroll.
    DISPLAY " "
    DISPLAY heading-line-1
    DISPLAY heading-line-2
    PERFORM 2100-return-sorted
    IF NOT end-of-sorted
        MOVE sr-dept TO ws-current-dept
    END-IF
    PERFORM UNTIL end-of-sorted
        IF sr-dept NOT = ws-current-dept
            PERFORM 2300-department-break
        END-IF
        PERFORM 2200-pay-one-employee
        PERFORM 2100-return-sorted
    END-PERFORM
    IF ws-paid-count > 0
        PERFORM 2300-department-break
    END-IF.

2100-return-sorted.
    RETURN sort-file
        AT END SET end-of-sorted TO TRUE
    END-RETURN.

2200-pay-one-employee.
    CALL "calc-pay" USING sr-rate sr-hours ws-pay
    ADD 1 TO ws-paid-count
    ADD ws-gross   TO ws-dept-gross  ws-total-gross
    ADD ws-net     TO ws-dept-net    ws-total-net
    ADD ws-tax     TO ws-total-tax
    ADD ws-pension TO ws-total-pension
    MOVE sr-dept    TO dl-dept
    MOVE sr-name    TO dl-name
    MOVE sr-hours   TO dl-hours
    MOVE ws-gross   TO dl-gross
    MOVE ws-tax     TO dl-tax
    MOVE ws-pension TO dl-pension
    MOVE ws-net     TO dl-net
    DISPLAY detail-line.

2300-department-break.
    MOVE SPACES TO dt-label
    STRING "Total " DELIMITED BY SIZE
           ws-current-dept DELIMITED BY SPACE
        INTO dt-label
    END-STRING
    MOVE ws-dept-gross TO dt-gross
    MOVE ws-dept-net   TO dt-net
    DISPLAY dept-total-line
    INITIALIZE ws-dept-totals
    MOVE sr-dept TO ws-current-dept.

3000-print-control-totals.
    DISPLAY " "
    MOVE "Records read"      TO cl-label  MOVE ws-read-count     TO cl-count
    DISPLAY count-line
    MOVE "Records accepted"  TO cl-label  MOVE ws-accepted-count TO cl-count
    DISPLAY count-line
    MOVE "Records rejected"  TO cl-label  MOVE ws-rejected-count TO cl-count
    DISPLAY count-line
    MOVE "Employees paid"    TO cl-label  MOVE ws-paid-count     TO cl-count
    DISPLAY count-line
    MOVE "Total gross"       TO tl-label  MOVE ws-total-gross    TO tl-amount
    DISPLAY total-line
    MOVE "Total tax"         TO tl-label  MOVE ws-total-tax      TO tl-amount
    DISPLAY total-line
    MOVE "Total pension"     TO tl-label  MOVE ws-total-pension  TO tl-amount
    DISPLAY total-line
    MOVE "Total net pay"     TO tl-label  MOVE ws-total-net      TO tl-amount
    DISPLAY total-line
    COMPUTE ws-check-sum = ws-total-net + ws-total-tax + ws-total-pension
    IF ws-check-sum = ws-total-gross
       AND ws-read-count = ws-accepted-count + ws-rejected-count
       AND ws-accepted-count = ws-paid-count
        DISPLAY "Control totals balance."
    ELSE
        DISPLAY "CONTROL TOTALS DO NOT BALANCE"
        MOVE 8 TO RETURN-CODE
    END-IF.
END PROGRAM weekly-payroll.

IDENTIFICATION DIVISION.
PROGRAM-ID. calc-pay.
*> Weekly pay for one employee. Overtime above 40 hours is paid at
*> one and a half times the rate. Tax is 10% of the first 600.00 and
*> 22% of the rest; the pension contribution is 5% of gross pay.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 standard-hours           PIC 99V9    VALUE 40.0.
01 overtime-factor          PIC 9V9     VALUE 1.5.
01 band-limit               PIC 9(5)V99 VALUE 600.00.
01 lower-rate               PIC V99     VALUE .10.
01 upper-rate               PIC V99     VALUE .22.
01 pension-rate             PIC V99     VALUE .05.
LINKAGE SECTION.
01 ls-rate                  PIC 9(3)V99.
01 ls-hours                 PIC 99V9.
01 ls-pay.
   05 ls-gross              PIC S9(7)V99.
   05 ls-tax                PIC S9(7)V99.
   05 ls-pension            PIC S9(7)V99.
   05 ls-net                PIC S9(7)V99.
PROCEDURE DIVISION USING ls-rate ls-hours ls-pay.
    IF ls-hours > standard-hours
        COMPUTE ls-gross ROUNDED =
              ls-rate * standard-hours
            + ls-rate * overtime-factor * (ls-hours - standard-hours)
    ELSE
        COMPUTE ls-gross ROUNDED = ls-rate * ls-hours
    END-IF
    IF ls-gross > band-limit
        COMPUTE ls-tax ROUNDED =
            band-limit * lower-rate + (ls-gross - band-limit) * upper-rate
    ELSE
        COMPUTE ls-tax ROUNDED = ls-gross * lower-rate
    END-IF
    COMPUTE ls-pension ROUNDED = ls-gross * pension-rate
    COMPUTE ls-net = ls-gross - ls-tax - ls-pension
    GOBACK.
END PROGRAM calc-pay.

Its output:

text
REJECTED 10006 Mateus Costa       hours or rate not numeric
REJECTED 10008 Sofia Rossi        hours over weekly limit
 
WEEKLY PAYROLL                                         WEEK 47 OF 2026
DEPT     EMPLOYEE           HOURS     GROSS      TAX  PENSION      NET
Delivery Olu Adeyemi         52.5    940.00   134.80    47.00   758.20
  Total Delivery                     940.00                     758.20
Front    Kenji Watanabe      38.5    847.00   114.34    42.35   690.31
Front    Priya Raman         40.0  1,096.00   169.12    54.80   872.08
  Total Front                      1,943.00                   1,562.39
Kitchen  Amina Yusuf         40.0    740.00    90.80    37.00   612.20
Kitchen  Hana Kim            32.0    616.00    63.52    30.80   521.68
Kitchen  Lucia Fernandez     46.0  1,212.75   194.81    60.64   957.30
  Total Kitchen                    2,568.75                   2,091.18
 
Records read                       8
Records accepted                   6
Records rejected                   2
Employees paid                     6
Total gross                     5,451.75
Total tax                         767.39
Total pension                     272.59
Total net pay                   4,411.77
Control totals balance.
Worked examplesH

A walk through the decisions

Validation before arithmetic. Mateus Costa's hours arrived as 3X.0, the kind of corruption real feeds produce. The class test ts-hours IS NOT NUMERIC caught it before any COMPUTE touched the field. On a mainframe, arithmetic on that field would have stopped the whole run with a data exception at two in the morning. Sofia Rossi's 95 hours are numeric but implausible, so a business rule rejects them too. Each rejection prints the employee and a reason, so the payroll team can fix the source and rerun.

The EVALUATE TRUE check. Validation rules are listed in priority order, and the first failing rule supplies the reason. Adding a rule later means adding one WHEN.

SORT with procedures. RELEASE and RETURN are the sort's equivalents of WRITE and READ. Using procedures instead of USING and GIVING means no intermediate files: records are validated on the way in and reported on the way out. The output procedure's loop has exactly the shape of lesson 6's read loop, with RETURN ... AT END instead of READ ... AT END.

Overtime and bands. Lucia Fernandez worked 46 hours at 24.75: 40 × 24.75 = 990.00 plus 6 × 1.5 × 24.75 = 222.75, so gross is 1,212.75. Her tax is 10% of 600.00 plus 22% of 612.75, which is 60.00 + 134.805 = 194.805, rounded to 194.81. Her pension is 5% of 1,212.75 = 60.6375, rounded to 60.64. Net pay is computed by subtraction, not rounded independently, so net + tax + pension equals gross exactly for every employee.

The control break and the empty file. The department break runs whenever the department changes and once more after the loop. The final break is guarded by IF ws-paid-count > 0: if every record were rejected, there would be no department to total, and an unguarded break would print a meaningless "Total" line.

Balancing. The last lines are what an operator or an automated check looks at first. Eight records read equals six accepted plus two rejected; six accepted equals six paid; and 4,411.77 + 767.39 + 272.59 = 5,451.75. If any of these failed, the program would say so loudly and set return code 8, so that a scheduler would stop the following steps (printing payslips, sending payments to the bank) until a person had looked.

Return codes. If the timesheet file cannot be opened, the program displays the file status and ends with return code 16; an unbalanced run ends with 8; a clean run ends with 0. Those values follow a widespread mainframe convention (0 fine, 4 warning, 8 error, 12 and 16 severe), which job schedulers and JCL COND and IF tests rely on.

ExplanationH

Testing it as a professional would

A reviewer of this program would ask for tests that hit the edges of every rule, because the edges are where implementations disagree:

CaseHoursRateExpected grossWhy
Exactly standard hours40.015.00600.00No overtime at exactly 40; gross exactly at the band, so tax is 60.00
Just over40.115.00602.25Overtime starts after 40
Rounding halfway46.024.751,212.75Tax is 194.805 and must round to 194.81
Hours at the limit80.0anyaccepted80.0 is allowed, 80.1 is not
Not numeric4X.0anyrejectedThe class test
Empty fileno detail linesCounts zero, totals zero, still balanced

Because the pay rules live in their own subprogram, the first four kinds of case can be tested without any files at all. A test harness is just another program that calls calc-pay with known inputs and compares the results with values worked out by hand from the specification. This one includes its own copy of calc-pay so that it runs on its own; in a real project both would include the same source through a copybook or link the same module:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. test-calc-pay.
*> Unit tests for calc-pay: each case gives inputs and expected results.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 tc-name               PIC X(26).
01 tc-rate               PIC 9(3)V99.
01 tc-hours              PIC 99V9.
01 tc-expect-gross       PIC S9(7)V99.
01 tc-expect-tax         PIC S9(7)V99.
01 tc-expect-net         PIC S9(7)V99.
01 tc-pay.
   05 tc-gross           PIC S9(7)V99.
   05 tc-tax             PIC S9(7)V99.
   05 tc-pension         PIC S9(7)V99.
   05 tc-net             PIC S9(7)V99.
01 passed-count          PIC 99 VALUE 0.
01 failed-count          PIC 99 VALUE 0.
PROCEDURE DIVISION.
0000-main.
    MOVE "no hours"                  TO tc-name
    MOVE 18.00 TO tc-rate  MOVE 00.0 TO tc-hours
    MOVE 0.00 TO tc-expect-gross MOVE 0.00 TO tc-expect-tax
    MOVE 0.00 TO tc-expect-net   PERFORM 1000-run-case

    MOVE "exactly 40 hours, at band" TO tc-name
    MOVE 15.00 TO tc-rate  MOVE 40.0 TO tc-hours
    MOVE 600.00 TO tc-expect-gross MOVE 60.00 TO tc-expect-tax
    MOVE 510.00 TO tc-expect-net   PERFORM 1000-run-case

    MOVE "first overtime tenth"      TO tc-name
    MOVE 15.00 TO tc-rate  MOVE 40.1 TO tc-hours
    MOVE 602.25 TO tc-expect-gross MOVE 60.50 TO tc-expect-tax
    MOVE 511.64 TO tc-expect-net   PERFORM 1000-run-case

    MOVE "half cent rounds up"       TO tc-name
    MOVE 24.75 TO tc-rate  MOVE 46.0 TO tc-hours
    MOVE 1212.75 TO tc-expect-gross MOVE 194.81 TO tc-expect-tax
    MOVE 957.30 TO tc-expect-net    PERFORM 1000-run-case

    MOVE "maximum hours"             TO tc-name
    MOVE 20.00 TO tc-rate  MOVE 80.0 TO tc-hours
    MOVE 2000.00 TO tc-expect-gross MOVE 368.00 TO tc-expect-tax
    MOVE 1532.00 TO tc-expect-net   PERFORM 1000-run-case

    DISPLAY passed-count " passed, " failed-count " failed"
    IF failed-count > 0
        MOVE 1 TO RETURN-CODE
    END-IF
    STOP RUN.

1000-run-case.
    CALL "calc-pay" USING tc-rate tc-hours tc-pay
    IF tc-gross = tc-expect-gross
       AND tc-tax = tc-expect-tax
       AND tc-net = tc-expect-net
       AND tc-gross = tc-net + tc-tax + tc-pension
        ADD 1 TO passed-count
        DISPLAY "PASS " FUNCTION TRIM(tc-name)
    ELSE
        ADD 1 TO failed-count
        DISPLAY "FAIL " tc-name " gross " tc-gross " tax " tc-tax
                " net " tc-net
    END-IF.
END PROGRAM test-calc-pay.

IDENTIFICATION DIVISION.
PROGRAM-ID. calc-pay.
*> Weekly pay for one employee. Overtime above 40 hours is paid at
*> one and a half times the rate. Tax is 10% of the first 600.00 and
*> 22% of the rest; the pension contribution is 5% of gross pay.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 standard-hours           PIC 99V9    VALUE 40.0.
01 overtime-factor          PIC 9V9     VALUE 1.5.
01 band-limit               PIC 9(5)V99 VALUE 600.00.
01 lower-rate               PIC V99     VALUE .10.
01 upper-rate               PIC V99     VALUE .22.
01 pension-rate             PIC V99     VALUE .05.
LINKAGE SECTION.
01 ls-rate                  PIC 9(3)V99.
01 ls-hours                 PIC 99V9.
01 ls-pay.
   05 ls-gross              PIC S9(7)V99.
   05 ls-tax                PIC S9(7)V99.
   05 ls-pension            PIC S9(7)V99.
   05 ls-net                PIC S9(7)V99.
PROCEDURE DIVISION USING ls-rate ls-hours ls-pay.
    IF ls-hours > standard-hours
        COMPUTE ls-gross ROUNDED =
              ls-rate * standard-hours
            + ls-rate * overtime-factor * (ls-hours - standard-hours)
    ELSE
        COMPUTE ls-gross ROUNDED = ls-rate * ls-hours
    END-IF
    IF ls-gross > band-limit
        COMPUTE ls-tax ROUNDED =
            band-limit * lower-rate + (ls-gross - band-limit) * upper-rate
    ELSE
        COMPUTE ls-tax ROUNDED = ls-gross * lower-rate
    END-IF
    COMPUTE ls-pension ROUNDED = ls-gross * pension-rate
    COMPUTE ls-net = ls-gross - ls-tax - ls-pension
    GOBACK.
END PROGRAM calc-pay.
text
PASS no hours
PASS exactly 40 hours, at band
PASS first overtime tenth
PASS half cent rounds up
PASS maximum hours
05 passed, 00 failed

Each case also checks the invariant gross = net + tax + pension, and the harness sets a non-zero return code if anything fails, so a build pipeline can stop on a failure without reading the output.

In a mainframe shop, calc-pay would have unit tests of its own, written with a COBOL unit-testing tool such as the Open Mainframe Project's COBOL Check or a vendor product, and run in the team's build pipeline. The whole program would also have a regression test: run last week's real input through the new version and compare the output with the old version's, line by line. When a change is meant to alter results (a new tax year), the comparison shows exactly which employees changed and by how much, which is what the payroll department will want to see before signing off.

Ways to extend the project, each practising something from the course:

  • Write a payments file of employee number and net pay in COMP-3, as a bank interface would expect, and a second program that reads it back.
  • Read the tax bands from a file into an OCCURS DEPENDING ON table, so the scale can change without recompiling.
  • Write the report to a print file with page headings, page numbers and AFTER ADVANCING, or rewrite it with the Report Writer.
  • Split the record layouts into copybooks and compile the subprogram separately with cobc -m.
Further reading

Where to go next

You can now read and write COBOL that does what most business COBOL does: describe records precisely, compute with exact decimals, process files in batch, and report with totals that balance. Where you go next depends on why you came.

  • To work on mainframes, get time on a real z/OS system. The Open Mainframe Project COBOL Programming Course provides free labs; IBM also runs free learning systems and badges for z/OS skills. Learn JCL, ISPF, data sets and the job output (SDSF), then CICS and Db2 basics. Read IBM's Enterprise COBOL documentation, especially the programming guide.
  • To keep learning the language, read the GnuCOBOL documentation, explore the intrinsic functions, and write programs that process real public data sets (exchange rates, weather, census tables) as fixed-width and CSV files.
  • To help modernise systems, practise reading large old programs: trace a field from the file layout through every MOVE and COMPUTE, draw the PERFORM hierarchy, and write down the business rules you find. That skill is scarce and valuable, whatever language the system ends up in.

Whichever path you take, keep the habits this course has stressed: size every field for the worst case, never use floating point for money, check every file status, close every scope, and make every run prove its own totals.

Optional Christian reflection · Faithful in little

A reflection from the Christian tradition that inspires Lumenary Academy. It is not part of the mathematics and you are welcome to skip it.

Payroll code is unglamorous. Nobody praises the program that paid everyone correctly this week, and its authors are often long retired. Yet a wrong cent repeated across a workforce is real money taken from real families, and a run that fails at night can mean rent paid late. There is an old line about being faithful in very little things (Luke 16:10). Careful, checkable work that serves people who will never know your name is a good way to practise it.

Exercises

Exercises

Try each question before opening the hints. Every answer about program output was checked by compiling and running the program with GnuCOBOL 3.2.

1. Project: a weekly payroll report

Warm-upComputational

Using the project's calc-pay, what is the net pay for 40.0 hours at 15.00 an hour? Give it with two decimals, for example 123.45.

2. Project: a weekly payroll report

CoreComputational

What gross pay does calc-pay compute for 44.0 hours at 20.00 an hour?

3. Project: a weekly payroll report

CoreComputational

For that same gross of 920.00, what tax does calc-pay compute?

4. Project: a weekly payroll report

Warm-upHuman

Suppose the sample file also contained a timesheet for 80.1 hours at 12.00 and one for 80.0 hours at 12.00. How many records would the run reject in total (counting the two already rejected)?

5. Project: a weekly payroll report

CoreHuman

Why does calc-pay compute net pay as gross − tax − pension rather than rounding a separate formula such as gross × (1 − effective rate)?

Choose one

6. Project: a weekly payroll report

StretchComputational

The output procedure ends with IF ws-paid-count > 0 PERFORM 2300-department-break END-IF. What would happen without the IF if every timesheet in the file were rejected?

Choose one

7. Project: a weekly payroll report

CoreHuman

Under the common mainframe convention used by the project, what return code does the program set when the control totals do not balance?

Further reading and resources