Java Modification 5: Write a method called addEmployeeToDepartment , which doesn
ID: 3739100 • Letter: J
Question
Java
Modification 5: Write a method called addEmployeeToDepartment, which doesn’t return a value and has 2 parameters, the first is of type int, and the second of type Employee. This method iterates through all the elements in the listOfDepartments instance variable looking for a Department whose departmentID is equal to the int value passed as the first argument. If it finds it, it adds to it a copy of the Employee object passed as the second argument.
Below is the method header:
/**
* The addEmployeeToDepartment method iterates through the
* listOfDepartments ArrayList looking for a Department whose departmentID
* is equal to the int value passed as the first argument.
* If it finds it, it adds the Employee object passed as the second
* argument.
* @param deptID the departmentID to search for in the list.
* @param empObject the Employee object to be added to the department.
*/
public void addEmployeeToDepartment (int deptID, Employee empObject)
{
//provide implementation
}
***************Code******************
public class Company
{
private String companyName;
private ArrayList<Department> listOfDepartments;
public Company(String companyName, ArrayList<Department> listOfDepartments)
{
this.companyName = companyName;
ArrayList<Department> dep = new ArrayList<>();
for (Department d : listOfDepartments)
{
dep.add(new Department(d));
}
this.listOfDepartments = dep;
}
public void addDepartment(Department dept)
{
listOfDepartments.add(new Department(dept));
}
public void setDepartmentManager(int deptID, Manager manObject)
{
for (int i = 0; i < listOfDepartments.size(); i++)
{
Department d = listOfDepartments.get(i);
if (d.getDepartmentID() == deptID)
{
d.setDepartmentManager(new Manager (manObject));
}
}
}
public String getCompanyName()
{
return companyName;
}
public ArrayList<Department> getListOfDepartments()
{
ArrayList<Department> dep = new ArrayList<>();
for (Department d : listOfDepartments)
{
dep.add(new Department(d));
}
return dep;
}
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
public void setListOfDepartments(ArrayList<Department> listOfDepartments)
{
ArrayList<Department> dep = new ArrayList<>();
for (Department d : listOfDepartments)
{
dep.add(new Department(d));
}
this.listOfDepartments = dep;
}
@Override
public String toString()
{
String output = "Company Name: " + companyName + " ";
if (listOfDepartments == null || listOfDepartments.isEmpty())
{
output += "no Departments.";
} else
{
for (Department checkElement : listOfDepartments)
{
output += checkElement.toString();
}
}
return output + " ";
}
}
Explanation / Answer
public class Company { private String companyName; private ArrayList listOfDepartments; public Company(String companyName, ArrayList listOfDepartments) { this.companyName = companyName; ArrayList dep = new ArrayList(); for (Department d : listOfDepartments) { dep.add(new Department(d)); } this.listOfDepartments = dep; } public void addDepartment(Department dept) { listOfDepartments.add(new Department(dept)); } public void setDepartmentManager(int deptID, Manager manObject) { for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.