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 : Adding a new Employee type
Define a new leaf class, Intern, that implements the Employee interface.
Define a new leaf class,
that implements the
interface. Attributes
are empld, Department, and Name
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.
Implement the showEmployeeDetails method to print the details of the Intern.
Implement the showEmployeeDetails method to print the details of the Manager
Part : Adding new functionality
Add a countEmployees method to the CompanyDirectory class. This method should
return the total number of employees in the directory.
Part : Testing your code
Create a CompanyDirectory for the software department and add two Developer
instances, one Manager instance, and one instance to it
Call showEmployeeDetails on the software department directory to print the details of
all the employees.
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 addEmployeeEmployee emp
employeeList.addemp;
public void removeEmployeeEmployee emp
employeeList.removeemp;
package CompositePatternDemo;
Leaf
class Developer implements Employee
private String name;
private long empId;
private String position;
public Developerlong empId, String name, String position
this.empId empId;
this.name name;
this.position position;
@Override
public void showEmployeeDetails
System.out.printlnDeveloper ID: empId Name: name
Driver:
package CompositePatternDemo;
public class CompositeDriver
public static void mainString args
Employee dev new Developer "Fred Flintstone", "Pro Develop
Employee dev new Developer "Bart Simpson", "Developer";
CompanyDirectory engDirectory new CompanyDirectory;
engDirectory.addEmployeedev;
engDirectory.addEmployeedev;
engDirectory.showEmployeeDetails;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
