You have been asked to write a program to calculate employee pay amounts for an
ID: 3854540 • Letter: Y
Question
You have been asked to write a program to calculate employee pay amounts for an organization from a batch file. There are two classification of employees: staff and managers. All employees have a unique alphanumeric employee ID but the first letter of the ID distinguishes their classification (s, t, or f for staff or m, n, g, or r for managers - eg. s10013 or m871452). If the ID ends in the letter z or Z that means the employee has chosen to contribute 2% of their gross pay to a charity. Staff are paid hourly and managers are paid salary. Pay checks are issued every two id salary. Pay checks are issued every two weeks. Staff members are paid for the actual hours worked, managers are paid 1/26h of t rs are paid for the actual hours worked, managers are paid 1/26t of their annual salary. If a staf person works more than 80 hours then any additional hours are paid at 1.5 times their base hourly rate. Staff can't work more than 30 hours of overtime. For all employees, the standard tax rate is 15% of gross pay and the standard health insurance contribution rate is 5% of gross pay. If an employee makes more than $1,500 gross pay then their tax rate goes to 20%. If an employee makes more than $2,500 gross pay then their tax rate goes to 25% Managers also contribute 5% of their gross pay to a retirement account. Any employee may earn a bonus of up to 10% of their gross pay that would be added to the gross pay for tax and insurance purposes Your program should open an existing batch file containing employee data in comma delimited format. Your program should implement individual methods to calculate tax, benefit, overtime, bonus, charitable contributions, and net pay. Add these fields along with corresponding data for each record to the original file, including the header. Use 0 for the optional fields if there is no value (e-g. person does t contribute to a charitable organization so enter O). Also add a column for date and enter today's date (7/6/2017) for all records. Any values that represent currency should be formatted to 2 decimal places In a separate file, name it lastnamelabFinal.txt, print out a printer friendly version of the data you processed, sorted by employee last name A->Z. (Print all employee records to the same file.) Use array(s) to store the data from the above step so you don't have to perform the calculations more than once. You should create a method to create the file and save the data pass the array(s) to this method. Any values that represent currency should be formatted to 2 decimal places.Explanation / Answer
Here is the completed code for the question. I have tested it with a small subset of the data. The input file itself will get modified and new computed values are added to it according to the question requirements. Also a new file with sorted data is present in lastnameLabFinal.txt.
In case of any issues, please post a comment. If happy with the answer, please rate it. Thank you.
package LabExam3;
import java.util.Date;
public class Employee {
private String empID;
private String firstName;
private String lastName;
private double payRate;
private double hours;
private double bonusRate;
private double grosspay;
private double bonus;
private double overtime;
private double tax;
private double insurance;
private double retirement;
private double charity;
private double netpay;
private Date date;
public Employee(String empId)
{
this.empID = empId;
}
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getPayRate() {
return payRate;
}
public void setPayRate(double payRate) {
this.payRate = payRate;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public double getBonusRate() {
return bonusRate;
}
public void setBonusRate(double bonusRate) {
this.bonusRate = bonusRate;
}
public double getInsurance() {
return insurance;
}
public void setInsurance(double insurance) {
this.insurance = insurance;
}
public double getRetirement() {
return retirement;
}
public void setRetirement(double retirement) {
this.retirement = retirement;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getGrossPay()
{
return grosspay;
}
public double getBonus() {
return bonus;
}
public double getOvertime() {
return overtime;
}
public double getTax() {
return tax;
}
public double getCharity() {
return charity;
}
public double getNetpay() {
return netpay;
}
public String getEmployeeType()
{
char first = empID.charAt(0);
if(first == 'm' || first == 'n' || first =='g' || first == 'r')
return "Manager";
else if(first == 's' || first == 't' || first =='f')
return "Staff";
else
return null;
}
public String getPayType()
{
if(getEmployeeType().equals("Manager"))
return "Salary";
else
return "Hourly";
}
public void compute()
{
calculateGrosspay();
calculateCharity();
calculateBonus();
calculateTax();
calculateRetirement();
}
private void calculateGrosspay()
{
if(getEmployeeType().equals("Staff"))
{
if(hours > 80)
{
grosspay = payRate * 80;
calculateOvertime();
}
else
{
grosspay = payRate * hours;
}
}
else if(getEmployeeType().equals("Manager"))
{
grosspay = payRate / 26;
}
}
private void calculateOvertime()
{
double overtimeHrs = 0;
if(hours > 80)
{
overtimeHrs = hours - 80;
if(overtimeHrs > 30)
overtimeHrs = 30;
}
overtime = overtimeHrs * payRate * 1.5;
}
private void calculateBonus()
{
bonus = grosspay * bonusRate / 100;
}
private void calculateCharity()
{
if(empID.endsWith("z"))
{
charity = grosspay * 0.02; // 2% of grosspay
}
else
charity = 0;
}
private void calculateInsurance()
{
insurance = grosspay * 0.05; // 5% of grosspay
}
private void calculateTax()
{
if(grosspay > 2500)
tax = grosspay * 0.25 ; // 25 % of grosspay
else if (grosspay > 1500)
tax = grosspay * 0.20 ; // 20 % of grosspay
else
tax = grosspay * 0.15; //15 % of grosspay
}
private void calculateRetirement()
{
if(getEmployeeType().equals("Manager"))
retirement = grosspay * 0.10; //10 % of grosspay
else
retirement = 0;
}
public double getNetPay()
{
return (grosspay + bonus + overtime) - (charity + insurance + retirement + tax);
}
}
package LabExam3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class ProcessEmployees {
private static int loadData(String filename, Employee employees[]) throws FileNotFoundException
{
int count = 0;
Scanner filescanner = new Scanner(new File(filename));
filescanner.nextLine(); // read the header line
while(filescanner.hasNextLine())
{
String line = filescanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
employees[count] = new Employee(lineScanner.next());
employees[count].setFirstName(lineScanner.next());
employees[count].setLastName(lineScanner.next());
employees[count].setPayRate(lineScanner.nextDouble());
employees[count].setHours(lineScanner.nextDouble());
employees[count].setBonusRate(lineScanner.nextDouble());
lineScanner.close();
count++;
}
filescanner.close();
return count;
}
public static void displayEmployee(Employee e)
{
System.out.println(" EMPLOYEE: " + e.getLastName() +", " + e.getFirstName());
System.out.println("ID: " + e.getEmpID());
System.out.println("CLASSIFICATION: " + e.getEmployeeType());
System.out.println("PAY TYPE: " + e.getPayType());
System.out.printf("GROSS: %.2f " , e.getGrossPay());
System.out.printf("BONUS: %.2f ", e.getBonus());
System.out.printf("OVERTIME: %.2f ", e.getOvertime());
System.out.printf("TAX: %.2f ", e.getTax());
System.out.printf("INSURANCE: %.2f ", e.getInsurance());
System.out.printf("RETIREMENT: %.2f ", e.getRetirement());
System.out.printf("CHARITY: %.2f ", e.getCharity());
System.out.printf("NETPAY: %.2f ", e.getNetPay());
System.out.println(" ************************************** ");
}
private static void sortEmployees(Employee emps[], int count)
{
int minIdx ;
for(int i = 0; i < count; i++)
{
minIdx = i;
for(int j = i + 1; j < count; j++)
{
if(emps[j].getLastName().compareToIgnoreCase(emps[minIdx].getLastName()) < 0)
minIdx = j;
}
if(minIdx != i)
{
Employee temp = emps[i];
emps[i] = emps[minIdx];
emps[minIdx] = temp;
}
}
}
private static void saveData(String filename, Employee emps[], int count) throws FileNotFoundException
{
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
PrintWriter writer = new PrintWriter(new File(filename));
Employee e;
writer.write("empID,firstName,lastName,payRate,hours,bonusRate,grossPay,bonus,overtime,tax,insurance,retirement,charity,netpay,date ");
for(int i=0 ; i < count ;i++)
{
e = emps[i];
writer.printf("%s,%s,%s,%.2f,%.2f,", e.getEmpID(), e.getFirstName(), e.getLastName(), e.getPayRate(),e.getHours());
writer.printf("%.2f,%.2f,%.2f,%.2f,",e.getBonusRate(), e.getGrossPay(),e.getBonus() , e.getOvertime());
writer.printf("%.2f,%.2f,%.2f,%.2f,%.2f,%s ",e.getTax(), e.getInsurance(), e.getRetirement(), e.getCharity(), e.getNetpay(), sdf.format(e.getDate()));
}
writer.close();
}
public static void main(String[] args) {
String filename = "LabExam3Data.txt";
Employee employees[] = new Employee[100];
int count = 0;
try {
count = loadData(filename, employees);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
Calendar cal = Calendar.getInstance();
cal.set(2017, 6, 6); //month index start from 0
for(int i = 0; i < count; i++)
{
employees[i].compute();
employees[i].setDate(cal.getTime());
displayEmployee(employees[i]);
}
try {
saveData(filename, employees, count);
sortEmployees(employees, count);
saveData("lastnameLabFinal.txt", employees, count);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
input file: LabExam3Data.txt
empID,firstName,lastName,payRate,hours,bonusRate
s2832z,Mistie,Batterton,15,82.5,0
t5956,Arnoldo,Bourdeau,16,90,1
f7231,Toney,Kinslow,7.5,78,0
m2086z,Paulene,Lamarca,1350.68,0,1
n3254,Isaiah,Stroup,3300.01,0,2
g1636,Kristina,Laymon,1432,0,0
output
EMPLOYEE: Batterton, Mistie
ID: s2832z
CLASSIFICATION: Staff
PAY TYPE: Hourly
GROSS: 1200.00
BONUS: 0.00
OVERTIME: 56.25
TAX: 180.00
INSURANCE: 0.00
RETIREMENT: 0.00
CHARITY: 24.00
NETPAY: 1052.25
**************************************
EMPLOYEE: Bourdeau, Arnoldo
ID: t5956
CLASSIFICATION: Staff
PAY TYPE: Hourly
GROSS: 1280.00
BONUS: 12.80
OVERTIME: 240.00
TAX: 192.00
INSURANCE: 0.00
RETIREMENT: 0.00
CHARITY: 0.00
NETPAY: 1340.80
**************************************
EMPLOYEE: Kinslow, Toney
ID: f7231
CLASSIFICATION: Staff
PAY TYPE: Hourly
GROSS: 585.00
BONUS: 0.00
OVERTIME: 0.00
TAX: 87.75
INSURANCE: 0.00
RETIREMENT: 0.00
CHARITY: 0.00
NETPAY: 497.25
**************************************
EMPLOYEE: Lamarca, Paulene
ID: m2086z
CLASSIFICATION: Manager
PAY TYPE: Salary
GROSS: 51.95
BONUS: 0.52
OVERTIME: 0.00
TAX: 7.79
INSURANCE: 0.00
RETIREMENT: 5.19
CHARITY: 1.04
NETPAY: 38.44
**************************************
EMPLOYEE: Stroup, Isaiah
ID: n3254
CLASSIFICATION: Manager
PAY TYPE: Salary
GROSS: 126.92
BONUS: 2.54
OVERTIME: 0.00
TAX: 19.04
INSURANCE: 0.00
RETIREMENT: 12.69
CHARITY: 0.00
NETPAY: 97.73
**************************************
EMPLOYEE: Laymon, Kristina
ID: g1636
CLASSIFICATION: Manager
PAY TYPE: Salary
GROSS: 55.08
BONUS: 0.00
OVERTIME: 0.00
TAX: 8.26
INSURANCE: 0.00
RETIREMENT: 5.51
CHARITY: 0.00
NETPAY: 41.31
**************************************
modified input file (from program)
empID,firstName,lastName,payRate,hours,bonusRate,grossPay,bonus,overtime,tax,insurance,retirement,charity,netpay,date
s2832z,Mistie,Batterton,15.00,82.50,0.00,1200.00,0.00,56.25,180.00,0.00,0.00,24.00,0.00,7/6/2017
t5956,Arnoldo,Bourdeau,16.00,90.00,1.00,1280.00,12.80,240.00,192.00,0.00,0.00,0.00,0.00,7/6/2017
f7231,Toney,Kinslow,7.50,78.00,0.00,585.00,0.00,0.00,87.75,0.00,0.00,0.00,0.00,7/6/2017
m2086z,Paulene,Lamarca,1350.68,0.00,1.00,51.95,0.52,0.00,7.79,0.00,5.19,1.04,0.00,7/6/2017
n3254,Isaiah,Stroup,3300.01,0.00,2.00,126.92,2.54,0.00,19.04,0.00,12.69,0.00,0.00,7/6/2017
g1636,Kristina,Laymon,1432.00,0.00,0.00,55.08,0.00,0.00,8.26,0.00,5.51,0.00,0.00,7/6/2017
outputfile : lastnameLabFinal.txt
empID,firstName,lastName,payRate,hours,bonusRate,grossPay,bonus,overtime,tax,insurance,retirement,charity,netpay,date
s2832z,Mistie,Batterton,15.00,82.50,0.00,1200.00,0.00,56.25,180.00,0.00,0.00,24.00,0.00,7/6/2017
t5956,Arnoldo,Bourdeau,16.00,90.00,1.00,1280.00,12.80,240.00,192.00,0.00,0.00,0.00,0.00,7/6/2017
f7231,Toney,Kinslow,7.50,78.00,0.00,585.00,0.00,0.00,87.75,0.00,0.00,0.00,0.00,7/6/2017
m2086z,Paulene,Lamarca,1350.68,0.00,1.00,51.95,0.52,0.00,7.79,0.00,5.19,1.04,0.00,7/6/2017
g1636,Kristina,Laymon,1432.00,0.00,0.00,55.08,0.00,0.00,8.26,0.00,5.51,0.00,0.00,7/6/2017
n3254,Isaiah,Stroup,3300.01,0.00,2.00,126.92,2.54,0.00,19.04,0.00,12.69,0.00,0.00,7/6/2017
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.