Results 1 to 4 of 4
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Best way to create a group of String,Double?

    The example would be a pizza business that allows toppings on a certain pizza. Each pizza you buy has its own add-ons allowed, and a maximum number of add-ons from each group allowed to be added.

    The String would refer to the topping description e.g. "Beef", "Sweetcorn", etc
    The Double would refer to that particular toppings price.

    I thought of something like the following, incorporating a Map:
    Java Code:
    //constructor
    ProductModifier(Map<String, Double> modifiers...)
    
    //should create 1 group of many modifiers
    ProductModifier pm = new ProductModifier("Beef",1.00,"Sweetcorn",0.50);
    is that possible? or how would you input values into Maps via parameters?

    or should I just go for a child class with 2 fields (one being description, the other being price) and the parent class containing a list of children with a maximum number of selections field?

    Whats better?

    (I've already created the 2nd method, but i'm not sure about it)

    thanks

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    And the context of the question is to make the code more efficient.

    Also, I'd like to be able to re-use each modifier created in other products. So if a group of Toppings is made as in the pizza example, a different pizza should be able to use the product modifier child object created for the first pizza, or maybe the entire group (am not sure myself)

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    I'd suggest HashTable instead of Maps because HashTable is synchronized and it will not allow NULL values. And there are methods in HashTable or HashMap APIs which allow you to insert values into the corresponding data structure.

    One such example for HashTable is given below.

    Java Code:
    		Hashtable<String, Double> toppings = new Hashtable<String, Double>();
    		toppings.put("beef", 1.2);
    		toppings.put("sweetcorn", 0.6);
    		System.out.println(toppings.get("beef"));

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    thanks subith, had I known that before it definately looks a lot simpler and I would've gone for it. yesterday before your post i already rewrote the whole class and tested it and i think its okay now so i won't be changing it. but thanks for the info on hashtable :)

Similar Threads

  1. Extracting Double from String?
    By mutagen in forum New To Java
    Replies: 4
    Last Post: 03-20-2011, 04:30 AM
  2. String to double HELP please!
    By zhen1337 in forum New To Java
    Replies: 33
    Last Post: 02-08-2011, 09:30 AM
  3. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 05:27 PM
  4. String to double errors
    By bigvanilla in forum New To Java
    Replies: 9
    Last Post: 12-05-2010, 08:42 AM
  5. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •