• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

add only unique values into the ArrayList

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5126
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28424
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic