aspose file tools*
The moose likes Beginning Java and the fly likes Ordered List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ordered List" Watch "Ordered List" New topic
Author

Ordered List

Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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).]


SCJP Tipline, etc.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
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).]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ordered List