Search in sources :

Example 1 with MonetaryAmount

use of javax.money.MonetaryAmount in project drbookings by DrBookings.

the class BookingEarningsCalculator method calculateEarnings.

public float calculateEarnings(Collection<? extends BookingBean> bookings) {
    if (isPaymentDone()) {
        bookings = bookings.stream().filter(b -> b.isPaymentDone()).collect(Collectors.toCollection(LinkedHashSet::new));
    }
    if (getDateRange() != null) {
        bookings = bookings.stream().filter(b -> getDateRange().contains(b.getCheckOut())).collect(Collectors.toCollection(LinkedHashSet::new));
    }
    MonetaryAmountFactory<?> moneyFactory = Monetary.getDefaultAmountFactory().setCurrency(DrBookingsApplication.DEFAULT_CURRENCY.getCurrencyCode());
    MonetaryAmount result = moneyFactory.setNumber(0).create();
    for (BookingBean b : bookings) {
        result = result.add(moneyFactory.setNumber(b.getEarnings(isNetEarnings())).create());
    }
    return result.getNumber().floatValue();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MonetaryAmount(javax.money.MonetaryAmount) BookingBean(com.github.drbookings.model.data.BookingBean)

Example 2 with MonetaryAmount

use of javax.money.MonetaryAmount in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenAmount_whenCustomFormat_thanEquals.

@Test
public void givenAmount_whenCustomFormat_thanEquals() {
    MonetaryAmount oneDollar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
    MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 �").build());
    String customFormatted = customFormat.format(oneDollar);
    assertNotNull(customFormat);
    assertEquals("USD 1", oneDollar.toString());
    assertEquals("00001.00 US Dollar", customFormatted);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) Test(org.junit.Test)

Example 3 with MonetaryAmount

use of javax.money.MonetaryAmount in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenAmount_whenConversion_thenNotNull.

@Test
@Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() {
    MonetaryAmount oneDollar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
    CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
    MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);
    assertEquals("USD 1", oneDollar.toString());
    assertNotNull(convertedAmountUSDtoEUR);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with MonetaryAmount

use of javax.money.MonetaryAmount in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenArithmetic_whenStringified_thanEqualsAmount.

@Test
public void givenArithmetic_whenStringified_thanEqualsAmount() {
    CurrencyUnit usd = Monetary.getCurrency("USD");
    Money moneyof = Money.of(12, usd);
    MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200.50).create();
    MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
    Money subtractedAmount = Money.of(1, "USD").subtract(fstAmtUSD);
    MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
    MonetaryAmount divideAmount = oneDolar.divide(0.25);
    assertEquals("USD", usd.toString());
    assertEquals("USD 1", oneDolar.toString());
    assertEquals("USD 200.5", fstAmtUSD.toString());
    assertEquals("USD 12", moneyof.toString());
    assertEquals("USD -199.5", subtractedAmount.toString());
    assertEquals("USD 0.25", multiplyAmount.toString());
    assertEquals("USD 4", divideAmount.toString());
}
Also used : FastMoney(org.javamoney.moneta.FastMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.junit.Test)

Example 5 with MonetaryAmount

use of javax.money.MonetaryAmount in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenAmounts_whenSummed_thanCorrect.

@Test
public void givenAmounts_whenSummed_thanCorrect() {
    List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"));
    Money sumAmtCHF = (Money) monetaryAmounts.stream().reduce(Money.of(0, "CHF"), MonetaryAmount::add);
    assertEquals("CHF 111.35", sumAmtCHF.toString());
}
Also used : FastMoney(org.javamoney.moneta.FastMoney) Money(org.javamoney.moneta.Money) MonetaryAmount(javax.money.MonetaryAmount) Test(org.junit.Test)

Aggregations

MonetaryAmount (javax.money.MonetaryAmount)275 Test (org.testng.annotations.Test)260 CurrencyConversion (javax.money.convert.CurrencyConversion)88 BeforeTest (org.testng.annotations.BeforeTest)83 BigDecimal (java.math.BigDecimal)64 CurrencyUnit (javax.money.CurrencyUnit)57 ConversionQuery (javax.money.convert.ConversionQuery)10 RoundedMoney (org.javamoney.moneta.RoundedMoney)10 LocalDate (java.time.LocalDate)9 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)9 Test (org.junit.Test)9 FastMoney (org.javamoney.moneta.FastMoney)6 Money (org.javamoney.moneta.Money)6 MathContext (java.math.MathContext)4 MonetaryOperator (javax.money.MonetaryOperator)4 MonetaryAmountFormat (javax.money.format.MonetaryAmountFormat)4 MonetaryAmountFactory (javax.money.MonetaryAmountFactory)3 BookingBean (com.github.drbookings.model.data.BookingBean)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2