Java Write the EmployeePayroll class to read an input file of Employee payroll i
ID: 3670840 • Letter: J
Question
Java
Write the EmployeePayroll class to read an input file of Employee payroll information, process the payroll for each Employee, and then write out information to an output file. The input file will give you a list of Employees of a company which you need to pay – each line in the file will give you all the information you need to process each Employee. There will be just two types of Employees – Hourly and Salary. Hourly Employees get paid by the hour, and 50% more if they work more than 40 hours. Salary Employees get paid biweekly, which is 1/26 of their annual salary, regardless of how many hours they work.
The data in the input file is delimited by the percentage sign character %, and the file gives all the information to identify the Employee and compute his or her pay.
Each line for an Hourly Employee will list the following information, in this format (firstName, lastName, and the String “Hourly” are Strings, hourlyRate, hoursWorked, fedTaxRate, and stateTaxRate are doubles):
firstName%lastName%employeeID%Hourly%hourlyRate%hoursWorked%fedTaxRate%stateTaxRate
Each line for a Salary Employee will list the following information, in this format (firstName, lastName, and the String Salary are Strings, annualSalary, fedTaxRate, and stateTaxRate are doubles):
firstName%lastName%employeeID%Salary%annualSalary%fedTaxRate%stateTaxRate
Your program must
1. Prompt the user to enter input and output file names from the command line and then scan those inputs. As always, you should use appropriate variable names throughout the program, exactly as in Exercise 13;
2. Read all the lines from the input file by chaining a BufferedReader to a FileReader, and place each line into successive ArrayList elements, and count the number of lines read, exactly as in Exercise 13;
3. As each line is read, print it out at the command line - when done, print out to the command line how many lines are read, exactly as in Exercise 13;
4. Scan each read line and separate each %-delimited String token into a separate variable of the appropriate type – explicitly set the delimiter to a blank using useDelimiter(“%“) when you instantiate your Scanner;
5. Write separate methods compute and return:
a. The pay for that pay period, based on the Employee type (hourlyRate*hoursWorked for Hourly and annualSalary/26 for Salary)
b. The federal tax withheld (pay*fedTaxRate)
c. The state tax withheld (pay*stateTaxRate)
6. As you process the payroll for each Employee, call the methods to compute pay, fedTaxWithheld, and stateTaxWithheld, and write out to both the command-line and the output file all the input file information PLUS the following. Use the same format as the input, appending %-delimited values for pay, fedTaxWithheld, and stateTaxWithheld – so for example, an Hourly Employee output line would read:
Ernie%Ball%123456%Salary%156000.00%30.00%6.00%6000.0%1800.0%360
7. At the command line, also print a count of lines written to the output file.
8. Catch a FileNotFoundException, an IOException, and a general Exception. In the catch blocks, print appropriate messages for the user at the command line.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeePayroll {
public static void main(String args[])
{
try
{
//Input the input and output file names
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the input file name");
String inputFile=br.readLine();
System.out.println("Enter the output file name");
String outputFile=br.readLine();
//Arraylist of strings storing each line from input file
ArrayList<String> employees=new ArrayList<String>();
BufferedReader fr=new BufferedReader(new FileReader(inputFile));
String line;
int count=0;
//reading the input file line by line and stroing each line in array list
while((line=fr.readLine())!=null)
{
employees.add(line);
System.out.println(line);
count++;
}
fr.close();
System.out.println("Number of lines read are "+count);
//Variable declaration for processing each employee data
int countOutput=0;
String firstName,lastName,employeeId,employeeType;
double hourlyRate=0,hoursWorked=0,fedTaxRate=0,stateTaxRate=0,annualSalary=0;
double pay=0,stateTax=0,fedTax=0;
String output = "";
//Intializing output file,creating a new one if it doesn't exist
File file=new File(outputFile);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
//Iterating over the array list and processing each line i.e employees data
for(int i=0;i<employees.size();++i)
{
output="";
String employee=employees.get(i);
//seperating tokens in string using delimiter
Scanner s=new Scanner(employee).useDelimiter("%");
firstName=s.next();
lastName=s.next();
employeeId=s.next();
employeeType=s.next();
if(employeeType.equalsIgnoreCase("Hourly"))
{
hourlyRate=s.nextDouble();
hoursWorked=s.nextDouble();
fedTaxRate=s.nextDouble();
stateTaxRate=s.nextDouble();
pay=computeHourlyPay(hourlyRate,hoursWorked);
fedTax=computeFederalTax(pay,fedTaxRate);
stateTax=computeStateTax(pay,stateTaxRate);
output+=firstName+"%"+lastName+"%"+employeeId+"%"+employeeType+"%"+hourlyRate
+"%"+hoursWorked+"%"+fedTaxRate+"%"+stateTaxRate+"%"+pay+"%"+fedTax+"%"+stateTax;
System.out.println(output);
}
else
{
annualSalary=s.nextDouble();
fedTaxRate=s.nextDouble();
stateTaxRate=s.nextDouble();
pay=computeSalaryPay(annualSalary);
fedTax=computeFederalTax(pay,fedTaxRate);
stateTax=computeStateTax(pay,stateTaxRate);
output+=firstName+"%"+lastName+"%"+employeeId+"%"+employeeType+"%"+annualSalary
+"%"+fedTaxRate+"%"+stateTaxRate+"%"+pay+"%"+fedTax+"%"+stateTax;
System.out.println(output);
}
s.close();
//writing the processed pay and tax data into ouput file
bw.write(output);
bw.newLine();
countOutput++;
}
bw.close();
System.out.println("Number of employees data written in file "+countOutput);
}
catch(FileNotFoundException ex)
{
System.out.println("File not found");
}
catch(IOException ex)
{
System.out.println("An input output exception happened");
}
catch(Exception ex)
{
System.out.println("Some unknown exception happened "+ex.getMessage());
}
}
//static functions for computing pay,federalTax and stateTax
public static double computeHourlyPay(double hourlyRate,double hoursWorked)
{
double pay=hourlyRate*hoursWorked;
if(hoursWorked>40)
{
pay+=pay*0.5;
return pay;
}
return pay;
}
public static double computeSalaryPay(double annualSalary)
{
return annualSalary/26;
}
public static double computeFederalTax(double pay,double fedTaxRate)
{
return pay*fedTaxRate/100;
}
public static double computeStateTax(double pay,double stateTaxRate )
{
return pay*stateTaxRate/100;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.