If I were you, I would set the scale of the BigDecimal so that I dont end up on lengthy numbers. The integer 2 in the BigDecimal initialization below sets the scale.
Since you have lots of mismatch of data type, I have changed it accordingly to adjust.
class Payment
{
BigDecimal itemCost=new BigDecimal(BigInteger.ZERO, 2);
BigDecimal totalCost=new BigDecimal(BigInteger.ZERO, 2);
public BigDecimal calculateCost(int itemQuantity,BigDecimal itemPrice)
{
BigDecimal itemCost = itemPrice.multiply(new BigDecimal(itemQuantity));
return totalCost.add(itemCost);
}
}
BigDecimals are Object , not primitives, so make sure you initialize itemCost
and totalCost
, otherwise it can give you nullpointer while you try to add on totalCost
or itemCost