• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing items from arraylist  RSS feed

 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm having a problem whereby I have two java classes, one (BasketItem) which sets the values of products (e.g. title, artist, price) these are then added to an arraylist in another class (ShoppingCart). I am using JSP to handle user requests to add products to the cart and also increment quantities. However, I am having trouble allowing users to remove items from their carts. What I have is a link next to the item to be deleted which is sent to a controlling JSP page which handles deleting the selected item from the arraylist. I can't figure out how to select the appropriate entry from the arraylist based on the name of the product, and then delete it. Can nayone help? Thanks.
 
Vasilis Karas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you display the cart...how do you pull everything out to display it on the JSP page? Since ArrayLists store everything in sequence and can be referenced by index numbers---I would recommend that when you display the contents out of the array list generate a hyplerlink with a parameter index value starting from 0..to ArrayListSize-1...
example.
I'm on a Jsp page display contents of shopping cart...
<%
int sizeOfCart = myCartIsAnArrayList.size();
for(int index = 0;index < sizeOfCart; index++)
{
// some code to pull the objects out of the ArrayList(shoppingcart)

>%
//now I'm in HTML dynamically generating the hyplerink the the controlling JSP page--which will be looking for a "removeIndex" parameter
<a <HREF="http://yourcontrolling.jsp?removeIndex=<%=index%>">Remove Item</a>

<%
}
%>
 
Vasilis Karas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhh.forgot to mention
on the receiving page..
you'll get this removeIndex parameter and do something like

yourArrayList.removeElementAt(removeIndex)<---you'll need to convert that parameter to an 'int' priort to making the removeElementAt call.
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Red, well I use the BasketItems class methods getTitle, getArtist etc to display the contents of the arrayList. But your idea sounds vey good, I'm gonna give it a try now. Thanks, I'll let you know how it goes.
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! It works! Now I just have to deal with only removing one of the items when there is more than that in the ArrayList (i.e. when the quantity is >1) But thanks Basil, if you have any ideas to share about the quantity issue I'd really appreciate it!
 
Vasilis Karas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shuzo monsoon:
Thanks so much! It works! Now I just have to deal with only removing one of the items when there is more than that in the ArrayList (i.e. when the quantity is >1) But thanks Basil, if you have any ideas to share about the quantity issue I'd really appreciate it!

not sure I follow you on the quantity issue but let me take a stab.
You have an Item(object) in your ArrayList(ShoppingCart), now these items have a quantity associated with them? Is this what you mean?
Once again you can reference you're arrayList directly providing it an index. So you'll pass in a parameter for index and another paramter for quantity---pull that Object out of your arraylist and update its quantity..
Hope that helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!