Linda Linda - 6 months ago 73
Java Question

How to pass a BigDecimal to a method?

this is my first Java app. I'm trying to create a map of products (keys) and their prices (values). The documentation says I can't use doubles for money, so I have to use BigDecimal. I'm completely stuck at the moment as to how to use BigDecimal in my methods and the map.

I only have a method to add products to the map and I'm trying to pass in a BigDecimal Price. It won't compile, because it sees a string instead of a BigDecimal, which confuses me, because BigDecimal accepts strings as a parameter (e.g. BigDecimal example = new BigDecimal("2.50") )

I also want to create a method that will add prices to a total, but you can't do that with strings. The documentation on BigDecimal is very confusing and Google isn't very helpful. Could someone please explain BigDecimal and how to use it?

This is my code so far:

Register class:

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class Register {
private HashMap<String, BigDecimal> productMap = new HashMap<>();

public void printProductMap() {
for(Map.Entry<String, BigDecimal> entry : productMap.entrySet()) {
String key = entry.getKey();
BigDecimal value = entry.getValue();
System.out.println(key + " " + value);
}
}

public HashMap<String, BigDecimal> addProduct(String product, BigDecimal price) {
BigDecimal productPrice = new BigDecimal(price);
productMap.put(product, productPrice);
return productMap;
}
}


Main class:

public class Main {

public static void main(String[] args) {

Register register = new Register();
register.addProduct("Coffee", "1.25");
register.addProduct("Cappuccino", "1.50");
register.printProductMap();
}
}

Answer Source

The fact that BigDecimal has a constructor that takes a String does not mean that it is fine to pass a String to a method taking BigDecimal.

There are two solutions to this problem:

  • Change the type of second parameter of addProduct to String - the code would continue to compile, because you construct BigDecimal inside the method, or
  • Pass new BigDecimal for second parameter - the call would look as follows: register.addProduct("Coffee", new BigDecimal("1.25"));

The change for the first approach would look like this:

public HashMap<String, BigDecimal> addProduct(String product, String price) {
    // This is the only change -------------------------------^^^^^^
    BigDecimal productPrice = new BigDecimal(price);
    productMap.put(product, productPrice);
    return productMap;
}

The change for the second approach would look like this:

public HashMap<String, BigDecimal> addProduct(String product, BigDecimal price) {
    // No need to create productPrice, because price is BigDecimal
    productMap.put(product, price);
    return productMap;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download