• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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
 
Marshal
Posts: 5222
323
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
Marshal
Posts: 5222
323
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
Marshal
Posts: 5222
323
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!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic