Payroll System - Phase 2 ***Include javadoc comments for all new methods added t
ID: 3732805 • Letter: P
Question
Payroll System - Phase 2
***Include javadoc comments for all new methods added to the project.***
Company class:
Modification 1: In the constructor, make a copy of the departments parameter before using it to set the listOfDepartments instance variable.
Modification 2: In the setListOfDepartments method, make a copy of the departments parameter before using it to set the listOfDepartments instance variable.
Modification 3: In the getListOfDepartments method, return a copy of the listOfDepartments instance variable.
Modification 4: Write a method called addDepartment, which doesn’t return a value and has 1 parameter of type Department. It adds a copy of the dept parameter to the listOfDepartments instance variable.
The following is the method signature:
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:
Modification 6: Write a method called setDepartmentManager, which doesn’t return a value and has 2 parameters, the first is of type int, and the second of type Manager. 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 calls the setDepartmentManager method on that object passing the manObject parameter.
Code that needs to be Modified:
Company.java
package payrollsystem_phase1;
import java.util.ArrayList;
/**
* The Company class represents a company having multiple departments.
*
* @author Mayelin
*/
public class Company
{
// instance variables
private String companyName;
private ArrayList<Department> listOfDepartments = new ArrayList<>();
/**
* This constructor sets the company's name and list of departments.
* @param name Company's name.
* @param departments List of departments in the company.
*/
public Company(String name, ArrayList<Department> departments)
{
companyName = name;
listOfDepartments = departments;
}
/**
* The getCompanyName method returns the company's name.
* @return The company's name.
*/
public String getCompanyName()
{
return companyName;
}
/**
* The getDepartmentList method returns the company's list of departments.
* @return The company's list of departments as an ArrayList of Department elements.
*/
public ArrayList<Department> getListOfDepartments()
{
return listOfDepartments;
}
/**
* The setCompanyName method sets the name for this company.
* @param name The value to store in the name field for this company.
*/
public void setCompanyName(String name)
{
companyName = name;
}
/**
* The setDepartmentList method sets the list of departments for this company.
* @param departments The value, as an ArrayList of Department elements, to store
* in the list of departments field for this company.
*/
public void setListOfDepartments(ArrayList<Department> departments)
{
listOfDepartments = departments;
}
/**
* The toString method returns a string containing the company's data.
* @return A String containing the Company's information: name and list of
* departments.
*/
@Override
public String toString()
{
String output = String.format("%-30s %s", "Company Name:", companyName );
if( listOfDepartments == null || listOfDepartments.isEmpty() )
output += "There are no departments in the company.";
else
{
for( Department deptElement : listOfDepartments)
output += " " + deptElement;
}
return output;
}
}
Explanation / Answer
Please find the below code with required modifications :
import java.util.ArrayList;
public class Company {
// instance variables
private String companyName;
private ArrayList<Department> listOfDepartments = new ArrayList<>();
/**
* This constructor sets the company's name and list of departments.
* @param name Company's name.
* @param departments List of departments in the company.
*/
public Company(String name, ArrayList<Department> departments)
{
companyName = name;
ArrayList<Department> newList = new ArrayList<Department>(departments);
listOfDepartments = newList;
}
/**
* The getCompanyName method returns the company's name.
* @return The company's name.
*/
public String getCompanyName()
{
return companyName;
}
/**
* The getDepartmentList method returns the company's list of departments.
* @return The company's list of departments as an ArrayList of Department elements.
*/
public ArrayList<Department> getListOfDepartments()
{
ArrayList<Department> newList = new ArrayList<Department>(listOfDepartments);
return newList;
}
/**
* The setCompanyName method sets the name for this company.
* @param name The value to store in the name field for this company.
*/
public void setCompanyName(String name)
{
companyName = name;
}
/**
* The setDepartmentList method sets the list of departments for this company.
* @param departments The value, as an ArrayList of Department elements, to store
* in the list of departments field for this company.
*/
public void setListOfDepartments(ArrayList<Department> departments)
{
ArrayList<Department> newList = new ArrayList<Department>(departments);
listOfDepartments = newList;
}
/**
* The toString method returns a string containing the company's data.
* @return A String containing the Company's information: name and list of
* departments.
*/
@Override
public String toString()
{
String output = String.format("%-30s %s", "Company Name:", companyName );
if( listOfDepartments == null || listOfDepartments.isEmpty() )
output += "There are no departments in the company.";
else
{
for( Department deptElement : listOfDepartments)
output += " " + deptElement;
}
return output;
}
/**
*
*/
public void addDepartment(Department dept) {
Department copy = dept.clone();
listOfDepartments.add(copy);
}
/**
*
*/
public void addEmployeeToDepartment(int deptId, Employee empObject) {
for(Department dept : listOfDepartments) {
if(dept.getId()==deptId) {
Employee copy = empObject.clone();
dept.addEmployee(copy);
}
}
}
/**
*
*/
public void setDepartmentmanager(int deptId, Manager manObject) {
for(Department dept : listOfDepartments) {
if(dept.getId()==deptId) {
Manager copy = manObject.clone();
dept.addManager(copy);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.