• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help to import information from other class' elements  RSS feed

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I am stuck for a while and I could use some help from you.

I need one class' (Employee) objects to use information from another class' (Department) objects. Each department has a certain average handlingSpeed (amount of items an employee is able to process per hour).
Each employee works in a department. I want the program to know that when an employee works for a certain department, he has a certain average handlingSpeed (Assuming that the average handlingSpeed for each employee is equal to the average handlingSpeed of the whole department. For this assignment, this is a valid assumption )

I would like my program to know that when one Employee works for Department A, he will have an average handling speed of X.

The Employee information is imported with a txt-file. Each line represents one Employee.

The Department-information is currently created by hand, and invoked by a method called initiateDepartments(), see below. I do not believe that this makes sense, but I could not come up with any better ideas. Do you have suggestions how to do this?


P.s. There is way more information to come for each department, that I need to know for Employees. For this question, I trimmed the amount of code.

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using tables or some sort of DB? It seems to me you need an EmployeeDepartment table with each row having the information for an EmployeeDepartment object. The ID of this table could be the Employee ID plus the Department ID.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My design is very basic. I do not use tables or DB at the moment.
I just have a List of Drivers and a (currently) non-listed number of Departments.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Hoovenaar wrote:Each employee works in a department. I want the program to know that when an employee works for a certain department, he has a certain average handlingSpeed (Assuming that the average handlingSpeed for each employee is equal to the average handlingSpeed of the whole department. For this assignment, this is a valid assumption )

OK, so it sounds like the Employee's "assumed speed" has nothing to do with him/her, but is solely dependant on the Department s/he works in.

You might also want to keep track of how fast they actually work; and that information would be related to an Employee; but that's not what you described.

However, very basically, you could do something like:and then anywhere you need to you can get an Employee's "assumed" speed with:
  someone.getDepartment().getAverageSpeed()

HIH

Winston
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you're going to have for that one datum that involves an Employee and a Department, I would create that EmployeeDepartment class and keep a list of them somewhere, either in your program's memory, or if you must, in the Employee or Department class. This last suggestion is not the best design, but it will work for a small project that is going to stay small.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading Winston's post I see that it looks like only the Department needs an average speed. What he suggests is good.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me some time to figure out, since I just started practicing with encapsulation, but it works like a charm now! I am incredibly thankful guys
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you do? I'm curious to know how you figured it out. Post some code if you want to.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Employees are created from a text file. The relevant code in my employee-class:




I have 5 departments (given). For each department, I try to match the text input from the Employee.txt-file with the name of the department. In other words, the text line with department name is transferred to variable departmentInput. This will be matched with the department name (variable of department D). If it matches, this department will be connected to the employee.



In class Department, all departments are made when invoking the method createDepartments. The getters and setters for speed are now:


In other classes that need to know the handling speed of an employee:
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicely done. Have a cow (that's a good thing).

You may want to make sure that getHandlingSpeed() never returns 0 or check it before you use it.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing.

Middle code snippet, line 3, number 5. Exchange it with lisfOfDepartments.size(). Never use magic numbers which doesn't say anything to your reader and it is error-prone. Imagine there will be one department closed after some time - so your index will go out of bounds and you'll get an exception. If there will be more departments openeded - one of your employees might won't know in which department he/she needs to go on Monday morning.
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. Changed the 5

Knute Snortum wrote:You may want to make sure that getHandlingSpeed() never returns 0 or check it before you use it.

I thought about this for a while. Would it be possible to make such a constraint in the getHandlingSpeed()-method?

I thought that when no department fits the input, I could print that no corresponding zone was found, and that a default speed would be used. I was thinking about making a 'Default' department, but I would prefer an error that does not work with 'default' values.


 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about this for a while. Would it be possible to make such a constraint in the getHandlingSpeed()-method?

The question to ask is, Is a handling speed of zero a valid amount? I would think that handling speeds of <= 0 would be invalid. I'd handle that in your setHandlingSpeed() method. What the default might be I don't know; maybe 1, maybe like you suggest, there should be a default department, maybe named "Not Dept Set".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!