The Company you work for has asked you to write a program that reads some hourly
ID: 650813 • Letter: T
Question
The Company you work for has asked you to write a program that reads some hourly employee pay information from a file, calculates their Net Pay, and saves a report to a file. The input file is called projldata.txt, and is in the folder S:mackayCSl11 There are only 3 hourly employees in the company, so there are only 3 lines of information in the mput file Each line of input contains the following information for each employee, separated by spaces: First name (no spaces) Last name (no spaces) Hourly pay rate Number of hours worked Whether or not they are in the union ('Y' or 'N') Whether or not they have health insurance ('Y' or 'N') If they do have health insurance, there will be another integer indicating how many dependents they have Joe Smith 12.75 48 YN Susan Storm 18.64 39.5 N Y 3 You must calculate the employees net pay based on the following: Their total earned is the rate times the hours If they are in the union, the union dues, $24, must be deducted from their pay If they have health insurance, their insurance premium of $10.34 per dependent must be deducted from their pay The output will be stored in a file called projlResultsFirstLasts.txt, where First and are YOUR first name and last name. The output will have this format: (using the example mput from above) The employess last name and first name should be left justified All the other information will be right justified, with 2 decimal places There should only be 1 header roe Getting the output to format exactly will be difficult, because Wordpad, even though called a 'plain' text editor, uses fonts in which different characters have different widths You May use a loop in this program, but you do not have to If you do not the code for input and output will be repeated 3 times.Explanation / Answer
//Employee.java
public class Employee
{
private String firstName;
private String lastName;
private double hourlyPayRate;
private double hoursWorked;
private boolean inUnion;
private boolean hasInsurance;
private int dependent;
//Default constructor that sets the default values
public Employee()
{
firstName="";
lastName="";
hourlyPayRate=0;
hoursWorked=0;
inUnion=false;
hasInsurance=false;
dependent=0;
}
//Parameterized constructor that sets the values to the class variales
public Employee(String firstName,String lastName,
double hourlyPayRate,double hoursWorked,
boolean inUnion,boolean hasInsurance,
int dependent)
{
this.firstName=firstName;
this.lastName=lastName;
this.hourlyPayRate=hourlyPayRate;
this.hoursWorked=hoursWorked;
this.inUnion=inUnion;
this.hasInsurance=hasInsurance;
this.dependent=dependent;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public String getLastName()
{
return lastName;
}
public void setRate(double rate)
{
hourlyPayRate=rate;
}
public double getRate()
{
return hourlyPayRate;
}
public void setHours(double hours)
{
hoursWorked=hours;
}
public double getHours()
{
return hoursWorked;
}
public void setUnion(boolean unionStatus)
{
inUnion=unionStatus;
}
public boolean getUnionStatus()
{
return inUnion;
}
public void setInsurance(boolean insuranceStatus)
{
hasInsurance=insuranceStatus;
}
public boolean getInsuranceStatus()
{
return hasInsurance;
}
public void setDependent(int dependent)
{
this.dependent=dependent;
}
public int getDependent()
{
return dependent;
}
}//end of the class
----------------------------------------------------------------------------------------------------------------------------------
/**
* Java program that reads a text file called "proj1data.txt"
* that contains employee data. The calculates the net pay of the
* employee. The write last name, first name, total earned, union due ,insurance
* and net pay to the file "Proj1ResultsFirstLast.txt".
*
* */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class EmployeeDriver
{
//union fee
private static double UNION_DUE=24.00;
//insurance per head
private static double INSURANCE_PER_HEAD=10.34;
public static void main(String[] args)
{
String fileName="proj1data.txt";
File file=null;
Scanner fileScanner=null;
//Create a Employee class variable
Employee employee=null;
String firstName;
String lastName;
double hourlyPayRate;
double hoursWorked;
boolean inUnion = false;
boolean hasInsurance = false;
int dependent = 0;
try
{
//Open file for reading input form file
file=new File(fileName);
fileScanner=new Scanner(file);
//Open a output file to write data to file Proj1ResultsFirstLast.txt
PrintWriter fileWriter=new PrintWriter("Proj1ResultsFirstLast.txt");
fileWriter.printf("%-15s%-15s%10s%15s%10s%10s","Last Name","First Names","Total Earned",
"Union Dues","Insurance","Net Pay");
fileWriter.println();
//read text data from file
while(fileScanner.hasNextLine())
{
String line=fileScanner.nextLine();
//split the line of text as space delimiter
StringTokenizer tokens=new StringTokenizer(line, " ");
//get tokens of text from the file
firstName=tokens.nextToken();
lastName=tokens.nextToken();
hourlyPayRate=Double.parseDouble(tokens.nextToken());
hoursWorked=Double.parseDouble(tokens.nextToken());
char union=tokens.nextToken().charAt(0);
if(union=='Y')
inUnion=true;
else if(union=='N')
{
inUnion=false;
UNION_DUE=0;
}
char insurance=tokens.nextToken().charAt(0);
if(insurance=='Y')
hasInsurance=true;
else if(insurance=='N')
hasInsurance=false;
if(hasInsurance)
dependent=Integer.parseInt(tokens.nextToken());
//Instantiate employee object
employee=new Employee(firstName, lastName,
hourlyPayRate, hoursWorked, inUnion, hasInsurance, dependent);
//call calculateNetPay that accepts the employee as object and
//return net pay of employye
double netpay=calculateNetPay(employee);
//write information to file
fileWriter.printf("%-15s%-15s%10.2f%15.2f%10.2f%10.2f",
lastName,
firstName,
employee.getRate()*employee.getHours(),
UNION_DUE,
employee.getDependent()*INSURANCE_PER_HEAD,
netpay
);
//write a new line to file
fileWriter.println();
//reset
}
//close output fileWriter stream
fileWriter.close();
}
catch (FileNotFoundException e)
{
System.out.println("Input file not found");
}
}
//The method calculateNetPay that takes employee as its object
//and returns the net pay of the employee after deducting union amount
//and insurance amount from total amount
private static double calculateNetPay(Employee employee)
{
double netPay=0;
double totalPay=employee.getRate()*employee.getHours();
//check if employee is in union
if(employee.getUnionStatus())
//deduct union pay
totalPay=totalPay-UNION_DUE;
//check if employee has insurance
if(employee.getInsuranceStatus())
//deducnt insurance per head from total pay
totalPay=totalPay-employee.getDependent()*INSURANCE_PER_HEAD;
netPay=totalPay;
return netPay;
}
}
------------------------------------------------------------------------------------------------------------------------
sample input file
proj1data.txt
Joe Smith 12.75 48 Y N
Susan Storm 18.64 39.5 N Y 3
maria shan 25.75 48 Y N
output file:
Last Name First Names Total Earned Union Dues Insurance Net Pay
Smith Joe 612.00 24.00 0.00 588.00
Storm Susan 736.28 0.00 31.02 705.26
shan maria 1236.00 0.00 31.02 1236.00
Note : This program is assumed to be java .
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.