• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing pairs of objects  RSS feed

 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, is there anyone that can give me advide as to a collection or something similar I can use to store paris of objects that are related to each other? Let me clarify, I am trying to basically store a shopping cart and I have a class which gets the items name and stores it in a vector (or arraylist) but I am having trouble accessing the related price for each object. So what I need to do is store the item name AND price as a pair so that I can then access both to diplay to the user. I have tried using separate vectors for each but obviously thats not a good idea. Any ideas?
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java provides three colleciton types List, Map and Set. A Vector can be considered a list - though in actual fact it is a legacy collection, which you probably shouldn't use. The characteristics of each type are:
List - a sequence of values (e.g. 1,1,2,3)
Set - a collection (sequence or map) of values where each value is unique
Map - an object that maps keys to values.
What you probably want is a HashMap, keyed on item name with item price as its value. Have a look at the Collections tutorial on Sun's site too; understanding collections is a very important aspect of Java programming.
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John. I'll look up HashMaps straight away. Just another questions though, is is possible to add the name of the item first (using a set method) and then in the same class, adding the price for that item later? I suppose I'll have to research this.
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMaps have a put(object key, Object value) method to add objects to the map. It is perfectly feasable to say map.put("item name", null) if you don't know the price at the time - though I'm not sure why you might want to do this. If later on you say map.put("item name", new Double(2.50)) the original value with the key "item name" is overwritten with the new value.
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried using the HashMap in my class but when I say basket.put(product) - where basket is the name of the hashmap and product the name of the item, I get an error saying that the method add(string) is undefined for type hashmap. And what I meant by adding the price 'later' may be incorrect as I have setProduct and setPrice methods and I can use these to add both to the hashmap at the same time. so it's just the error I need to resolve I think.
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the docs. There is no put(Object object) method for a HashMap, neither is there an add(Object object) method.
[ March 11, 2004: Message edited by: john smith ]
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I realised that after i posted the message. I think I need to pay a bit more attention next time.
 
Greg Reinl
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to consider creating a class that represents an item in your sopping cart (maybe called ShoppingCartItem) which has the product name and price as attributes. That way, instead of using a HashMap to hold the contents of your shopping cart, you could just use a List or Set of ShoppingCartItems. The advantage with this approach is that each shopping cart item can have more then just product name and price attributes as well as have behaviors of their own.
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Greg, that's next on my list if I give up on these HashMaps! Anyway, now I've managed to get the name and price of items in pairs in a hashmap, however, the problem I am facing now is how to display them separately on a page. For example, my class uses an addProduct method to add the name and price of an item to the hashmap and a getProducts method to return the hashmap. When I use this getProducts() method I want to display the name separately from the price, i.e. in separate rows of a table. Is there a way I can call the name using the method and then separately call the price using the same method?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!