Construct a java program that will retrieve, update and manipulate a small payro
ID: 3828429 • Letter: C
Question
Construct a java program that will retrieve, update and manipulate a small payroll database. The payroll data (payfile.txt) along with two additional files ( hirefile.txt, firefile.txt) required for this lab can be found down at the bottom of this question.
The program you are writing should do each of the following:
a) Read each line of data from payfile.txt, place the data into an Employee object, and insert the employee object onto the end of an Objectlist.
b) Output the contents of the info field of each ObjectListNode into an easily read table format with each field appropriately labeled.
c) Output the number of employees.
d)Output the first and last name of all women on the payroll.
e)Output the first and last names and salary of all weekly employees who make more than $35,000 per year and who have been with the company for at least five years.
f) Give a raise of $.75 per hour to all employees who are paid on an hourly basis and make less than $10.00 per hour; and give a raise of $50.00 per week to all employees who are paid on a weekly basis and make less than $350.00 per week. Be sure to output the first and last names and new salaries for each employee on the payroll who has received a raise.
g) Sort the nodes of the linked list into alphabetical order according to last/first name and print the first and last names and salaries for each employee on the payroll.
h) The file hirefile.txt contains data for three employees to be hired by the company. Insert each of the new employees into the correct location in the sorted linear linked list and print the first and last names for each employee on the payroll.
i) the file firefile.txt contains data for two employees to be fired by the company. Delete the corresponding nodes in the sorted linear linked list for each of the employees to be fired and print the first and last names for each employee on the payroll.
Here are the necessary files:
Payfile.txt
Howard Starr M 8 H 30.00
Joan Jacobus F 9 W 925.00
David Renn M 3 H 4.75
Albert Cahana M 3 H 18.75
Douglas Sheer M 5 W 250.00
Shari Buchman F 9 W 325.00
Sara Jones F 1 H 7.50
Ricky Mofsen M 6 H 12.50
Jean Brennan F 6 H 5.40
Deborah Starr F 3 W 1000.00
Jamie Michaels F 8 W 150.00
hirefile.txt
Barry Allen M 0 H 6.75
Nina Pinella F 0 W 425.00
Lane Wagger M 0 W 725.00
firefile.txt
Jean Brennan F
Ricky Mofsen M
SIDE NOTES:
The classes used should contains at least the following:
ObjectListNode ObjectList Employee Driver Payroll
Here is an algorithm given to sort your linked lists:
While (list != null)
remove the first node
insert node into newList
List = newList
Please use java docs for every method.
Thank you for your help.
Explanation / Answer
PayrollTest.java
package payroll;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PayrollTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File payfile = new File("D:/workspace/chegg/src/payroll/payfile.txt");
File hirefile = new File("D:/workspace/chegg/src/payroll/hirefile.txt");
File firefile = new File("D:/workspace/chegg/src/payroll/firefile.txt");
List<Employee> employees = readFile(payfile);
System.out.println("Number of Employees: " + employees.size());
System.out.println("First and Last Names of Women Employees:");
for (Employee employee : employees) {
if (employee.getGender() == 'F')
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName());
}
System.out.println(
" First and Last Names of weekly Employees with Annual salary>35000$ and experience>=5 years ");
for (Employee employee : employees) {
if (employee.getEmpType() == 'W') {
double annualSalary = employee.getSalaryPaid() * 56;
if (annualSalary > 35000 && employee.getExp() >= 5)
System.out.println(
"First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName());
}
}
System.out.println(
" First and Last Names of Employees with hike of .75$ per hour for hourly making less than 10$ and 50$ for weekly employees making less that 350 per week ");
for (Employee employee : employees) {
if (employee.getEmpType() == 'H' && employee.getSalaryPaid() <= 10) {
employee.setSalaryPaid(employee.getSalaryPaid() + .75);
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName()
+ " Revised Salary: " + employee.getSalaryPaid());
}
if (employee.getEmpType() == 'W' && employee.getSalaryPaid() <= 350) {
employee.setSalaryPaid(employee.getSalaryPaid() + 50);
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName()
+ " Revised Salary: " + employee.getSalaryPaid());
}
}
Collections.sort(employees, new Employee());
System.out.println(" First and Last Names of Employees sorted based on first Name ");
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName()
+ " Salary: " + employee.getSalaryPaid());
}
List<Employee> newHirees = readFile(hirefile);
// Add new Hired employees
employees.addAll(newHirees);
Collections.sort(employees, new Employee());
System.out.println(" First and Last Names of Employees sorted based on first Name after adding new hires ");
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName()
+ " Salary: " + employee.getSalaryPaid());
}
List<Employee> firedEmployees = readFile(firefile);
System.out.println(" First and Last Names of Employees After removing fired employees ");
for (Employee fired : firedEmployees) {
String firstName=fired.getFirstName();
String lastname=fired.getLastName();
for (Employee employee : employees) {
if (employee.getFirstName().trim().equals(firstName)
&& employee.getLastName().equals(lastname)) {
employees.remove(employee);
}
}
}
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName());
}
}
/**
* @param file
* @return list of employees read from the txt files
*/
public static List<Employee> readFile(File file) {
List<Employee> employees = new ArrayList<Employee>();
Scanner in;
try {
in = new Scanner(file);
String line;
// while file has lines
while (in.hasNextLine()) {
Employee employee = null;
line = in.nextLine();
String tokens[] = line.split("\s+");
// condition to satisfy fired employees
if (tokens.length <= 3) {
employee = new Employee();
employee.setFirstName(tokens[0]);
employee.setLastName(tokens[1]);
employee.setGender(tokens[2].trim().charAt(0));
} else {
employee = new Employee(tokens[0], tokens[1], tokens[2].trim().charAt(0),
Integer.parseInt(tokens[3]), tokens[4].trim().charAt(0), Double.parseDouble(tokens[5]));
}
employees.add(employee);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employees;
}
}
Employee.java
package payroll;
import java.util.Comparator;
/**
* @author
*
*/
public class Employee implements Comparator<Employee>{
private String firstName;
private String lastName;
private char gender;
private char empType;
private double salaryPaid;
private int exp;
public Employee(){
}
/**
* @param firstName
* @param lastname
* @param gender
* @param exp
* @param empType
* @param salary
*/
public Employee(String firstName, String lastname,char gender, int exp,char empType, double salary){
this.firstName=firstName;
this.lastName=lastname;
this.gender=gender;
this.exp=exp;
this.empType=empType;
this.salaryPaid=salary;
}
/**
* @return firstName of Employee
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return last name
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return
*/
public char getGender() {
return gender;
}
/**
* @param gender
*/
public void setGender(char gender) {
this.gender = gender;
}
/**
* @return employee type as W for weekly and H for hourly
*/
public char getEmpType() {
return empType;
}
/**
* @param empType
*/
public void setEmpType(char empType) {
this.empType = empType;
}
/**
* @return salary paid
*/
public double getSalaryPaid() {
return salaryPaid;
}
/**
* @param salaryPaid
*/
public void setSalaryPaid(double salaryPaid) {
this.salaryPaid = salaryPaid;
}
/**
* @return no of years of experience
*/
public int getExp() {
return exp;
}
/**
* @param exp
*/
public void setExp(int exp) {
this.exp = exp;
}
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Employee arg0, Employee arg1) {
return arg0.getFirstName().compareTo(arg1.getFirstName());
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
return " FirstName: "+firstName+" Last Name: "+lastName+" Gender: "+gender+" Experience: "+exp+"years Salary: $"+salaryPaid+" per "+empType;
}
}
Sample Output
Number of Employees: 11
First and Last Names of Women Employees:
First Name: Joan Last Name: Jacobus
First Name: Shari Last Name: Buchman
First Name: Sara Last Name: Jones
First Name: Jean Last Name: Brennan
First Name: Deborah Last Name: Starr
First Name: Jamie Last Name: Michaels
First and Last Names of weekly Employees with Annual salary>35000$ and experience>=5 years
First Name: Joan Last Name: Jacobus
First and Last Names of Employees with hike of .75$ per hour for hourly making less than 10$ and 50$ for weekly employees making less that 350 per week
First Name: David Last Name: Renn Revised Salary: 5.5
First Name: Douglas Last Name: Sheer Revised Salary: 300.0
First Name: Shari Last Name: Buchman Revised Salary: 375.0
First Name: Sara Last Name: Jones Revised Salary: 8.25
First Name: Jean Last Name: Brennan Revised Salary: 6.15
First Name: Jamie Last Name: Michaels Revised Salary: 200.0
First and Last Names of Employees sorted based on first Name
First Name: Albert Last Name: Cahana Salary: 18.75
First Name: David Last Name: Renn Salary: 5.5
First Name: Deborah Last Name: Starr Salary: 1000.0
First Name: Douglas Last Name: Sheer Salary: 300.0
First Name: Howard Last Name: Starr Salary: 30.0
First Name: Jamie Last Name: Michaels Salary: 200.0
First Name: Jean Last Name: Brennan Salary: 6.15
First Name: Joan Last Name: Jacobus Salary: 925.0
First Name: Ricky Last Name: Mofsen Salary: 12.5
First Name: Sara Last Name: Jones Salary: 8.25
First Name: Shari Last Name: Buchman Salary: 375.0
First and Last Names of Employees sorted based on first Name after adding new hires
First Name: Albert Last Name: Cahana Salary: 18.75
First Name: Barry Last Name: Allen Salary: 6.75
First Name: David Last Name: Renn Salary: 5.5
First Name: Deborah Last Name: Starr Salary: 1000.0
First Name: Douglas Last Name: Sheer Salary: 300.0
First Name: Howard Last Name: Starr Salary: 30.0
First Name: Jamie Last Name: Michaels Salary: 200.0
First Name: Jean Last Name: Brennan Salary: 6.15
First Name: Joan Last Name: Jacobus Salary: 925.0
First Name: Lane Last Name: Wagger Salary: 725.0
First Name: Nina Last Name: Pinella Salary: 425.0
First Name: Ricky Last Name: Mofsen Salary: 12.5
First Name: Sara Last Name: Jones Salary: 8.25
First Name: Shari Last Name: Buchman Salary: 375.0
First and Last Names of Employees After removing fired employees
Only the last point is pending.