Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Payroll BlueJ Project

Click here to download a BlueJ project for Payroll.


Payroll Source Code

The Java source code for Payroll is below. Click on a file name to download it.


Payroll.java

/*
 * Example Payroll: compute net pay and taxes for a given hourly wage,
 * work schedule (with time and a half for overtime), and tax rate.
 * 
 * Demonstrates more conditional execution and the use of DecimalFormat
 * to format output nicely.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: Payroll.java 1928 2012-10-02 00:52:05Z terescoj $
 */

import java.text.DecimalFormat;
import java.util.Scanner;

public class Payroll {

    public static void main(String[] args) {

        // use a named constant for tax rate, which stays the same
        // for all employees for whom this program is run
        final double TAX_RATE = 20.0;

        // another named constant for the standard work week - the 
        // point beyond which the employee earns "overtime" pay
        final double STANDARD_HOURS = 40.0;

        // and one for the pay rate multiplier for hours above the standard
        final double OVERTIME_RATE = 1.5;

        // we'll use a Scanner
        Scanner keyboard = new Scanner(System.in);

        // prompt for hourly wage
        System.out.print("Please enter the employee's hourly wage: ");
        double payRate = keyboard.nextDouble();

        // do some basic error checking
        if (payRate <= 0.0) {
            System.out.println("Wages must be positive.");
            System.exit(1);
        }

        // now hours worked
        System.out.print("Please enter the employee's work hours for the week: ");
        double hoursWorked = keyboard.nextDouble();

        // hours should also be positive
        if (hoursWorked <= 0.0) {
            System.out.println("Hours worked must be positive.");
            System.exit(1);
        }

        // Compute the weekly pay.  How we do this depends on whether there
        // is any overtime, so we will check for that first.
        // Note that we must declare the variables regularPay and overtimePay
        // outside of the if-else so they retain their values
        // after we leave the if-else.  Note also that we can declare multiple
        // variables of the same type on one line by separating the names
        // with commas.
        double regularPay, overtimePay;

        // apply the overtime rate if applicable
        if (hoursWorked > STANDARD_HOURS) {
            // We credit STANDARD_HOURS of the work time at the regular pay
            // rate and then remaining hours at the OT rate.
            regularPay = STANDARD_HOURS * payRate;
            overtimePay = (hoursWorked - STANDARD_HOURS) *
            OVERTIME_RATE * payRate;
        }
        else {
            regularPay = hoursWorked * payRate;
            overtimePay = 0.0;
        }

        double grossPay = regularPay + overtimePay;

        // now we know the gross pay, compute the tax and from that
        // the net pay
        double tax = grossPay * TAX_RATE/100;
        double netPay = grossPay - tax;

        // finally, we print out the answers, using a DecimalFormat object
        // to make it look nice.
        // This one will have at least one digit before the decimal point 
        // (more if needed) and exactly one digit after (even if there are
        // more non-zero digits in the hundredths place and beyond).
        DecimalFormat hoursFormat = new DecimalFormat("0.0");
        
        // This one will specifically print the number in a format with
        // a comma for more than 4 places before the decimal point, and will
        // always have 2 places after the decimal point.
        DecimalFormat formatter = new DecimalFormat("#,##0.00");

        System.out.println("For " + hoursFormat.format(hoursWorked) + 
            " hours worked at $" + formatter.format(payRate) + " per hour:");
        System.out.println("Regular pay: $" + formatter.format(regularPay));
        System.out.println("Overtime pay: $" + formatter.format(overtimePay));
        System.out.println("Gross pay: $" + formatter.format(grossPay));
        System.out.println("Taxes: $" + formatter.format(tax));
        System.out.println("Net pay: $" + formatter.format(netPay));
    }
}