In java, write a program that completes the two tasks below. The program should
ID: 3887380 • Letter: I
Question
In java, write a program that completes the two tasks below. The program should take input from the CSV file listed below to perform the tasks. Also, provide a brief explanation as to how the code works AND how to run your code (how would someone run the program if they have Java and the CSV file.
Task 1: List people who work in the same department as “Adcock Sr Gerald W”- himself excluded.
Task 2: List all the departments and total number of employees working in that department.
.CSV File: I had to post it this way since chegg does not allow for the attachment of files.
https://drive.google.com/file/d/0B6wUR2OjN6GcZldwN1YzMlo2YWs/view?usp=sharing
Explanation / Answer
HI.
1. Employees.java
public class Employee {
private String fullName;
private String currentAnnualSalary;
private String grossPayReceived;
private String overtimePay;
private String department;
private String division;
private String assignmentCategory;
private String positionTitle;
private String dateFirstHired;
public Employee(String fullName, String currentAnnualSalary, String grossPayReceived,
String overtimePay, String department, String division, String assignmentCategory,
String positionTitle, String dateFirstHired){
this.fullName = fullName;
this.currentAnnualSalary = currentAnnualSalary;
this.grossPayReceived = grossPayReceived;
this.overtimePay = overtimePay;
this.department = department;
this.division = division;
this.assignmentCategory = assignmentCategory;
this.positionTitle = positionTitle;
this.dateFirstHired = dateFirstHired;
}
@Override
public String toString(){
return "Employee [full name= "+fullName
+" , currentAnnualSalary="+currentAnnualSalary
+" , grossPayReceived="+grossPayReceived
+" , overtimePay="+overtimePay
+" , department="+department
+" , division="+division
+" , assignmentCategory="+assignmentCategory
+" , positionTitle="+positionTitle
+" , dateFirstHired="+dateFirstHired+"]";
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getCurrentAnnualSalary()
{
return currentAnnualSalary;
}
public void setCurrentAnnualSalary(String currentAnnualSalary)
{
this.currentAnnualSalary = currentAnnualSalary;
}
public String getGrossPayReceived()
{
return grossPayReceived;
}
public void setGrossPayReceived(String grossPayReceived)
{
this.grossPayReceived = grossPayReceived;
}
public String getOvertimePay()
{
return overtimePay;
}
public void setOvertimePay(String overtimePay)
{
this.overtimePay = overtimePay;
}
public String getDepartment()
{
return department;
}
public void setDepartment(String department)
{
this.department = department;
}
public String getDivision()
{
return division;
}
public void setDivision(String division)
{
this.division = division;
}
public String getAssignmentCategory()
{
return assignmentCategory;
}
public void setAssignmentCategory(String assignmentCategory)
{
this.assignmentCategory = assignmentCategory;
}
public String getPositionTitle()
{
return positionTitle;
}
public void setPositionTitle(String positionTitle)
{
this.positionTitle = positionTitle;
}
public String getDateFirstHired()
{
return dateFirstHired;
}
public void setDateFirstHired(String dateFirstHired)
{
this.dateFirstHired = dateFirstHired;
}
}
2. EmployeesDriver.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class EmployeesDriver {
static ArrayList<Employee> employeeList = new ArrayList<>();
static ArrayList<String> departmentList = new ArrayList<>();
public static void main(String[] args)
{
String csvFile = "employee.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
line = br.readLine();// read column names;
while ((line = br.readLine())!=null) {
// use comma as separator
String[] employee = line.split(cvsSplitBy);
// add employee info into Employee class object
Employee emp = new Employee(employee[0],employee[1],employee[2],employee[3],employee[4],employee[5],employee[6],employee[7],employee[8]);
// add emp object into employeeList
employeeList.add(emp);
// check Department name already in DepartmentList
if (!departmentList.contains(employee[4])) {
// if not found in DepartmentList then add in DepartmentList
departmentList.add(employee[4]);
}
}
}
catch (IOException e){
System.out.println("Can not read file");
}
task1();
System.out.println("================================");
task2();
}
private static void task1(){
String department = "";
for (int i = 0; i<employeeList.size(); i++) {
// get employee object of Adcock Sr Gerald W
Employee emp = employeeList.get(i);
// find Adcock Sr Gerald W department's name
if (emp.getFullName().equals("Adcock Sr Gerald W")) {
// if not found in employeeList then get Department
department = emp.getDepartment();
}
}
System.out.println("List people who work in the same department as “Adcock Sr Gerald W”");
for (int j = 0; j<employeeList.size(); j++) {
// read one employee object from employeeList
Employee emp = employeeList.get(j);
// check Adcock Sr Gerald W department's with other employee department's
if (department.equals(emp.getDepartment())) {
//System.out.println(emp.toString());// if print all detail of employee please comment line
// if same department's of Adcock Sr Gerald W then print name of employee
System.out.println(emp.getFullName());
}
}
}
private static void task2(){
int totalNoOfEmpDepartments = 0;
System.out.println("List all the departments");
for (int i = 0; i<departmentList.size(); i++) {
// get department name from departmentList
totalNoOfEmpDepartments = 0;
String dept = departmentList.get(i);
for (int j = 0; j<employeeList.size(); j++) {
// read one employee object from employeeList
Employee emp = employeeList.get(j);
if (dept.equals(emp.getDepartment())) {
// if dept match with employee department increase value of totalNoOfEmpDepartments
totalNoOfEmpDepartments ++;
}
}
// print total not of employee in department
System.out.println("Department name: "+dept +", Total no of employees working in : "+totalNoOfEmpDepartments);
}
}
}
Let me know if any change required
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.