Is using a HashMap the best way to calculate the price of a part that has multiple sub parts?

Viewed 1430

Question: Write a program that calculates the price of a part. This part has multiple sub-parts with their own respective cost. Would using a HashMap be the best way to do this?

Here's what I have:

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.text.NumberFormat;

public class RocketStructure {

 public static void main(String args[]) {

   /*Declare HashMap and Formatter*/
   HashMap<String, Double> hmap = new HashMap<String, Double>();
   NumberFormat formatter = NumberFormat.getCurrencyInstance();

   /*Adding elements to HashMap (Name, Price)*/
   hmap.put("Nose Cone", 1233863.45);
   hmap.put("Frame", 76495.26);
   hmap.put("Pumps", 4679358.25);
   hmap.put("Fin", 467945.25);
   hmap.put("Nozzle", 46235.72);

   /*HashMap Size and Part Information*/
   System.out.println("Rocket Structure | Total Parts: " +hmap.size());
   System.out.println("----------------------------------");

   /*Display content using iterator*/
   Set set = hmap.entrySet();
   Iterator iterator = set.iterator();
   while(iterator.hasNext()) {
      Map.Entry mentry = (Map.Entry)iterator.next();
      System.out.print(mentry.getKey() + " | Cost: ");
      System.out.println(formatter.format(mentry.getValue()));
   }

   /*Calculate total value of all sub parts*/
   double sum = 0f;
   for (double value : hmap.values()) {
      sum += value;
   }
   System.out.println("----------------------------------");
   System.out.println("Total Cost: " +formatter.format(sum));
 }
}

Result:

Rocket Structure - Total Parts: 5


Frame | Cost: $76,495.26

Nose Cone | Cost: $1,233,863.45

Pumps | Cost: $4,679,358.25

Nozzle | Cost: $46,235.72

Fin | Cost: $467,945.25


Total Cost: $6,503,897.93

3 Answers

Yes, or you can use 2 separate List. One is for object the other for price. And you work with index like : Object[i] Price[i] you see what I mean ? Hopefully I helped a bit you

Yup. HashMap's are known for their 'Key-Value Pairs', and using them in this particular scenario would be the best solution here, in order to avoid the usage or multiple singular collections and maintaining the index values.

Opmtimality, this is. :)

Thanks.

The real answer here: create a class that represents a product. And then that Product class could have a reference to some children, that are Products as well. Then you could do something like:

class Product {
  List<Product> children ...
  double basePrice ...

  Product(double basePrice) { ...

  Product(List<Product> children) { ...

  double getPrice() { 
    if (children.isEmpty()) return basePrice;
    else return SUM over getPrice() called on each child

Meaning: when a product isn't made up of other products, it has a fixed base price. But if a product consists of other products, then its price is computed from the children.

The key point here: the relation between price and product should be made explicit. Ideas to use different lists, or a map ... are fine for a first draft version. But in any realistic scenario, you want to really model such relationships. And in good OOP, the key thing to model is: creating appropriate class structures!

And further thinking: actually you would turn to polymorphism here. You start with an abstract base Product class that has an abstract getPrice() method. Then you create two subclasses: one with a fixed price. And one that contains child products - where the price is the sum of the child prices.

Related