I am learning Java8 and BigDecimal. The following exercise involvedwriting a Tax Calculator that took a salary and calculated the taxpayable based on the tax band into which the salary falls. The testpasses as the code stands but if I remove the call to setScale(0, BigDecimal.ROUND_UP)then the rounding does not occur and the tests fail like this >TaxCalculator.calculateTax > 712.50java.lang.AssertionError:  Expected: is <713>      but: was <712.50>I don't understand that because the 2nd parameter to the divide callspecifies that rounding should be performed. Also, Can anyone suggestimprovements to the way this class is written please? I'm just tryingto nail down best practices with respect to the Streams API. Am I usingan appropriate way to calculate a percentage in Java?import org.junit.Before;import org.junit.Test;import java.math.BigDecimal;import java.util.HashMap;import java.util.TreeMap;import static org.hamcrest.CoreMatchers.is;import static org.junit.Assert.assertThat;public class TaxCalculatorTest {    private TaxCalculator taxCalculator;    private HashMap<BigDecimal, BigDecimal> testData = new HashMap<>(); // <salary, taxPayable>    private TreeMap<BigDecimal, BigDecimal> taxBands = new TreeMap<>(); // uses natural ordering of keys    // Could have used LinkedHashMap to maintain order but that relies on entries being inserted in the correct order    @Before    public void setup() {        testData.put(new BigDecimal(5000), new BigDecimal(225));        testData.put(new BigDecimal(11000), new BigDecimal(413));        testData.put(new BigDecimal(19000), new BigDecimal(713));        testData.put(new BigDecimal(27000), new BigDecimal(905));        taxBands.put(new BigDecimal(10000), new BigDecimal(4.5));        taxBands.put(new BigDecimal(35000), new BigDecimal(3.35));         // TreeMap fixes the ordering        taxBands.put(new BigDecimal(20000), new BigDecimal(3.75)); // This entry will be inserted before the previous one        taxCalculator = new TaxCalculator(taxBands);    }    @Test    public void calculateTax(){        testData.forEach((salary, taxPayable) -> {            assertThat(taxCalculator.calculateTax(salary), is(taxPayable));        }) ;    }}import java.math.BigDecimal;import java.util.HashMap;import java.util.TreeMap;class TaxCalculator {    private static final BigDecimal NO_TAX = new BigDecimal(0);    /*     * scale > the number of digits to the right of the decimal point     *     * precision > the number of significant digits     *     * BigDecimal instances are immutable     *     */    private BigDecimal ONE_HUNDRED = new BigDecimal(100);    private final TreeMap<BigDecimal, BigDecimal> taxBands;    TaxCalculator(TreeMap<BigDecimal, BigDecimal> taxBands) {        this.taxBands = taxBands;    }    BigDecimal calculateTax(BigDecimal salary) {        BigDecimal taxPayable = salary.multiply(getTaxRate(salary)).divide(ONE_HUNDRED, BigDecimal.ROUND_UP).setScale(0, BigDecimal.ROUND_UP);        System.out.println(TaxCalculator.calculateTax >  + taxPayable);        return taxPayable;    }    private BigDecimal getTaxRate(BigDecimal salary) {        return taxBands.entrySet().stream()                .filter((entry) -> salary.compareTo(entry.getKey()) < 0)                .map(HashMap.Entry::getValue)                .findFirst().orElse(NO_TAX);    }}
Tax Calculator using Java 8 and BigDecimal
java;stream