remove from arraylist
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I want to remove the item that is added to the cart. The cart will have the added items, with the delete button against each item.When i click the delete button,
the item is not removed from cart.Cart loads with already added items.How to delete an item.Please help me
Controller code for delete
Cart.java
Edit : Product.java
the item is not removed from cart.Cart loads with already added items.How to delete an item.Please help me
Controller code for delete
Cart.java
Edit : Product.java
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Looks like an object equality issue to me. The object obtained from your productService.getProductById(int id) method is not equal to the object in your Shopping List when compared using.equals(), which a List will do during its .remove() operation.
Edit: (Moderator Tim) Moved from Spring forum to Java in General. I don't think this issue has anything to do with Spring.
Edit: (Moderator Tim) Moved from Spring forum to Java in General. I don't think this issue has anything to do with Spring.
Tim Driven Development | Test until the fear goes away
p mayur
Ranch Hand
Posts: 30
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Any suggestion on how i should change the code now.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I would suggest a couple things. First, if you have not already you may need to override the equals() method in your Product class. This will ensure the ArrayList.remove() method is properly determining if the objects are equal.
Second I would output the "id" parameter being passed into the removeFromCart() method on the controller and the Index of the object in the ArrayList to see if they are correct.
Like Tim said, it seems like an equality issue is which case you should have a look at the equals() method in the Product class. If the Product class does not have it's own equals() method, you should add one and override the one inherited from the base Object class.
Second I would output the "id" parameter being passed into the removeFromCart() method on the controller and the Index of the object in the ArrayList to see if they are correct.
Like Tim said, it seems like an equality issue is which case you should have a look at the equals() method in the Product class. If the Product class does not have it's own equals() method, you should add one and override the one inherited from the base Object class.
p mayur
Ranch Hand
Posts: 30
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have added the product class file. I don't have any equals method there. How i should do it. Please help me.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Don't forget when overriding .equals() you must also override hashcode() and implement canEqual(). Luckily we have a How-To section on this very topic here.
Tim Driven Development | Test until the fear goes away
p mayur
Ranch Hand
Posts: 30
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks to all. It worked. You people really helped me a lot. I learned a new thing today.
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You're very welcome. I'm glad you got it working.
And welcome to the Ranch!
And welcome to the Ranch!
Tim Driven Development | Test until the fear goes away
| look! it's a bird! it's a plane! It's .... a teeny tiny ad Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









