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();
}
}
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:
addProduct
to String
- the code would continue to compile, because you construct BigDecimal
inside the method, ornew 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;
}
Check the available constructors here. There is one that accepts long as parameter (BigDecimal(long val)
).
register.addProduct("Coffee", new BigDecimal(1.25));
register.addProduct("Cappuccino", new BigDecimal(1.50));
Check comment of @BackSlash, it appears that passing a double
or float
to BigDecimal
may be not handled precisely, so use BigDecimal(String val)
instead:
register.addProduct("Coffee", new BigDecimal("1.25"));
register.addProduct("Cappuccino", new BigDecimal("1.50"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With