• Post Reply Bookmark Topic Watch Topic
  • New Topic

print the reportees using one id  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on a project which is based on composite pattern where we have a tree hierarchy that builds the ceo and under it managers and under it its reportees. The program has been formulated upto 80%.Now i just want to print the reportees like in a tree like  structure.The structure begins from the ceo then we iterate the reportees that automatically gets printed by the virtue of an iterator. However how do i start to print from the ceo, this is where i am facing the trouble. I have the employeeid of the ceo and based on which i have to print the reportees. Please help.

Employee class


Main Class
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you start by drawing a diagram showing what you want. You will have to work out how far to the right to put the first line depending on the length of subsequent lines. You may find that easier to calculate if you work backwards from the last line.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made an attempt by assigning the ceo the map. But the reportees are not getting printed when i call showdetails.



it prints only

Details 7839
----------------
Name = 'KING'    Designation = 'PRESIDENT'
----------------

and not the entire hierarchy.


the entire output :

key = 7566  7788='SCOTT', 'ANALYST',7566,'09-DEC-82',3000,NULL,20

key = 7566  7902='FORD', 'ANALYST',7566,'3-DEC-81',3000,NULL,20

key = 7902  7369='SMITH', 'CLERK',7902,'17-DEC-80',800,NULL,20

key = 7839  7782='CLARK', 'MANAGER',7839,'9-JUN-81',2450,NULL,10

key = 7839  7566='JONES', 'MANAGER',7839,'2-APR-81',2975,NULL,20

empId : 7782
'CLARK'
Employee values = []

empId : 7369
'SMITH'
Employee values = []

empId : 7566
'JONES'
Employee values = ['SCOTT' ,'ANALYST', 7566, '09-DEC-82',3000.0,NULL,20, 'FORD' ,'ANALYST', 7566, '3-DEC-81',3000.0,NULL,20]

empId : 7788
'SCOTT'
Employee values = []

empId : 7902
'FORD'
Employee values = ['SMITH' ,'CLERK', 7902, '17-DEC-80',800.0,NULL,20]

empId : 7839
'KING'
Employee values = ['CLARK' ,'MANAGER', 7839, '9-JUN-81',2450.0,NULL,10, 'JONES' ,'MANAGER', 7839, '2-APR-81',2975.0,NULL,20]

Details7839
----------------
Name = 'KING'    Designation = 'PRESIDENT'
----------------





 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:.... Now i just want to print the reportees like in a tree like  structure.

As soon as someone mentions printing a tree like structure they are talking about recursion.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you also post your EmployeeDTO. It is needed for compilation.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some observations about your code...
These are constants and the java convention is that their names be in all-caps with underscores to separate pieces of names instead of using camel-case. E.g. "EMP_ID".
Would be cleaner as...
See how it improves the readability, not to mention the amount of typing required. You have several places in your code that could benefit from this.

You appear to have a Map<String,Employee> with the key being empId. This is good, on the other hand, you've then removed empId from your Employee class. This is not so good. It's desirable to keep you Employee class whole so that it could be used in other cases that don't involve Maps. It is often the case where a Map's key is also embedded in the value object.

This would have simplified this
to

I'm not sure why EmployeeDTO and Employee couldn't be one and the same. Perhaps seeing your DTO class might make it obvious. Has this been suggested in your class?




 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey


here is my employeeDTO class

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Carey


I implemented what you said  for obtaining tree-like structure:

In Main() class:




In Employee class



however the output i obtain is :



But i want the structure like  first CEO then under ceo -->Manager under managers --> analyst and clerks. how to obtain tht
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wrote that as a static method. Here they are as non-static.

How do you determine which Employee is the CEO?
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In main()

I added reportees

Your list of reportees is not being completely filled in.

My output is
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are constructing a new Employee from an EmployeeDTO object in two places. Once when you read the file, and again when you compute reportees. Note that, although the two created Employee objects have the same data, they are not the same Employee object. Therefore you are trying to print the Employee that is not the one you calculated reportees for.

I'm still not convinced that you need a DTO class at all.

I am convinced that your Employee class should contain an empId field.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code you should be adding type parameters to Iterator. This also eliminates the need for a cast.
Additionally, iterators are not used (directly) so much any more since the introduction of the enhanced for() loop; which happens to use iterators under the covers, but you don't see them.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got way too much code in main(). It needs to be refactored into smaller methods. This task is somewhat problematic because main() is a static method and therefore doesn't have any member variables (a.k.a fields) associated with it which then forces you to declare all methods static and pass around gobs of data, rather than sharing the data. So, the first pass at modularizing is to have a main() that does nothing but instantiate an EmployeeHierarchyMain object and then use that to run the remainder of your logic.
This frees you from the bonds of having to declare every thing static. Now you can do things like make your employeeModel variable a class level variable. Now you don't have to pass it around and as you continue to refactor your code, all new methods will have access to employeeModel.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey

I have made the changes that you have told. However , i am still not able to link a certain part.
i.e. I add the reportees when the employee id is equal to manager id. However , the obstacle that i am facing here is  i am getting reportees for Jones individually as 2 , as Jones is a manager for  Scott and Ford , Jones has King as his manager. Here when i go through Jones  i get two of his reportees , but when i see it via Kings reportees of Jones , i get 0.
I guess somewhere the Kings reportees of Jones and alone Jones reportees are not linked and hence i am not getting a tree like structure. How to link that.





Output :



 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still looking. In the mean time, the way you call a recursive method is by starting at the root of the tree, which in your case is the CEO. Then you don't need to enclose it inside a loop because that is what the recursion does for you.

Just with that change my output is:

As you can see, there is still a problem with reportees not being filled in.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to close your BufferedReader when you're done with it.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not need nested loops, as in

You can loop through your employeeModel and then use the managerId as a key to look up the Employee manager object.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you do the steps I mentioned the tree prints correctly.

I did some playing around with the print formatting just for fun and this is the output I'm now getting.

To the the vertical bar in the indent I just appended "|   " to the indent.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Carey

1]  I closed the file still no proper output.The problem of Jones not showing its reportees still exist.


2] I implemented the second for loop this way :






My Entire code :

What more changes do i need to make to get the output you are getting.




Output :

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ carey. can you paste your code
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey

I even changed the code like this you mentioned for the nested loop.



ouput i am getting is -



I am not getting a tree below the King
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey . here in the above loop the manager id it searches for the same employees managerid. Hence no output
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Carey . thanks. i got it.

for (Entry<String, Employee1> employeeEntry : keyys) {

for (String empId : employeeModel.keySet()) {

if (empId.equals(employeeEntry.getValue().getManagerid())) {

System.out.println("key = " + empId + " " + employeeEntry.toString());

employeeModel.get(employeeEntry.getValue().getManagerid()).addReportees(employeeEntry.getValue());

}
}

I now have been given to apply design patterns
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whooo, hooo !!!
Congrats.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!