Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Maps and Sets help please  RSS feed

 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working on this question for a couple of days and I just have no idea on how to do it. I have tried everything I can and I am not any further in getting the solution. It is literally my last resort posting this on here. It has quite a bit of coding, but I really appreciates anyone who takes the time to help me

I have the Employee class below which I am not allowed to change.








What I have to do is write a public method updatesSalesFigures() in the Payroll class which returns no value but should take as its argument a map whose keys are strings and whose values are integers. The strings represent employee numbers and the integers the respective sales figures. For each Employee in employeeSet your method should check whether its employeeNumber is one of the keys in the map. If so, the sales for that Employee should be increased by the map value corresponding to that key.

To test the code, I have to use the Sales method which method getSales()returns a map whose keys are strings representing employee numbers and whose values are integers. I have tried to do it if you look at the updateSalesFigures, but I have done very little and not sure if that is even correct. I think it takes a for-each and an if statement though. I haven't got a clue on what to do if I am being honest.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to think this through logically and don't get caught up with the language machinations. It may seem daunting but if you think about what you need to do first, then the how will quickly follow.

First, what does that Map<String, Integer> that getSales returns represent? What does the String represent and what does the integer represent?
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you didn't post the complete code were given, there's a flaw in the Employee class: It doesn't override the equals() and hashCode() methods. That means that any Employee object you add to a Set will be considered unique because Object.equals() implementation will be used. This means that two Employee objects with the same employeeNumber value will still be seen as unique. That's not right.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rickey Fuller wrote:I have tried everything I can and I am not any further in getting the solution.

What I have to do is write a public method updatesSalesFigures() in the Payroll class which returns no value but should take as its argument a map whose keys are strings and whose values are integers. The strings represent employee numbers and the integers the respective sales figures. For each Employee in employeeSet your method should check whether its employeeNumber is one of the keys in the map. If so, the sales for that Employee should be increased by the map value corresponding to that key.

I have tried to do it if you look at the updateSalesFigures, but I have done very little and not sure if that is even correct.

Well, at least you're honest. That is very little. You probably should try to put some more effort into writing some code that will actually do something, even if it's the wrong thing. Nobody around here is going to do your homework for you.

Break down the problem into small tasks, then figure out what's involved in each small task. For example, one task is to "check whether an EmployeeNumber is one of the keys in the map." Try to translate that into Java code and see if you can get that small part of the program to work.
 
Dave Tolls
Rancher
Posts: 2909
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Unless you didn't post the complete code were given, there's a flaw in the Employee class: It doesn't override the equals() and hashCode() methods. That means that any Employee object you add to a Set will be considered unique because Object.equals() implementation will be used. This means that two Employee objects with the same employeeNumber value will still be seen as unique. That's not right.


Yes it's technically an error, but I suspect whoever wrote it either knew TreeSet only uses compareTo, or they simply struck lucky.
Either way, they should have done it properly since it is a Set...but it won't break the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!