• Post Reply Bookmark Topic Watch Topic
  • New Topic

add only unique values into the ArrayList  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have made a shopping cart that contains product class, LineItems class and Order class.
I Order class that has addToCart method that adds only the unique products. Like if i add Books and Books item is already present  , then add only the quantity. I have made an attempt but it adds the renewed quantity as
well as the old one.  I even tried using a HashSet still its not working.

Please help.




Main Class
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it adds the renewed quantity as well as the old one.

Can you give an example that shows what you mean?
For example I'd expect this:
given an old value = 3
add in a new count = 4
gives a new value = 3+4 = 7

Where in the posted code does it try to add in the new value?
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm

Here in this method it is adding the new quantified item as well as the old.



Output -

size =3
Name =Kellogs Cost =100.0 product id = 1 Quantity = 30 Id  = 1 cost = 2700.0
Name =Kellogs Cost =100.0 product id = 1 Quantity = 10 Id  = 1 cost = 900.0
Name =Vaseline Cost =250.0 product id = 2 Quantity = 20 Id  = 2 cost = 4600.0



for eg. here in this output , i want only kellogs with quantity 30 (since i gave two inputs - with quantity 20 and quantity 10 , so add To Cart method adds the quantity to 30). Here once the kellogs with quantity 30 is added i want to add only quantity 30  Kellogs to it. And not both the values of Kellogs
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This line... Don't use the == operator to compare the contents of two String objects; it compares whether the two variables refer to the same object. Use the equals() method of the String object instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!