Saturday, October 8, 2016

// // Leave a Comment

Java GUI Payroll Program

The program below is the solution to Liang's Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.27.

Question: Rewrite Exercise 2.25 using GUI input and output dialog boxes.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.27 - Financial application: payroll
 *
 */ 

import javax.swing.JOptionPane;

public class Ex02_27 {

    public static void main(String[] args) {
        
        //collect employee data using GUI input dialog box
        String name = JOptionPane.showInputDialog(null, "Enter employee's name: ",
                "Input", JOptionPane.QUESTION_MESSAGE);
        String sHours = JOptionPane.showInputDialog(null,
                "Enter number of hours worked in a week: ", "Input",
                JOptionPane.QUESTION_MESSAGE);
        int hours = Integer.parseInt(sHours);
        String sRate = JOptionPane.showInputDialog(null, "Enter hourly pay rate: ",
                "Input", JOptionPane.QUESTION_MESSAGE);
        double rate = Double.parseDouble(sRate);
        String sFedRate = JOptionPane.showInputDialog(null,
                "Enter federal tax withholding rate e.g 20 for 20%: ", "Input",
                JOptionPane.QUESTION_MESSAGE);
        int fedRate = Integer.parseInt(sFedRate);
        String sStateRate = JOptionPane.showInputDialog(null,
                "Enter state tax withholding rate e.g 10 for 10%: ", "Input",
                JOptionPane.QUESTION_MESSAGE);
        int stateRate = Integer.parseInt(sStateRate);

        //calculate employee gross pay and net pay
        double grossPay = hours * rate;
        double fedWithHolding = ((fedRate / 100.0) * grossPay);
        double stateWithHolding = ((stateRate / 100.0) * grossPay);
        double totalDeduction = fedWithHolding + stateWithHolding;
        double netPay = grossPay - totalDeduction;

        //format netPay to two decimal places
        netPay = (int) (netPay * 100) / 100.0;

        //print payroll using GUI output dialog box
        String output = ("\n\nEmployee Name: " + name + "\nHours Worked: " + hours
                + "\nPay Rate: $" + rate + "\nGross Pay: $" + grossPay + "\nDeductions: "
                + "\n  Federal Withholding (" + (double) fedRate + "%): $" + fedWithHolding
                + "\n  State Withholding (" + (double) stateRate + "%): $" + stateWithHolding
                + "\n  Total Deduction: $" + totalDeduction + "\nNet Pay: $" + netPay);

        JOptionPane.showMessageDialog(null, output, "Employee Payroll Info", 1);
    }
}
Exercise 2.27 solution - Java GUI Payroll Program
Program output
Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.