Forums Register Login

Removing items from arraylist

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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>

<%
}
%>
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
 

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
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2064 times.
Similar Threads
Problem filling Select from Database - c:forEach
ActionForm with indexed properties
(IN) PREDICATE in sql
Shopping cart application using struts
Instantiation Exception
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:28:29.