wood burning stoves 2.0*
The moose likes Servlets and the fly likes remove item in shopping cart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "remove item in shopping cart" Watch "remove item in shopping cart" New topic
Author

remove item in shopping cart

syidah mk
Greenhorn

Joined: Feb 03, 2005
Posts: 13
hi all.. i need ur help to check what's wrong with my codes..

e.g.
item 1 Remove btn
item 2 Remove btn
item 3 Remove btn

when i click on the Remove btn of item 2 in shopping cart page.. it removes the item 1.. y???

below are the codes:


public void removeItem(String id){
Enumeration productEnum = getProducts();
while(productEnum.hasMoreElements()){
Product product = (Product)productEnum.nextElement();
products.removeElement(product);



break;

}
}






pls i desperately need someone.. to help me .. :<
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The code you show will always remove the first item. Your enumeration hits the first element, removes it, and then breaks out of the enumeration. Instead, you want to iterate through the enumeration and get the next product as you are doing, but only delete it if its id matches the id received as a parameter, and then break out of iterating through the enumeration (if the id doesn't match, you don't delete the current element, or break out of the enumeration; instead just loop through to the next element).

It should be noted that iterating through a shopping cart like that to remove an item is very inefficient. What type of collection (i.e. what class) are you using for your Shopping Cart? Take a look through the various collection classes in the Java API; you can learn about them via the Sun Tutorial on the Collection Framework. You should see a collection type that is geared towards this type of thing, and will allow you to add and remove items in a very efficient manner, without needing to iterate through them.
syidah mk
Greenhorn

Joined: Feb 03, 2005
Posts: 13
Originally posted by Mark Vedder:
The code you show will always remove the first item. Your enumeration hits the first element, removes it, and then breaks out of the enumeration. Instead, you want to iterate through the enumeration and get the next product as you are doing, but only delete it if its id matches the id received as a parameter, and then break out of iterating through the enumeration (if the id doesn't match, you don't delete the current element, or break out of the enumeration; instead just loop through to the next element).

It should be noted that iterating through a shopping cart like that to remove an item is very inefficient. What type of collection (i.e. what class) are you using for your Shopping Cart? Take a look through the various collection classes in the Java API; you can learn about them via the Sun Tutorial on the Collection Framework. You should see a collection type that is geared towards this type of thing, and will allow you to add and remove items in a very efficient manner, without needing to iterate through them.


thanks for the reply.. the code works.. but when i used sun 1 microsystem .. but now i have to used websphere.. and this problem faced.. and i dunno wat to do...
syidah mk
Greenhorn

Joined: Feb 03, 2005
Posts: 13
and here are my new codes..

public void removeItem(String id){

System.out.println("Id = " + id);
for(Enumeration productEnum = getProducts();productEnum.hasMoreElements() {
Product product = (Product)productEnum.nextElement();
System.out.println("product id = " + product.getId());
if(id.equals(product.id)){
products.removeElement(product);
break;
}
}
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, if you use the code button to wrap your code, the indenting will be preserved (which means more people will take the time to look at it).
Also there is a checkbox to disable smileys so they don't show up in the middle of your code.

Why are you still pulling the whole enumeration from you collection and looping through them? Mark already asked you what type of data structure you're using and suggested that this wasn't necessary.

If Products has a remove(String) method, you shouldn't need to loop through the keys at all.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
syidah mk
Greenhorn

Joined: Feb 03, 2005
Posts: 13
i'm using a vector.. and i thought if u use vector.. u need this enumeration loop??..

and this is a new code:

public void removeItem(String id){

Enumeration productEnum = getProducts();
System.out.println(productEnum);
while(productEnum.hasMoreElements()){
Product product = (Product)productEnum.nextElement();
String p = product.getId();
System.out.println("\n Inside removeItem(..) prodId , Id = " + p.trim() + ", "+ id);
System.out.println(productEnum);
if(p.trim()==id.trim()){
System.out.println("\n One match Found, Deleting it ");
products.removeElement(product);
System.out.println("Id = " + id);
System.out.println("product id = " + p.trim());
System.out.println(productEnum);
}
}
}

this works when i'm using sun one studio but when i used websphere.. it doesn't work.. so any of the experts know what's happening?? ..

what i found out is that it doesn't go to the if statement?? y??

and the output is like this:
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you put the code tags around your code, the indentation will be preserved and it will be much easier for us to read.

You're using the == operator to compare strings.
You should use the equals() method of String.
You were but you switched.



The '==' operator, when comparing object variables, will return true only if both variables point to the same object. Since you're using trim (which returns a new string) on your strings, it will always return false.
syidah mk
Greenhorn

Joined: Feb 03, 2005
Posts: 13
Originally posted by Ben Souther:
If you put the code tags around your code, the indentation will be preserved and it will be much easier for us to read.

You're using the == operator to compare strings.
You should use the equals() method of String.
You were but you switched.



The '==' operator, when comparing object variables, will return true only if both variables point to the same object. Since you're using trim (which returns a new string) on your strings, it will always return false.


wow.. thanks you so much for helping me.. it works!!! frankly speaking, i'm not good in programming..hooray.. can party..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remove item in shopping cart
 
Similar Threads
A better way to implement this?
LineItem class in Cade's class diagram
Why doesn't exercise 8-1 from Murach's Java Servlets work for me on IE7?
Update cart asynchronously please
Problem with session