0

I'm kind of stuck on a programming conceptual problem in a Java small task. I hava an Employee class, which has, among others, two parameters: ID and the ID of his manager. I also have class Staff which is a container for my employees. Now I would like to list all of them in a tree-like hierarchy, like this:

Dominik Sankowski
|-Radosław Adamus
| |- Łukasz Sturgulewski
| |- Andrzej Albrecht
|-Lidia Jackowska
| |- Michał Paluch
|-Szymon Grabowski
| |- Wojciech Bieniecki
| |- Sebastian Stoliński
| |- Tomasz Węgliński
|-Jacek Kucharski

depending on who is the boss of whom.

I have tried with TreeMaps, and with recurrence, but my head is literally exploding, and I just can't get it right, my mind is blocked. Any tips at all?

EDIT1: My code so far:

public class Staff {
    //Integer - manager id, Employee - object
    private ArrayList <Employee> employees;

    public Staff(){

        employees = new ArrayList<Employee>();
        employees.add(new Employee(1, "Wojciech","Bieniecki", 15));
        employees.add(new Employee(2, "Dariusz","Brzeziński", 18));
        employees.add(new Employee(3, "Michał","Paluch", 19));
        employees.add(new Employee(4, "Sebastian","Stoliński",  1));
        employees.add(new Employee(5, "Tomasz","Węgliński", 1));
        employees.add(new Employee(6, "Radosław","Adamus", 16));
        employees.add(new Employee(7, "Andrzej","Albrecht",  11));
        employees.add(new Employee(8, "Robert","Banasiak",  13));
        employees.add(new Employee(9, "Tomasz","Koszmider",  10));
        employees.add(new Employee(10, "Krzysztof","Strzecha", 16));
        employees.add(new Employee(11, "Łukasz","Sturgulewski",  6));
        employees.add(new Employee(12, "Piotr","Urbanek", 17));
        employees.add(new Employee(13, "Radosław","Wajman",  16));
        employees.add(new Employee(14, "Jacek","Wiślicki",  16));
        employees.add(new Employee(15, "Szymon","Grabowski",  16));
        employees.add(new Employee(16, "Dominik","Sankowski",  0));
        employees.add(new Employee(17, "Jacek","Kucharski",  16));
        employees.add(new Employee(18, "Piotr","Ostalczyk",  16));
        employees.add(new Employee(19, "Lidia","Jackowska",  16));
    }

    public Employee convertIdToEmployee (int id)
    {
        for (int i=0; i<employees.size(); i++)
        {
            if(id == employees.get(i).getId())
            {
                return employees.get(i);
            }
        }
        return null;
    }


    public Employee find(String familyName)
    {
        for (int i=0; i<employees.size(); i++)
        {
            if(familyName == employees.get(i).getFamilyName())
            {
                return employees.get(i);
            }
        }
        return null;

    }

    public ArrayList<Employee> showSubordinates(Employee emp)
    {
        ArrayList <Employee> result = new ArrayList <Employee>();
        for (int i=0; i<employees.size(); i++)
        {
            if(emp.getId() == employees.get(i).getManagerId())
            {
                result.add(employees.get(i));
            }
        }
        return result.size() != 0 ? result : null;
    }

    public ArrayList<Employee> buildTree(ArrayList <Employee> subordinates){

        ArrayList <Employee> result = new ArrayList <Employee>();
        for(int i=0; i<subordinates.size();i++)
        {
            buildTree(showSubordinates(subordinates.get(i)));
            result.add(subordinates.get(i));

        }
        return result;
    }

    public ArrayList<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(ArrayList<Employee> employees) {
        this.employees = employees;
    }

    @Override
    public String toString()
    {
        return "";
    }
}

And the Employee class is just a data holder with getters and setters.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55

1 Answers1

-1

I would suggest creating a employee class as the top level of hierarchy. Managers can inherit from this Employee class. You can have a list of employees in Manager class who work under him/her.

For printing the hierarchy. You can call print on the topmost level of manager and recursively printing down all employee who refer him and call print in turn on them.

Hope this helps. Recursion could be the key here to print the entire hierarchy.

Prasad Shinde
  • 652
  • 2
  • 8
  • 21