|
| 1 | +package composite; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +// Implementing the Tree Like Structure for the Employee |
| 7 | +// Creating the abstract class such that any leaf or composite can make use of it |
| 8 | +abstract class Employee { |
| 9 | + private String name; |
| 10 | + private int Salary; |
| 11 | + private String role; |
| 12 | + |
| 13 | + public Employee(String name, int Salary, String role) { |
| 14 | + this.name = name; |
| 15 | + this.Salary = Salary; |
| 16 | + this.role = role; |
| 17 | + } |
| 18 | + |
| 19 | + public String getRole() { |
| 20 | + return role; |
| 21 | + } |
| 22 | + |
| 23 | + public String getName() { |
| 24 | + return name; |
| 25 | + } |
| 26 | + |
| 27 | + public int getSalary() { |
| 28 | + return Salary; |
| 29 | + } |
| 30 | + |
| 31 | + // Making the methods as abstract as the implementation can be provided by the inherited class |
| 32 | + public void addEmployee(Employee employee) { |
| 33 | + throw new UnsupportedOperationException("Inherited Class must override"); |
| 34 | + } |
| 35 | + |
| 36 | + public void removeEmployee(Employee employee) { |
| 37 | + throw new UnsupportedOperationException("Inherited Class must override"); |
| 38 | + } |
| 39 | + |
| 40 | + public abstract void displayDetails(); |
| 41 | +} |
| 42 | + |
| 43 | +// Creating the Developer Employee using the common Employee abstract class as an Leaf |
| 44 | +class Developer extends Employee { |
| 45 | + private String name; |
| 46 | + private int salary; |
| 47 | + private String role; |
| 48 | + |
| 49 | + public Developer(String name, int salary, String role) { |
| 50 | + super(name, salary, role); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void displayDetails() { |
| 55 | + System.out.println("\tRole: " + getRole() + "\tName: " + getName() + "\tSalary: " + getSalary()); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Creating the Composite Class |
| 60 | +class Manager extends Employee { |
| 61 | + private String name; |
| 62 | + private int salary; |
| 63 | + private String role; |
| 64 | + private List<Employee> employees = new ArrayList<>(); |
| 65 | + |
| 66 | + public Manager(String name, int salary, String role) { |
| 67 | + super(name, salary, role); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void addEmployee(Employee employee) { |
| 72 | + this.employees.add(employee); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void removeEmployee(Employee employee) { |
| 77 | + this.employees.remove(employee); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void displayDetails() { |
| 82 | + System.out.println("Role: " + getRole() + "\tName: " + getName() + "\tSalary: " + getSalary()); |
| 83 | + System.out.println("-----------------------------------"); |
| 84 | + for (Employee employee : this.employees) { |
| 85 | + employee.displayDetails(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +public class CompositePattern { |
| 91 | + public static void main(String[] args) { |
| 92 | + // Creating the Tree Like Structure for the composite |
| 93 | + Employee developer1 = new Developer("John Dantle", 15000, "Frontend Developer"); |
| 94 | + Employee developer2 = new Developer("Carry Yadav", 20000, "Backend Developer"); |
| 95 | + Employee developer3 = new Developer("Sunny wiley", 12000, "SQL"); |
| 96 | + Employee developer4 = new Developer("Alice", 30000, "Full Stack Developer"); |
| 97 | + |
| 98 | + // Creating the CEO or root |
| 99 | + Employee ceo = new Manager("Danny", 1300000, "CEO"); |
| 100 | + |
| 101 | + // Creating the two manager |
| 102 | + Employee manager1 = new Manager("Bob Rao", 1000000, "Manager"); |
| 103 | + Employee manager2 = new Manager("Frank", 120000, "Manager"); |
| 104 | + |
| 105 | + // Creating the Team Lead |
| 106 | + Employee teamlead1 = new Manager("Franklin", 90000, "Team Lead"); |
| 107 | + Employee teamlead2 = new Manager("Rosario", 100000, "Senior Team Lead"); |
| 108 | + |
| 109 | + // Adding the tree or connection between them |
| 110 | + ceo.addEmployee(manager1); |
| 111 | + ceo.addEmployee(manager2); |
| 112 | + |
| 113 | + manager1.addEmployee(teamlead1); |
| 114 | + manager2.addEmployee(teamlead2); |
| 115 | + manager2.addEmployee(teamlead2); |
| 116 | + |
| 117 | + teamlead1.addEmployee(developer1); |
| 118 | + teamlead1.addEmployee(developer2); |
| 119 | + teamlead2.addEmployee(developer3); |
| 120 | + teamlead2.addEmployee(developer4); |
| 121 | + teamlead2.addEmployee(developer2); |
| 122 | + |
| 123 | + System.out.println("--------------- Company Organizational Chart ---------------------\n"); |
| 124 | + // printing the tree like structure |
| 125 | + ceo.displayDetails(); |
| 126 | + |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// --------------- Company Organizational Chart --------------------- |
| 131 | +/// Role: CEO Name: Danny Salary: 1300000 |
| 132 | +/// ----------------------------------- |
| 133 | +/// Role: Manager Name: Bob Rao Salary: 1000000 |
| 134 | +/// ----------------------------------- |
| 135 | +/// Role: Team Lead Name: Franklin Salary: 90000 |
| 136 | +/// ----------------------------------- |
| 137 | +/// Role: Frontend Developer Name: John Dantle Salary: 15000 |
| 138 | +/// Role: Backend Developer Name: Carry Yadav Salary: 20000 |
| 139 | +/// Role: Manager Name: Frank Salary: 120000 |
| 140 | +/// ----------------------------------- |
| 141 | +/// Role: Senior Team Lead Name: Rosario Salary: 100000 |
| 142 | +/// ----------------------------------- |
| 143 | +/// Role: SQL Name: Sunny wiley Salary: 12000 |
| 144 | +/// Role: Full Stack Developer Name: Alice Salary: 30000 |
| 145 | +/// Role: Backend Developer Name: Carry Yadav Salary: 20000 |
| 146 | +/// Role: Senior Team Lead Name: Rosario Salary: 100000 |
| 147 | +/// ----------------------------------- |
| 148 | +/// Role: SQL Name: Sunny wiley Salary: 12000 |
| 149 | +/// Role: Full Stack Developer Name: Alice Salary: 30000 |
| 150 | +/// Role: Backend Developer Name: Carry Yadav Salary: 20000 |
0 commit comments