Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Programming. Barnhill Fastener Company runs a small factory. The company em

ID: 665432 • Letter: J

Question

JAVA Programming.

Barnhill Fastener Company runs a small factory. The company employs workers who

are paid one of three hourly rates depending on skill level:

Skill Level

Hourly Pay Rate ($)

1

17.00

2

20.00

3

22.00

Each factory worker might work any number of hours per week; any hours over 40 are

paid at one and one-half times the usual rate.

In addition, workers in skill levels 2 and 3 can elect the following insurance

options:

Option

Explanation

Weekly Cost to Employee ($)

1

Medical Insurance

32.50

2

Dental Insurance

20.00

3

Long Term DI

10.00

Write an interactive Java payroll application that calculates the net pay for a factory

worker. The program prompts the user for skill level and hours worked, as well as

appropriate insurance and retirement options for the employee’s skill level category. Use three JOptionPane Confirm Dialog Boxes (using the JOptionPane.YES_NO_OPTION) to ask the

worker if they want medical insurance, dental insurance, and long-term disability insurance, as the

worker can enroll in more than one insurance option. Use a do...while() loop to ask the user for their skill level. Keep looping until a valid skill level is provided. Use a JOptionPane to show the worker’s gross pay.

The application displays: (1) the hours worked, (2) the hourly pay rate, (3) the regular

pay for 40 hours, (4) the overtime pay, (5) the total of regular and overtime pay, and

(6) the total itemized deductions. If the deductions exceed the gross pay, display an

error message; otherwise, calculate and display (7) the net pay after all the deductions

have been subtracted from the gross. Save the file as Pay.java.

Skill Level

Hourly Pay Rate ($)

1

17.00

2

20.00

3

22.00

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPaneConfirmDialogInput implements ActionListener {
JFrame myFrame = null;
public static void main(String[] a) {
   String employeeName;
double payRatePerHour;
double hoursWorkedPerWeek;
double regularPay;
double overtimePay;


      
   employeeName =
               JOptionPane.showInputDialog (null,
               "PAYROLL PROGAM" +
           " Please enter your name");

   payRatePerHour =
               Double.parseDouble(JOptionPane.showInputDialog (null,
               "PAYROLL PROGAM" +
               " Please enter your hourly pay rate"));
   hoursWorkedPerWeek =
               Double.parseDouble(JOptionPane.showInputDialog (null,
               "PAYROLL PROGAM" +
               " Please enter how may hours you worked this week"));
(new JOptionPaneConfirmDialogInput()).test();
}
Scanner input = new Scanner(System.in);

System.out.println("Enter employee's name:");
String employee = input.next();

System.out.println("Enter number of hours worked:");
double hours = input.nextDouble();

System.out.println("Enter hourly pay rate:");
double pay = input.nextDouble();

double gross_pay = pay * hours;

System.out.println("Enter federal tax withholding rate:");
double fedtax = input.nextDouble();
double fedtaxr = fedtax * 0.20;

System.out.println("Enter state tax withholding rate:");
double statetax = input.nextDouble();

double statetaxr = statetax * 0.20;

double deductions = fedtaxr + statetaxr;

double total_pay = gross_pay - deductions;

System.out.println("Employee name: " + employee);

System.out.println("Hours worked: " + hours);

System.out.println(" Enter payrate: " + pay);

System.out.println(" Enter gross pay: " + gross_pay);

System.out.println(" Deductions: ");
System.out.println(" Federdal Withholding (20.0%): " + fedtaxr);
System.out.println(" State Withholding (9.0%)" + statetaxr);
System.out.println(" Total deductions:" + deductions);
System.out.println("Total pay: " + total_pay);
}
private void test() {
myFrame = new JFrame("showConfirmDialog() Input Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myPane = myFrame.getContentPane();
JButton myButton = new JButton("Show");
myButton.addActionListener(this);
myPane.add(myButton);
myFrame.pack();
myFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int messageType = JOptionPane.INFORMATION_MESSAGE;
int optionType = JOptionPane.YES_NO_CANCEL_OPTION;
int code = JOptionPane.showConfirmDialog(myFrame,
"Do you want to continue?",
"Confirmation Dialog Box", optionType, messageType);

String answer = "Unknown";
if (code == JOptionPane.OK_OPTION) {
answer = "Ok";
} else if (code == JOptionPane.YES_OPTION) {
answer = "Yes";
} else if (code == JOptionPane.NO_OPTION) {
answer = "No";
} else if (code == JOptionPane.CANCEL_OPTION) {
answer = "Cancel";
} else if (code == JOptionPane.CLOSED_OPTION) {
answer = "Closed";

if(textArea.getLineCount() >= 1)
{

int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);

if(dialogButton == JOptionPane.YES_OPTION){ //The ISSUE is here

JFileChooser saveFile = new JFileChooser();
int saveOption = saveFile.showSaveDialog(frame);
if(saveOption == JFileChooser.APPROVE_OPTION){

try{
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
fileWriter.write(textArea.getText());
fileWriter.close();
}catch(Exception ex){

}


  
   if(hoursWorkedPerWeek > 40)
{
regularPay = 40 * payRatePerHour;
overtimePay = (hoursWorkedPerWeek - 40) * 1.5 * payRatePerHour;
}
else
{
regularPay = hoursWorkedPerWeek * payRatePerHour;
overtimePay = 0.0;
}

   
JOptionPane.showMessageDialog(null,
   "PAYROLL PROGAM" +
               " Payroll for : " +
               employeeName +
   " Regular pay is " +
regularPay + " Overtime pay is " +
               overtimePay);
}
}
System.out.println("Answer: "+answer);
}
System.exit(0);
}
}