How could I check if any objects in my list have children of it's own?
I might have blown this code entirely up and I missed the purpose.
here is what I tried
import java.util.Arrays;
import java.util.List;
public class Employee {
public String name;
public double pay;
public Employee(String name, double pay) {
this.name = name;
this.pay = pay;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
public void addEmployee(Employee emp) {
throw new UnsupportedOperationException();
}
public void removeEmployee(Employee emp) {
throw new UnsupportedOperationException();
}
public double calculateTotalPay() {
throw new UnsupportedOperationException();
}
public void printList(){
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", pay=" + pay +
'}';
}
}
package composite;
public class DatabaseEngineer extends Employee {
private int numberOfDB;
public DatabaseEngineer(String name, double pay, int numberOfDB) {
super(name, pay);
this.numberOfDB = numberOfDB;
}
public int getNumberOfDB() {
return numberOfDB;
}
@Override
public void addEmployee(Employee emp) {
throw new UnsupportedOperationException();
}
@Override
public double calculateTotalPay() {
return this.getPay() + (1000 * this.getNumberOfDB());
}
}
package composite;
public class SoftwareEngineer extends Employee {
private String title;
public SoftwareEngineer(String name, double pay, String title) {
super(name, pay);
this.title = title;
}
@Override
public double calculateTotalPay() {
if (this.title.equals("SWE2")) {
return this.getPay() + 5000.00 * 1;
} else if (this.title.equals("Senior SWE")) {
return this.getPay() + 20000.00 * 1.5;
} else if (this.title.equals("Principle Engineer")) {
return this.getPay() + 50000.00 * 2;
} else
return 0.0;
}
}
package composite;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Manager extends Employee {
List<Employee> listOfEmployee = new ArrayList<Employee>();
public Manager(String name, double pay) {
super(name, pay);
}
@Override
public void addEmployee(Employee employee) {
listOfEmployee.add(employee);
}
@Override
public void removeEmployee(Employee employee) {
listOfEmployee.remove(employee);
}
@Override
public double calculateTotalPay() {
double pay = 0.0;
for (Employee employee1 : listOfEmployee) {
if (employee1 instanceof SoftwareEngineer) {
pay += employee1.calculateTotalPay();
}
if (employee1 instanceof DatabaseEngineer) {
pay += employee1.calculateTotalPay();
}
if (employee1 instanceof Manager) {
pay += employee1.getPay();
}
}
return pay;
}
@Override
public void printList() {
listOfEmployee.forEach(System.out::println);
}
}
package composite;
public class Main {
public static void main(String[] args) {
/**
* Test managers that manage some employees.
*/
Employee softwareEngEmployee = new SoftwareEngineer("joe", 200.00, "SWE2");
Employee databaseGuy = new DatabaseEngineer("bob", 200.00, 2);
Employee manager = new Manager("tim", 200.00);
manager.addEmployee(softwareEngEmployee);
manager.addEmployee(databaseGuy);
manager.addEmployee(manager);
System.out.println(manager.calculateTotalPay());
/**
* Test managers that manage managers with some employees.
*/
Employee softwareEngEmployee1 = new SoftwareEngineer("joe", 200.00, "SWE2");
Employee databaseGuy1 = new DatabaseEngineer("bob", 200.00, 2);
Employee manager1 = new Manager("tim", 200.00);
Employee manager2 = new Manager("cali", 200.00);
Employee manager3 = new Manager("swan", 200.00);
manager1.addEmployee(softwareEngEmployee1);
manager1.addEmployee(databaseGuy1);
manager1.addEmployee(manager1);
manager1.addEmployee(manager2);
manager1.addEmployee(manager3);
System.out.println(manager1.calculateTotalPay());
manager1.removeEmployee(manager1);
System.out.println(manager1.calculateTotalPay());
/**
* printing out employees
*/
manager1.printList();
}
}
My iteration does not discover the child of managers.
Example:
manager2.addEmployee(databaseGuy1);
As databaseGuy1
goes inside the manager2, the calculateTotalPay, from the manager class will not find him.
I tried google for getChild()
and I've also tried playing with indexes, without success.
Aucun commentaire:
Enregistrer un commentaire