Results 1 to 4 of 4
-
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:
is that possible? or how would you input values into Maps via parameters?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);
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
-
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)
- 04-18-2011, 02:37 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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"));
-
Similar Threads
-
Extracting Double from String?
By mutagen in forum New To JavaReplies: 4Last Post: 03-20-2011, 04:30 AM -
String to double HELP please!
By zhen1337 in forum New To JavaReplies: 33Last Post: 02-08-2011, 09:30 AM -
Convert from string to double
By Lord ice in forum New To JavaReplies: 4Last Post: 12-12-2010, 05:27 PM -
String to double errors
By bigvanilla in forum New To JavaReplies: 9Last Post: 12-05-2010, 08:42 AM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks