In the Company class, provide the implementation for the overloaded method below
ID: 3711261 • Letter: I
Question
In the Company class, provide the implementation for the overloaded method below:
public void addEmployeeToDepartment(String deptName, Employee empObject)
{
// provide implementation
}
Also, in the DepartmentLinkedList class, provide the implementation for the overloaded method below:
public Department find(String deptName)
{
// provide implementation
}
Note: Since this method should be case insensitive, I suggest you use the equalsIgnoreCase method of the String class.
Right-click on the Test_Extra_Credit class and choose “Run File”.
The output generated should be as shown below.
Submit a printed copy or email only the code for the two new methods: addEmployeeToDepartment and find.
_________________________________________________________
Test Extra_Credit. java
public class Test_Extra_Credit
{
public static void main(String args[])
{
Manager manObj1 = new Manager(1,"Bob", "Brown", null, 55000, 250);
Manager manObj2 = new Manager(2,"Mary", "Martinez", null, 55000, 250);
Manager manObj3 = new Manager(3,"Paul", "Porter", null, 55000, 250);
Manager manObj4 = new Manager(4,"John", "Doe", null, 55000, 250);
Manager manObj5 = new Manager(5,"Lucy", "Lopez", null, 55000, 250);
Manager manObj6 = new Manager(6,"Barbara", "Blanco", null, 55000, 250);
Department dept1 = new Department(1, "Human Resources", manObj1, null);
Department dept2 = new Department(2, "Information Technology", manObj2, null);
Department dept3 = new Department(3, "Quality Assurance", manObj3, null);
Department dept4 = new Department(4, "Public Relations", manObj4, null);
Department dept5 = new Department(5, "Communications", manObj5, null);
Department dept6 = new Department(6, "Marketing", manObj6, null);
DepartmentLinkedList deptList = new DepartmentLinkedList();
deptList.add(dept1);
deptList.add(0, dept2);
deptList.add(dept3);
deptList.add(1, dept4);
deptList.add(3, dept5);
deptList.add(dept6);
Company companyObj = new Company("Test Company", deptList);
HourlyEmployee emp1 = new HourlyEmployee(7, "Albert", "Hernandez", null, 14.75, 30);
HourlyEmployee emp2 = new HourlyEmployee(8, "Nancy", "Noor", null, 20, 40);
SalariedEmployee emp3 = new SalariedEmployee(3, "John", "Smith", null, 560.00);
companyObj.addEmployeeToDepartment("public Relations", emp1);
companyObj.addEmployeeToDepartment("QUALITY ASSURANCE", emp2);
companyObj.addEmployeeToDepartment("Communications", emp3);
Department foundDept4 = deptList.find("public Relations");
Department foundDept3 = deptList.find("QUALITY ASSURANCE");
Department foundDept1 = deptList.find("human resources");
Department foundDept5 = deptList.find("Communications");
System.out.println(foundDept4);
System.out.println(foundDept3);
System.out.println(foundDept1);
System.out.println(foundDept5);
}
}
Company.java
public class Company
{
// instance variables
private String companyName;
private DepartmentLinkedList listOfDepartments = new DepartmentLinkedList();
/**
* 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, DepartmentLinkedList departments)
{
companyName = name;
// before trying to copy the departments linked list, make sure it isn't null
if(departments != null)
listOfDepartments = departments.copy();
}
/**
* The getCompanyName method returns the company's name.
* @return The company's name.
*/
public String getCompanyName()
{
return companyName;
}
/**
* The getListOfDepartments method returns the company's list of departments.
* @return The company's list of departments as a linked list of Department elements.
*/
public DepartmentLinkedList getListOfDepartments()
{
if(listOfDepartments != null)
return listOfDepartments.copy();
else
return null;
}
/**
* 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 setListOfDepartments method stores a value in the listOfDepartments field.
* @param departments the list of departments to store in the listOfDepartments field.
*/
public void setListOfDepartments(DepartmentLinkedList departments)
{
if(departments != null)
listOfDepartments = departments.copy();
}
/**
* 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
output += " " + listOfDepartments.toString();
return output;
}
/**
* The addDepartment method adds a department to the list of departments in the company.
* @param dept The Department object to be added to the list.
*/
public void addDepartment(Department dept)
{
listOfDepartments.add(new Department(dept));
}
/**
* The addEmployeeToDepartment method iterates through the listOfDepartments Linked List 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)
{
Department foundDept = listOfDepartments.find(deptID);
if( foundDept != null )
{
if( empObject instanceof HourlyEmployee )
foundDept.addEmployee( new HourlyEmployee((HourlyEmployee)empObject));
else if( empObject instanceof SalariedEmployee )
foundDept.addEmployee( new SalariedEmployee((SalariedEmployee)empObject));
}
}
/**
* The addEmployeeToDepartment method iterates through the listOfDepartments Linked List looking
* for a Department whose departmentName is equal to the String value passed as the first argument.
* If it finds it, it adds a copy of the Employee object passed as the second argument.
* @param deptName the departmentName to search for in the list.
* @param empObject the Employee object to be added to the department.
*/
public void addEmployeeToDepartment (String deptName, Employee empObject)
{
// provide implementation
}
/**
* The setDepartmentManager 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 sets its manager to be the value passed as the second argument.
* @param deptID the departmentID to search for in the list.
* @param manObject the Manager object used to set the department's manager.
*/
public void setDepartmentManager(int deptID, Manager manObject)
{
Department foundDept = listOfDepartments.find(deptID);
if( foundDept != null )
foundDept.setDepartmentManager(new Manager(manObject));
}
}
DepartmentLinkedList.java
class DepartmentLinkedList
{
/**
* The Node class stores a list element and a reference to the next node.
*/
private class Node
{
Department value;
Node next;
/**
* Constructor.
* @param val The element to store in the node.
* @param n The reference to the successor node.
*/
Node(Department val, Node n)
{
value = val;
next = n;
}
/**
* Constructor.
* @param val The element to store in the node.
*/
Node(Department val)
{
// Call the other (sister) constructor.
this(val, null);
}
}
private Node first; // list head
private Node last; // last element in list
/**
* Constructor.
*/
public DepartmentLinkedList()
{
first = null;
last = null;
}
/**
* The isEmpty method checks to see if the list is empty.
* @return true if list is empty, false otherwise.
*/
public boolean isEmpty()
{
return first == null;
}
/**
* The size method returns the length of the list.
* @return The number of elements in the list.
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count++;
p = p.next;
}
return count;
}
/**
* The add method adds an element to the end of the list.
* @param e The value to add to the end of the list.
*/
public void add(Department d)
{
if (isEmpty())
{
first = new Node(d);
last = first;
}
else
{
// Add to the end of existing list
last.next = new Node(d);
last = last.next;
}
}
/**
* The add method adds an element at a position.
* @param e The element to add to the list.
* @param index The position at which to add the element.
* @exception IndexOutOfBoundsException When index is out of bounds.
*/
public void add(int index, Department d)
{
if (index < 0 || index > size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
// Index is at least 0
if (index == 0)
{
// New element goes at beginning
first = new Node(d, first);
if (last == null)
{
last = first;
}
return;
}
// Set a reference pred to point to the node that will be the
// predecessor of the new node
Node pred = first;
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next;
}
// Splice in a node containing the new element
pred.next = new Node(d, pred.next);
// Is there a new last element ?
if (pred.next.next == null)
{
last = pred.next;
}
}
/**
* The toString method computes the string representation of the list.
* @return The string form of the list.
*/
@Override
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + " ");
p = p.next;
}
return strBuilder.toString();
}
/**
* The remove method removes the element at an index.
* @param index The index of the element to remove.
* @return A copy of the element removed.
* @exception IndexOutOfBoundsException When index is out of bounds.
*/
public Department remove(int index)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
Department element; // The element to return
if (index == 0)
{
// Removal of first item in the list
element = first.value;
first = first.next;
if (first == null)
{
last = null;
}
}
else
{
// To remove an element other than the first, find the predecessor
// of the element to be removed.
Node pred = first;
// Move pred forward index - 1 times
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next;
}
// Store the value to return
element = pred.next.value;
// Route link around the node to be removed
pred.next = pred.next.next;
// Check if pred is now last
if (pred.next == null)
{
last = pred;
}
}
return element;
}
/**
* The remove method removes an element.
* @param element The element to remove.
* @return true if the remove succeeded, false otherwise.
*/
public boolean remove(Department element)
{
if (isEmpty())
{
return false;
}
if (element.equals(first.value))
{
// Removal of first item in the list
first = first.next;
if (first == null)
{
last = null;
}
return true;
}
// Find the predecessor of the element to remove
Node pred = first;
while (pred.next != null && !pred.next.value.equals(element))
{
pred = pred.next;
}
// pred.next == null OR pred.next.value is element
if (pred.next == null)
{
return false;
}
// pred.next.value is element
pred.next = pred.next.next;
// Check if pred is now last
if (pred.next == null)
{
last = pred;
}
return true;
}
/**
* The copy method returns a copy of the linked list.
* @return A copy of the list.
*/
public DepartmentLinkedList copy()
{
DepartmentLinkedList newList = new DepartmentLinkedList();
Node element = first;
while( element != null )
{
if( element.value != null )
newList.add(element.value);
else
newList.add(null);
element = element.next;
}
return newList;
}
/**
* The find method finds the Department element in the list that has the
* id specified in the parameter. If none is found, the method returns null.
* @param deptID ID of the department to be found.
* @return The Department object with the given id. Null if not found.
*/
public Department find(int deptID)
{
Node element = first;
while( element != null )
{
if( element.value.getDepartmentID() == deptID )
return element.value;
element = element.next;
}
return null;
}
/**
* The find method finds the Department element in the list that has the
* name specified in the parameter. If none is found, the method returns null.
* @param deptName Name of the department to be found.
* @return The Department object with the given name. Null if not found.
*/
public Department find(String deptName)
{
// complete the implementation
}