MoneyUtils { public static Money getMoney(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded) { if (money == null) { return new Money(Total.ZERO, Total.ZERO, Total.ZERO, Total.ZERO, taxRate, taxIncluded); } final BigDecimal tax = getTaxAmount(money, taxRate, taxIncluded); if (taxIncluded) { return new Money(money, money.subtract(tax), money, tax, taxRate, taxIncluded); } return new Money(money, money, money.add(tax), tax, taxRate, taxIncluded); } private  MoneyUtils(); static BigDecimal max(final BigDecimal first, final BigDecimal second); static BigDecimal secondOrFirst(final Pair<BigDecimal, BigDecimal> firstAndSecond); static BigDecimal minPositive(final Pair<BigDecimal, BigDecimal> firstAndSecond); static BigDecimal minPositive(final BigDecimal first, final BigDecimal second); static BigDecimal notNull(final BigDecimal value); static BigDecimal notNull(final BigDecimal value, final BigDecimal ifNull); static boolean isPositive(final BigDecimal value); static boolean isFirstBiggerThanSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstBiggerThanOrEqualToSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstEqualToSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstEqualToSecond(final BigDecimal first, final BigDecimal second, final int scale); static BigDecimal getDiscountDisplayValue(final BigDecimal original, final BigDecimal discounted); static Money getMoney(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getTaxAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getNetAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getGrossAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static final BigDecimal ZERO; static final BigDecimal ONE; static final BigDecimal HUNDRED; }
@Test public void testGetMoney() throws Exception { final MoneyUtils.Money moneyIncl = MoneyUtils.getMoney(new BigDecimal("100.00"), new BigDecimal("20.00"), true); assertEquals("100.00", moneyIncl.getMoney().toPlainString()); assertEquals("83.33", moneyIncl.getNet().toPlainString()); assertEquals("100.00", moneyIncl.getGross().toPlainString()); assertEquals("16.67", moneyIncl.getTax().toPlainString()); assertEquals("20.00", moneyIncl.getRate().toPlainString()); assertTrue(moneyIncl.isIncluded()); final MoneyUtils.Money moneyExcl = MoneyUtils.getMoney(new BigDecimal("100.00"), new BigDecimal("20.00"), false); assertEquals("100.00", moneyExcl.getMoney().toPlainString()); assertEquals("100.00", moneyExcl.getNet().toPlainString()); assertEquals("120.00", moneyExcl.getGross().toPlainString()); assertEquals("20.00", moneyExcl.getTax().toPlainString()); assertEquals("20.00", moneyExcl.getRate().toPlainString()); assertFalse(moneyExcl.isIncluded()); final MoneyUtils.Money moneyNullIncl = MoneyUtils.getMoney(null, BigDecimal.ZERO, true); assertEquals("0.00", moneyNullIncl.getMoney().toPlainString()); assertEquals("0.00", moneyNullIncl.getNet().toPlainString()); assertEquals("0.00", moneyNullIncl.getGross().toPlainString()); assertEquals("0.00", moneyNullIncl.getTax().toPlainString()); assertEquals("0", moneyNullIncl.getRate().toPlainString()); assertTrue(moneyNullIncl.isIncluded()); final MoneyUtils.Money moneyNullExcl = MoneyUtils.getMoney(null, BigDecimal.ZERO, false); assertEquals("0.00", moneyNullExcl.getMoney().toPlainString()); assertEquals("0.00", moneyNullExcl.getNet().toPlainString()); assertEquals("0.00", moneyNullExcl.getGross().toPlainString()); assertEquals("0.00", moneyNullExcl.getTax().toPlainString()); assertEquals("0", moneyNullExcl.getRate().toPlainString()); assertFalse(moneyNullExcl.isIncluded()); final MoneyUtils.Money moneyZeroIncl = MoneyUtils.getMoney(new BigDecimal("100.00"), BigDecimal.ZERO, true); assertEquals("100.00", moneyZeroIncl.getMoney().toPlainString()); assertEquals("100.00", moneyZeroIncl.getNet().toPlainString()); assertEquals("100.00", moneyZeroIncl.getGross().toPlainString()); assertEquals("0.00", moneyZeroIncl.getTax().toPlainString()); assertEquals("0", moneyZeroIncl.getRate().toPlainString()); assertTrue(moneyZeroIncl.isIncluded()); final MoneyUtils.Money moneyZeroExcl = MoneyUtils.getMoney(new BigDecimal("100.00"), BigDecimal.ZERO, false); assertEquals("100.00", moneyZeroExcl.getMoney().toPlainString()); assertEquals("100.00", moneyZeroExcl.getNet().toPlainString()); assertEquals("100.00", moneyZeroExcl.getGross().toPlainString()); assertEquals("0.00", moneyZeroExcl.getTax().toPlainString()); assertEquals("0", moneyZeroExcl.getRate().toPlainString()); assertFalse(moneyZeroExcl.isIncluded()); }