COBOL Mini Project
In this project you build a small weekly payroll run, the kind of batch program COBOL has run for decades. It
brings together record layouts, arithmetic with ROUNDED, EVALUATE, level-88 names, tables, files, edited
pictures and a control-break report.
The finished program will:
- read employee time records (id, name, department, hours worked, hourly rate) from a file,
- calculate gross pay with overtime, tax and pension deductions, and net pay,
- print a payroll report with headings, one line per employee, a subtotal per department and a grand total.
Every step below is a complete program you can run and change. The input data is built inside the program, since the playground has no files of its own: step 4 writes it to a file first and then processes the file, exactly as a batch job would.
Step 1: The employee record
Start with the layout. Each time record is 29 characters wide:
| Columns | Field | Picture |
|---|---|---|
| 1–5 | Employee id | 9(5) |
| 6–17 | Name | X(12) |
| 18–22 | Department | X(5) |
| 23–25 | Hours worked, one decimal | 99V9 |
| 26–29 | Hourly rate | 99V99 |
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL-STEP1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT-DATA.
05 FILLER PIC X(29) VALUE "10001Alice MoreauADMIN4002550".
05 FILLER PIC X(29) VALUE "10002Bruno Diaz ADMIN3752200".
05 FILLER PIC X(29) VALUE "10003Chen Wei SALES4501980".
01 WS-INPUT REDEFINES WS-INPUT-DATA.
05 WS-LINE PIC X(29) OCCURS 3 TIMES.
01 EMP-RECORD.
05 EMP-ID PIC 9(5).
05 EMP-NAME PIC X(12).
05 EMP-DEPT PIC X(5).
05 EMP-HOURS PIC 99V9.
05 EMP-RATE PIC 99V99.
01 WS-I PIC 9.
PROCEDURE DIVISION.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 3
MOVE WS-LINE (WS-I) TO EMP-RECORD
DISPLAY EMP-ID " " EMP-NAME " " EMP-DEPT
" hours " EMP-HOURS " rate " EMP-RATE
END-PERFORM
STOP RUN.10001 Alice Moreau ADMIN hours 40.0 rate 25.50
10002 Bruno Diaz ADMIN hours 37.5 rate 22.00
10003 Chen Wei SALES hours 45.0 rate 19.80
Moving the 29-character line into the EMP-RECORD group splits it into fields, because the group is just those
29 bytes. That is how every COBOL program sees a record from a file.
Step 2: Gross pay with overtime
Hours above 40 are paid at 1.5 times the normal rate. The calculation goes into its own paragraph, and the results into working-storage fields with enough digits:
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL-STEP2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMP-RECORD.
05 EMP-ID PIC 9(5).
05 EMP-NAME PIC X(12).
05 EMP-DEPT PIC X(5).
05 EMP-HOURS PIC 99V9.
05 EMP-RATE PIC 99V99.
01 WS-STANDARD-HOURS PIC 99V9 VALUE 40.
01 WS-OVERTIME-HOURS PIC 99V9.
01 WS-GROSS PIC 9(5)V99.
PROCEDURE DIVISION.
MOVE "10003Chen Wei SALES4501980" TO EMP-RECORD
PERFORM CALCULATE-GROSS
MOVE "10002Bruno Diaz ADMIN3752200" TO EMP-RECORD
PERFORM CALCULATE-GROSS
STOP RUN.
CALCULATE-GROSS.
IF EMP-HOURS > WS-STANDARD-HOURS
COMPUTE WS-OVERTIME-HOURS =
EMP-HOURS - WS-STANDARD-HOURS
COMPUTE WS-GROSS ROUNDED =
WS-STANDARD-HOURS * EMP-RATE
+ WS-OVERTIME-HOURS * EMP-RATE * 1.5
ELSE
MOVE ZERO TO WS-OVERTIME-HOURS
COMPUTE WS-GROSS ROUNDED = EMP-HOURS * EMP-RATE
END-IF
DISPLAY EMP-NAME " overtime " WS-OVERTIME-HOURS
" gross " WS-GROSS.Chen Wei overtime 05.0 gross 00940.50
Bruno Diaz overtime 00.0 gross 00825.00
Chen worked 45 hours: 40 × 19.80 = 792.00 plus 5 × 19.80 × 1.5 = 148.50, so 940.50.
Step 3: Deductions and net pay
Tax depends on the weekly gross (a simplified flat rate per band), and everyone pays 5% into the pension scheme.
The tax band becomes a set of level-88 condition names on the gross pay, and EVALUATE TRUE picks the rate:
| Weekly gross | Tax rate |
|---|---|
| below 400.00 | 10% |
| 400.00 – 899.99 | 20% |
| 900.00 and more | 30% |
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL-STEP3.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-GROSS PIC 9(5)V99.
88 LOW-BAND VALUE 0 THRU 399.99.
88 MIDDLE-BAND VALUE 400 THRU 899.99.
01 WS-TAX-RATE PIC V99.
01 WS-TAX PIC 9(5)V99.
01 WS-PENSION PIC 9(5)V99.
01 WS-NET PIC 9(5)V99.
01 ED-AMOUNT PIC ZZ,ZZ9.99.
PROCEDURE DIVISION.
MOVE 940.50 TO WS-GROSS
PERFORM CALCULATE-NET
MOVE 825.00 TO WS-GROSS
PERFORM CALCULATE-NET
MOVE 380.25 TO WS-GROSS
PERFORM CALCULATE-NET
STOP RUN.
CALCULATE-NET.
EVALUATE TRUE
WHEN LOW-BAND MOVE .10 TO WS-TAX-RATE
WHEN MIDDLE-BAND MOVE .20 TO WS-TAX-RATE
WHEN OTHER MOVE .30 TO WS-TAX-RATE
END-EVALUATE
COMPUTE WS-TAX ROUNDED = WS-GROSS * WS-TAX-RATE
COMPUTE WS-PENSION ROUNDED = WS-GROSS * 0.05
COMPUTE WS-NET = WS-GROSS - WS-TAX - WS-PENSION
MOVE WS-GROSS TO ED-AMOUNT
DISPLAY "Gross " ED-AMOUNT WITH NO ADVANCING
MOVE WS-TAX TO ED-AMOUNT
DISPLAY " tax " ED-AMOUNT WITH NO ADVANCING
MOVE WS-PENSION TO ED-AMOUNT
DISPLAY " pension " ED-AMOUNT WITH NO ADVANCING
MOVE WS-NET TO ED-AMOUNT
DISPLAY " net " ED-AMOUNT.Gross 940.50 tax 282.15 pension 47.03 net 611.32
Gross 825.00 tax 165.00 pension 41.25 net 618.75
Gross 380.25 tax 38.03 pension 19.01 net 323.21
Each deduction is rounded to the cent separately, and net pay is the gross minus the rounded deductions, so the three printed amounts always add up exactly. That is a rule auditors check.
Step 4: The complete payroll run
The final program puts everything together:
1000-INITIALISEwrites the test input file (in production it would already exist) and opens the files.2000-PROCESS-EMPLOYEEruns once per record: it calculates pay, handles the department control break and prints a detail line.3000-FINISHprints the last department subtotal and the grand totals.
The input must be sorted by department for the control break to work; the test data is already in that order (a
real job would run a SORT step first).
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT TIME-FILE ASSIGN TO "timecards.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-TIME-STATUS.
DATA DIVISION.
FILE SECTION.
FD TIME-FILE.
01 TIME-RECORD.
05 EMP-ID PIC 9(5).
05 EMP-NAME PIC X(12).
05 EMP-DEPT PIC X(5).
05 EMP-HOURS PIC 99V9.
05 EMP-RATE PIC 99V99.
WORKING-STORAGE SECTION.
01 WS-TIME-STATUS PIC XX.
01 WS-EOF PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
*> ---------- calculation fields ----------
01 WS-OVERTIME-HOURS PIC 99V9.
01 WS-GROSS PIC 9(5)V99.
88 LOW-BAND VALUE 0 THRU 399.99.
88 MIDDLE-BAND VALUE 400 THRU 899.99.
01 WS-TAX-RATE PIC V99.
01 WS-TAX PIC 9(5)V99.
01 WS-PENSION PIC 9(5)V99.
01 WS-NET PIC 9(5)V99.
*> ---------- totals ----------
01 WS-PREV-DEPT PIC X(5) VALUE SPACES.
01 WS-DEPT-TOTALS.
05 WS-DEPT-GROSS PIC 9(6)V99 VALUE 0.
05 WS-DEPT-NET PIC 9(6)V99 VALUE 0.
01 WS-GRAND-GROSS PIC 9(7)V99 VALUE 0.
01 WS-GRAND-NET PIC 9(7)V99 VALUE 0.
01 WS-EMPLOYEES PIC 99 VALUE 0.
*> ---------- print lines ----------
01 HL-TITLE PIC X(40)
VALUE "TAD BANK - WEEKLY PAYROLL, WEEK 07".
01 HL-COLUMNS.
05 FILLER PIC X(18) VALUE "ID NAME".
05 FILLER PIC X(15) VALUE "HOURS GROSS".
05 FILLER PIC X(27) VALUE " TAX PENSION NET".
01 HL-RULE PIC X(60) VALUE ALL "=".
01 DL-DETAIL.
05 DL-ID PIC 9(5).
05 FILLER PIC X VALUE SPACE.
05 DL-NAME PIC X(12).
05 FILLER PIC X VALUE SPACE.
05 DL-HOURS PIC Z9.9.
05 DL-OT-FLAG PIC X.
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 TL-DEPT.
05 FILLER PIC X(10) VALUE " Subtotal".
05 FILLER PIC X VALUE SPACE.
05 TL-DEPT-NAME PIC X(5).
05 FILLER PIC X(7) VALUE SPACES.
05 TL-GROSS PIC ZZZ,ZZ9.99.
05 FILLER PIC X(17) VALUE SPACES.
05 TL-NET PIC ZZZ,ZZ9.99.
01 TL-GRAND.
05 FILLER PIC X(11) VALUE "GRAND TOTAL".
05 FILLER PIC X(11) VALUE SPACES.
05 TL-GRAND-GROSS PIC $$,$9.99.
05 FILLER PIC X(16) VALUE SPACES.
05 TL-GRAND-NET PIC $$,$9.99.
PROCEDURE DIVISION.
0000-MAIN.
PERFORM 1000-INITIALISE
PERFORM 1100-READ-TIME-RECORD
PERFORM 2000-PROCESS-EMPLOYEE UNTIL END-OF-FILE
PERFORM 3000-FINISH
GOBACK.
1000-INITIALISE.
OPEN OUTPUT TIME-FILE
WRITE TIME-RECORD FROM "10001Alice MoreauADMIN4002550"
WRITE TIME-RECORD FROM "10002Bruno Diaz ADMIN3752200"
WRITE TIME-RECORD FROM "10003Chen Wei SALES4501980"
WRITE TIME-RECORD FROM "10004Dana Kowal SALES4002100"
WRITE TIME-RECORD FROM "10005Emeka Obi STORE5201650"
CLOSE TIME-FILE
OPEN INPUT TIME-FILE
IF WS-TIME-STATUS NOT = "00"
DISPLAY "Cannot open time file, status "
WS-TIME-STATUS
MOVE 16 TO RETURN-CODE
STOP RUN
END-IF
DISPLAY HL-TITLE
DISPLAY HL-COLUMNS
DISPLAY HL-RULE.
1100-READ-TIME-RECORD.
READ TIME-FILE
AT END SET END-OF-FILE TO TRUE
END-READ.
2000-PROCESS-EMPLOYEE.
IF EMP-DEPT NOT = WS-PREV-DEPT
IF WS-PREV-DEPT NOT = SPACES
PERFORM 2300-PRINT-DEPT-TOTAL
END-IF
MOVE EMP-DEPT TO WS-PREV-DEPT
END-IF
PERFORM 2100-CALCULATE-PAY
PERFORM 2200-PRINT-DETAIL
ADD WS-GROSS TO WS-DEPT-GROSS WS-GRAND-GROSS
ADD WS-NET TO WS-DEPT-NET WS-GRAND-NET
ADD 1 TO WS-EMPLOYEES
PERFORM 1100-READ-TIME-RECORD.
2100-CALCULATE-PAY.
IF EMP-HOURS > 40
COMPUTE WS-OVERTIME-HOURS = EMP-HOURS - 40
COMPUTE WS-GROSS ROUNDED = 40 * EMP-RATE
+ WS-OVERTIME-HOURS * EMP-RATE * 1.5
ELSE
MOVE ZERO TO WS-OVERTIME-HOURS
COMPUTE WS-GROSS ROUNDED = EMP-HOURS * EMP-RATE
END-IF
EVALUATE TRUE
WHEN LOW-BAND MOVE .10 TO WS-TAX-RATE
WHEN MIDDLE-BAND MOVE .20 TO WS-TAX-RATE
WHEN OTHER MOVE .30 TO WS-TAX-RATE
END-EVALUATE
COMPUTE WS-TAX ROUNDED = WS-GROSS * WS-TAX-RATE
COMPUTE WS-PENSION ROUNDED = WS-GROSS * 0.05
COMPUTE WS-NET = WS-GROSS - WS-TAX - WS-PENSION.
2200-PRINT-DETAIL.
MOVE EMP-ID TO DL-ID
MOVE EMP-NAME TO DL-NAME
MOVE EMP-HOURS TO DL-HOURS
IF WS-OVERTIME-HOURS > 0
MOVE "*" TO DL-OT-FLAG
ELSE
MOVE SPACE TO DL-OT-FLAG
END-IF
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 DL-DETAIL.
2300-PRINT-DEPT-TOTAL.
MOVE WS-PREV-DEPT TO TL-DEPT-NAME
MOVE WS-DEPT-GROSS TO TL-GROSS
MOVE WS-DEPT-NET TO TL-NET
DISPLAY TL-DEPT
INITIALIZE WS-DEPT-TOTALS.
3000-FINISH.
IF WS-PREV-DEPT NOT = SPACES
PERFORM 2300-PRINT-DEPT-TOTAL
END-IF
CLOSE TIME-FILE
DISPLAY HL-RULE
MOVE WS-GRAND-GROSS TO TL-GRAND-GROSS
MOVE WS-GRAND-NET TO TL-GRAND-NET
DISPLAY TL-GRAND
DISPLAY WS-EMPLOYEES " employees paid; * = overtime".TAD BANK - WEEKLY PAYROLL, WEEK 07
ID NAME HOURS GROSS TAX PENSION NET
============================================================
10001 Alice Moreau 40.0 1,020.00 306.00 51.00 663.00
10002 Bruno Diaz 37.5 825.00 165.00 41.25 618.75
Subtotal ADMIN 1,845.00 1,281.75
10003 Chen Wei 45.0* 940.50 282.15 47.03 611.32
10004 Dana Kowal 40.0 840.00 168.00 42.00 630.00
Subtotal SALES 1,780.50 1,241.32
10005 Emeka Obi 52.0* 957.00 287.10 47.85 622.05
Subtotal STORE 957.00 622.05
============================================================
GRAND TOTAL $4,582.50 $3,145.12
05 employees paid; * = overtime
How the pieces fit together
| Paragraph | Responsibility | Concepts used |
|---|---|---|
0000-MAIN |
Overall flow: initialise, priming read, loop, finish | PERFORM … UNTIL, condition name |
1000-INITIALISE |
Create and open the input file, print headings | OPEN, WRITE … FROM, file status |
2000-PROCESS-EMPLOYEE |
Control break, totals, next read | Control-break logic, multi-receiver ADD |
2100-CALCULATE-PAY |
Gross, tax band, deductions, net | COMPUTE ROUNDED, EVALUATE TRUE, level 88 |
2200-PRINT-DETAIL |
Format one line | Edited pictures, group print line |
2300-PRINT-DEPT-TOTAL |
Subtotal and reset | INITIALIZE on a group |
3000-FINISH |
Last subtotal, grand total, close | $ floating currency |
Ideas for extending the project
- Add a
SORTstep (SORT … USING … GIVING) so the input file no longer needs to be in department order. - Reject records with non-numeric hours or rates (
IS NUMERICon a textREDEFINES) and list them in an error section with a reason, settingRETURN-CODEto 4 if any were rejected. - Move the tax bands into a table (
OCCURSwith limits and rates) and look them up withSEARCH. - Write the report to a print file with
WRITE … AFTER ADVANCING, with a page heading and page numbers every 20 lines. - Move the pay calculation into a subprogram
CALC-PAYwith aLINKAGE SECTIONparameter block.
Test yourself
Why does 0000-MAIN read the first record before the PERFORM … UNTIL loop?
- To skip a header record
- It is the priming read: the loop processes the current record and reads the next one at the end of each pass
- Because READ cannot be inside a loop
- To count the records
Why is the department subtotal printed again in 3000-FINISH?
- To print it twice
- The last department has no following key change, so its subtotal must be printed at end of file
- Because INITIALIZE failed
- Because the file was not sorted
Why are tax and pension each rounded before net pay is calculated?
- To make the program faster
- So that gross = tax + pension + net exactly, to the cent, on every line
- Because COMPUTE cannot handle three operands
- It makes no difference