In: Computer Science
The human resources department for your company needs a program that will determine how much to deduct from an employee’s paycheck to cover healthcare costs. Health care deductions are based on several factors. All employees are charged at flat rate of $150 to be enrolled in the company healthcare system. If they are married there is an additional charge of $75 to cover their spouse/partner. If they have children, the cost is $50 per child. In addition, all employees are given a 10% deduction in the total cost if they have declared to be a “non-smoker”.
Your goal is to create a program that gathers the employee’s name, marital status, number of children and whether or not they smoke tobacco for a single employee. While gathering this information, if the user enters at least one invalid value, the program must display one error message telling the user they made a mistake and that they should re-run the program to try again. The program must then end at this point. However, if all valid values are entered, the program should calculate the total cost of the healthcare payroll deduction and then print a well-formatted report that shows the employee’s name, marital status, total number of children, and smoker designation, and total deduction amount.
Write a well-documented, efficient Java program that implements the algorithm you identified. Include appropriate documentation as identified in the documentation expectations document. Note: You must use the JOptionPane class for input/output. Additionally, if you use System.exit as shown in the textbook, it may only be used as the absolute last line in the program. You may not use System.exit, or any variant that exits the program in the middle of the program. The program should be designed to only exit once the algorithm has finished.
thanks for the question, here is the complete code in Java, have used JoptionPane only to input andoutput data.
Below is the code
==============================================================
import javax.swing.*;
public class HealthInsurance {
public static void
main(String[] args) {
String name =
getName();
boolean
isMarried = isMarried();
double
deduction = 150; // base fees
int
childrens = 0;
if
(isMarried) {
childrens = childrens();
deduction += 75 + childrens * 50; // 75 for spouse and 50$ for each
children
}
boolean
isSmoker = isSmoker();
if
(!isSmoker) {
deduction -= deduction * .1; // deduct 10%
}
String desc =
"Here are the
details:\n";
desc +=
"\nName: " +
name;
desc +=
"\nMarried : " +
isMarried;
desc +=
"\nChildrens : "
+ childrens;
desc +=
"\nSmoker : " +
isSmoker;
desc +=
"\nHealth Care Deductions
: $" + String.format("%.2f", deduction) +
"\n";
JOptionPane.showMessageDialog(null, desc);
}
private static String getName()
{
String name =
JOptionPane.showInputDialog("Enter name");
if
(name.trim().length() == 0) {
JOptionPane.showMessageDialog(null,"You
didnt entered a valid name. Program will terminate
now.");
System.exit(1);
}
return
name;
}
private static boolean
isMarried() {
String response =
JOptionPane.showInputDialog("Are you married (yes/no)?
");
if
(response.trim().length() == 0 ||
!(response.equalsIgnoreCase("yes") ||
response.equalsIgnoreCase("no"))) {
JOptionPane.showMessageDialog(null,"You
need to enter either yes or no. " +
"You didnt entered a valid response. Program will terminate
now.");
System.exit(1);
}
return
response.equalsIgnoreCase("yes");
}
private static int childrens()
{
int
childrenCount = 0;
String childrens =
JOptionPane.showInputDialog("How many childrens do you
have: ");
try
{
childrenCount =
Integer.parseInt(childrens);
if (childrenCount >= 0) return
childrenCount;
else {
JOptionPane.showMessageDialog(null,"Count
cannot be negative. Program will terminate now.");
System.exit(1);
}
} catch
(NumberFormatException e) {
JOptionPane.showMessageDialog(null,"You
didnt entered a valid number. Program will terminate
now.");
System.exit(1);
}
return
0;
}
private static boolean
isSmoker() {
String response =
JOptionPane.showInputDialog("Do you smoke tobacco (yes/no)?
");
if
(response.trim().length() == 0 ||
!(response.equalsIgnoreCase("yes") ||
response.equalsIgnoreCase("no"))) {
JOptionPane.showMessageDialog(null,"You
need to enter either yes or no. " +
"You didnt entered a valid response. Program will terminate
now.");
System.exit(1);
}
return
response.equalsIgnoreCase("yes");
}
}