Skip to content
Advertisement

Boolean failing to return the correct value

I have four classes Customer, Store and Item and Online.

I am using ‘LinkedList’ to move around classes.

I added items at the Customer class.

When I want to check whether the item under exists or not under Online class.

Because you cannot check at the Customer.

I use the hasItem().It’s failing constantly.

When I say that, I mean even though I type “001A:GTA”, the item is store in the data and it’s not working. it keep returning null.

Can someone explain to me? I really appreciate it. I am a complete begineer.

Item Class

JavaScript

Store class

JavaScript

Customer

JavaScript

Online Class

JavaScript

Advertisement

Answer

Override Object::equals

Do it as follows:

JavaScript

Output:

JavaScript

Note that it doesn’t sound good for the class, Item to have a method called, hasItem. It gives an impression as if class, Item is a container class. Also, it is always a recommended practice to override Object::equals and Object::hashCode in a class. It makes your code clean (as you can see in the code given above) and helps you write logic in an easy manner; especially when you are dealing with collections.

Advertisement