• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ordered List

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to figure out what I should use to implement a shopping cart. I've got a class that implements a product instance, and I want to have add() and remove() and updateQty() methods as part of my cart class. What I'm trying to figure out is what class to use to keep the list of products in the cart. I've been looking at all the different lists and collections and frankly I'm lost. Something quick and easy to plug into my cart class would be ideal. Also, I will need to alphabetize them, so I was looking at classes that use the compareTo() method, which I have implemented in my product class.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

Well, I think for simplicity's sake, you have two options on lists, either ArrayList or Vector. Both implement the List interface and therefore have the add and remove methods that you're after. The only difference is that Vector is thread-safe. If your application is going to have multiple threads accessing this list at once, use Vector, if not, cut down on complexity and use ArrayList.


As far as sorting the list goes, take a look at the class java.util.Arrays. It offers a lot of static methods that can do all sorts of great things to arrays of data. Fortunely, the List interface supplies a toArray() method. In order to sort your data, you just need to do something like this:


Object[] o = yourList.toArray();
Arrays.sort(o);
yourList = new ArrayList(Arrays.asList(o));


Assuming the variable yourList had your items in it to begin with, it will then contain all of the same items, but sorted according to their "natural order" (in other words, the order defined within the compareTo operator).


As for the updateQty method, thaht sounds like it should be a method of the object contained in the list, rather than on the list itself. I'd simply write a method that uses an iterator to search through the list, find the item that you want to update, and sets its qty variable (most likely through a "setter" method).


I hope this helps,

Corey
[This message has been edited by Corey McGlone (edited December 20, 2001).]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
As far as sorting the list goes, take a look at the class java.util.Arrays. It offers a lot of static methods that can do all sorts of great things to arrays of data. Fortunely, the List interface supplies a toArray() method. In order to sort your data, you just need to do something like this:


If you're working with a List, just use java.util.Collections instead of java.util.Arrays for sorting and so forth.

[This message has been edited by Michael Matola (edited December 20, 2001).]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps not relevant to this particular application, but worthy of note ...
Where you want to maintain ordering of a collection, you can consider using a SortedSet or SortedMap. There are implementations of these in JDK called TreeSet and TreeMap.
These have the advantage that they stay ordered, as items are added and removed. In many applications, this may be more efficient than explicitly sorting a collection.
Because SortedSet only stores object references, it is often not terribly expensive to have more than one SortedSet containing references to the same set of object, but ordered differently. This should enable lightning-fast switching of the sort key, from the user's point of view, as no actual sorting is needed.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those two would be good only if there is not a requirement that duplicate entries are allowed.
If you want a relation between an item and the quantity, SortedMap would work good, have the item name as a String for your key, and an Integer object encapsulating the quantity as your value.

[This message has been edited by jason adam (edited December 21, 2001).]
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic