luke brown wrote:i got you.but please help me out of the statement item.hashCode(). If i am not wrong then this is the hashCode method of Object class that is been called over here.
I suspect that you're obsessing too much about the mechanics here.
Back up a bit: What is a "Price", and
how do you want it to behave? Specifically: What do you want a
HashSet<Price> to do? Then write
equals() and
hashCode() methods that fulfil
those goals.
Personally, I'm not sure that you actually need a Price class at all (at least not at this stage), since the way you've written it, it would appear to simply be an attribute of an
Item (Note: an
Item,
NOT a String). [*]
It's possibly also worth mentioning that there is a big difference between a "price" (the RRP of an
Item), which you probably don't want duplicated, and a "line-item price" (the price that appears in a sales receipt), which you almost certainly do.
HIH
Winston
[*] Indeed, I suspect you could achieve this by simply renaming your current class to
Item, and renaming its "
item" field (and getter method) to "
name".