← Back to Week 3 Hub
Workbook 3a — Exercise

Payroll Calculator (Read)

Read an employee file, build Employee objects, and print a payroll report  |  Workbook p.17-18

The Exercise

Create a Java app called payroll-calculator. It reads employee data from a CSV-style text file where columns are separated by the | (pipe) character. Each line looks like:

10|Dana Wyatt|52.5|12.50  —  that's id | name | hoursWorked | payRate.

Build an Employee class with private fields for each of those four values, a parameterized constructor that takes all four, getters (and setters if you want them), and a method getGrossPay() that returns hoursWorked * payRate.

In main, ask the user for the employee filename, open it with a BufferedReader, read it line by line, split each line on |, create an Employee object, and print a formatted row with printf showing id, name, and gross pay.

Example Run
Sample employees.csv
10|Dana Wyatt|52.5|12.50
20|Ezra Aiden|21.5|13.25
30|Brittany Thibbs|44|15.00
Run 1 — Payroll report
Enter the employee file name: employees.csv

10 Dana Wyatt $656.25
20 Ezra Aiden $284.88
30 Brittany Thibbs $660.00
Run 2 — File not found
Enter the employee file name: missing.csv

Could not open that file. Check the name and try again.
Concepts You'll Use
Flow
Build Employee class
Prompt for filename
Open BufferedReader
Loop readLine()
Split on |
Convert tokens + new Employee
printf id, name, gross pay
Close reader

Workbook 3a, p.17-18 — Exercise: Payroll Calculator (Read)

← Bedtime Stories Payroll Calculator (Write) →