Results 1 to 4 of 4
- 04-18-2011, 12:28 PM #1
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:
//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?
(I've already created the 2nd method, but i'm not sure about it)
- 04-18-2011, 12:31 PM #2
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 #3Senior Member
- Join Date
- Jan 2011
- Bangalore, India
- Rep Power
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.
Hashtable<String, Double> toppings = new Hashtable<String, Double>(); toppings.put("beef", 1.2); toppings.put("sweetcorn", 0.6); System.out.println(toppings.get("beef"));
- 04-19-2011, 11:22 AM #4
- By mutagen in forum New To JavaReplies: 4Last Post: 03-20-2011, 05:30 AM
- By zhen1337 in forum New To JavaReplies: 33Last Post: 02-08-2011, 10:30 AM
- By Lord ice in forum New To JavaReplies: 4Last Post: 12-12-2010, 06:27 PM
- By bigvanilla in forum New To JavaReplies: 9Last Post: 12-05-2010, 09:42 AM
- By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 09:03 PM