Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove item in shopping cart

 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic