Question: This assignment aims to practice implementing and using the Composite Design Pattern. You will extend the Employee hierarchy by adding a new leaf classes, implementing

This assignment aims to practice implementing and using the Composite Design Pattern. You
will extend the Employee hierarchy by adding a new leaf classes, implementing new
functionality, and creating a test program to exercise your code.
Reference for Lab rarr
Part 1: Adding a new Employee type
1.1. Define a new leaf class, Intern, that implements the Employee interface.
1.2..1. Define a new leaf class,
, that implements the
interface. Attributes
are empld, Department, and Name
1.3. The Intern 's position should always be "Intern." This should be set inside the Intern
class and not be modifiable from outside. The Attributes of the class are Name, position, and
universityAttended.
1.4. Implement the showEmployeeDetails() method to print the details of the Intern.
1.5 Implement the showEmployeeDetails() method to print the details of the Manager .
Part 2: Adding new functionality
2.1. Add a countEmployees() method to the CompanyDirectory class. This method should
return the total number of employees in the directory.
Part 3: Testing your code
3.1. Create a CompanyDirectory for the software department and add two Developer
instances, one Manager instance, and one instance to it.
3.2. Call showEmployeeDetails() on the software department directory to print the details of
all the employees.
3.3. Call
on the software department directory and print the result.
Submission Guidelines
Please submit the .java files that include your code (.zip file if your code.) and a screenshot of
the code running. Please ensure that your code compiles and runs without errors before
submitting.
Composite design pattern:
Code:
package CompositePatternDemo;
//Component
interface Employee {
void showEmployeeDetails();
}
The Employee interface defines a common operation, showEmployeeDetails(), which is to
be implemented by all objects in the composition. All employees, whether they are a simple
developer or a composite directory, will have this operation.
package CompositePatternDemo;
import java.util.ArrayList;
import java.util.List;
//Composite
class CompanyDirectory implements Employee {
private List employeeList = new ArrayList();
@Override
public void showEmployeeDetails(){
for (Employee emp : employeeList){
emp.showEmployeeDetails();
}
}
public void addEmployee(Employee emp){
employeeList.add(emp);
}
public void removeEmployee(Employee emp){
employeeList.remove(emp);
}
}
package CompositePatternDemo;
//Leaf
class Developer implements Employee {
private String name;
private long empId;
private String position;
public Developer(long empId, String name, String position){
this.empId = empId;
this.name = name;
this.position = position;
}
@Override
public void showEmployeeDetails(){
System.out.println("Developer ID: "+ empId +" Name: "+ name +"
}
}
Driver:
package CompositePatternDemo;
public class CompositeDriver {
public static void main(String[] args)
{
Employee dev1= new Developer(100, "Fred Flintstone", "Pro Develop
Employee dev2= new Developer(101, "Bart Simpson", "Developer");
CompanyDirectory engDirectory = new CompanyDirectory();
engDirectory.addEmployee(dev1);
engDirectory.addEmployee(dev2);
engDirectory.showEmployeeDetails();
}
}
16
This assignment aims to practice implementing and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!