• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove from arraylist

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
p mayur
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestion on how i should change the code now.
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
p mayur
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added the product class file. I don't have any equals method there. How i should do it. Please help me.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
p mayur
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. It worked. You people really helped me a lot. I learned a new thing today.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome. I'm glad you got it working.

And welcome to the Ranch!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic