Search in sources :

Example 1 with CurrencyUnit

use of javax.money.CurrencyUnit in project rulesservice by genny-project.

the class QRules method calcOwnerFee.

public Money calcOwnerFee(Money input) {
    CurrencyUnit DEFAULT_CURRENCY_TYPE = input.getCurrency();
    Number inputNum = input.getNumber();
    Money ownerFee = Money.of(0, DEFAULT_CURRENCY_TYPE);
    Number RANGE_1 = 999.99;
    Number RANGE_2 = 2999.99;
    Number RANGE_3 = 4999.99;
    Number FEE_1 = 0.15;
    Number FEE_2 = 0.125;
    Number FEE_3 = 0.09;
    Number FEE_4 = 0.05;
    Number RANGE_1_COMPONENT = MoneyHelper.mul(inputNum, FEE_1);
    Number RANGE_2_COMPONENT = MoneyHelper.mul(RANGE_1, FEE_1);
    ;
    Number RANGE_3_COMPONENT = MoneyHelper.mul(MoneyHelper.sub(RANGE_2, RANGE_1), FEE_2);
    Number RANGE_4_COMPONENT = MoneyHelper.mul(MoneyHelper.sub(RANGE_3, RANGE_2), FEE_3);
    if (inputNum.doubleValue() <= RANGE_1.doubleValue()) {
        // RANGE_1_COMPONENT
        ownerFee = Money.of(RANGE_1_COMPONENT, DEFAULT_CURRENCY_TYPE);
        println("range 1 ");
    }
    if (inputNum.doubleValue() > RANGE_1.doubleValue() && inputNum.doubleValue() <= RANGE_2.doubleValue()) {
        // RANGE_2_COMPONENT + (input - RANGE_1) * FEE_2
        println(input);
        Money subtract = MoneyHelper.sub(input, RANGE_1);
        println(subtract);
        Money multiply = MoneyHelper.mul(subtract, FEE_2);
        println(multiply);
        ownerFee = MoneyHelper.add(multiply, RANGE_2_COMPONENT);
        println(ownerFee);
        println("range 2 ");
    }
    if (inputNum.doubleValue() > RANGE_2.doubleValue() && inputNum.doubleValue() <= RANGE_3.doubleValue()) {
        // RANGE_2_COMPONENT + RANGE_3_COMPONENT + (input - RANGE_2) * FEE_3
        Number addition1 = MoneyHelper.add(RANGE_2_COMPONENT, RANGE_3_COMPONENT);
        Money subtract = MoneyHelper.sub(input, RANGE_2);
        Money multiply = MoneyHelper.mul(subtract, FEE_3);
        Money addition2 = MoneyHelper.add(multiply, addition1);
        ownerFee = addition2;
        println("range 3 ");
    }
    if (inputNum.doubleValue() > RANGE_3.doubleValue()) {
        // RANGE_2_COMPONENT + RANGE_3_COMPONENT + RANGE_4_COMPONENT + ( input - RANGE_3
        // ) * FEE_4
        Number addition1 = MoneyHelper.add(RANGE_2_COMPONENT, RANGE_3_COMPONENT);
        Number addition2 = MoneyHelper.add(addition1, RANGE_4_COMPONENT);
        Money subtract = MoneyHelper.sub(input, RANGE_3);
        Money multiply = MoneyHelper.mul(subtract, FEE_4);
        Money addition3 = MoneyHelper.add(multiply, addition2);
        ownerFee = addition3;
        println("range 4 ");
    }
    /*
		 * To prevent exponential values from appearing in amount. Not 1.7E+2, We need
		 * 170
		 */
    ownerFee = MoneyHelper.round(ownerFee);
    ownerFee = Money.of(ownerFee.getNumber().doubleValue(), DEFAULT_CURRENCY_TYPE);
    Number amount = ownerFee.getNumber().doubleValue();
    Money fee = Money.of(amount.doubleValue(), DEFAULT_CURRENCY_TYPE);
    println("From QRules " + fee);
    return fee;
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit)

Example 2 with CurrencyUnit

use of javax.money.CurrencyUnit 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 3 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class IMFRateReadingHandler method read.

RateIMFResult read(InputStream inputStream) throws IOException, ParseException {
    Map<CurrencyUnit, List<ExchangeRate>> currencyToSdr = new HashMap<>();
    Map<CurrencyUnit, List<ExchangeRate>> sdrToCurrency = new HashMap<>();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line = reader.readLine();
    if (line.contains("Request Rejected")) {
        throw new IOException("Request has been rejected by IMF server.");
    }
    boolean isCurrencyToSdr = true;
    // SDRs per Currency unit (2)
    // 
    // Currency January 31, 2013 January 30, 2013 January 29, 2013
    // January 28, 2013 January 25, 2013
    // Euro 0.8791080000 0.8789170000 0.8742470000 0.8752180000
    // 0.8768020000
    // Currency units per SDR(3)
    // 
    // Currency January 31, 2013 January 30, 2013 January 29, 2013
    // January 28, 2013 January 25, 2013
    // Euro 1.137520 1.137760 1.143840 1.142570 1.140510
    List<LocalDate> timestamps = null;
    while (Objects.nonNull(line)) {
        if (line.trim().isEmpty()) {
            line = reader.readLine();
            continue;
        }
        if (line.startsWith("SDRs per Currency unit")) {
            isCurrencyToSdr = false;
            line = reader.readLine();
            continue;
        } else if (line.startsWith("Currency units per SDR")) {
            isCurrencyToSdr = true;
            line = reader.readLine();
            continue;
        } else if (line.startsWith("Currency")) {
            timestamps = readTimestamps(line);
            line = reader.readLine();
            continue;
        }
        String[] parts = line.split("\\t");
        CurrencyUnit currency = currenciresByName.get(parts[0]);
        if (Objects.isNull(currency)) {
            LOG.finest(() -> "Uninterpretable data from IMF data feed: " + parts[0]);
            line = reader.readLine();
            continue;
        }
        saveExchangeRate(currencyToSdr, sdrToCurrency, isCurrencyToSdr, timestamps, currency, parseValues(parts));
        line = reader.readLine();
    }
    // Cast is save, since contained DefaultExchangeRate is Comparable!
    sortResult(currencyToSdr, sdrToCurrency);
    return new RateIMFResult(currencyToSdr, sdrToCurrency);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) IOException(java.io.IOException) LocalDate(java.time.LocalDate) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ConversionAggregatorTest method groupBySummarizingMonetaryTest.

@Test
public void groupBySummarizingMonetaryTest() {
    GroupMonetarySummaryStatistics group = currenciesToSummary().collect(groupBySummarizingMonetary());
    Map<CurrencyUnit, MonetarySummaryStatistics> mapSummary = group.get();
    assertEquals(mapSummary.keySet().size(), 3);
}
Also used : MonetarySummaryStatistics(org.javamoney.moneta.function.MonetarySummaryStatistics) GroupMonetarySummaryStatistics(org.javamoney.moneta.function.GroupMonetarySummaryStatistics) CurrencyUnit(javax.money.CurrencyUnit) GroupMonetarySummaryStatistics(org.javamoney.moneta.function.GroupMonetarySummaryStatistics) Test(org.testng.annotations.Test)

Example 5 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ConversionOperatorsTest method shouldExchangeCurrencyNegativeValue.

@Test
public void shouldExchangeCurrencyNegativeValue() {
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount money = Money.parse("BHD -1.345");
    MonetaryAmount result = ConversionOperators.exchange(real).apply(money);
    assertNotNull(result);
    assertEquals(result.getCurrency(), real);
    assertEquals(Double.valueOf(-1.345), result.getNumber().doubleValue());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Aggregations

CurrencyUnit (javax.money.CurrencyUnit)116 Test (org.testng.annotations.Test)78 MonetaryAmount (javax.money.MonetaryAmount)57 BigDecimal (java.math.BigDecimal)14 Currency (java.util.Currency)8 MonetaryException (javax.money.MonetaryException)6 ExchangeRate (javax.money.convert.ExchangeRate)6 RoundedMoney (org.javamoney.moneta.RoundedMoney)6 IOException (java.io.IOException)5 MathContext (java.math.MathContext)5 MonetaryParseException (javax.money.format.MonetaryParseException)5 Money (org.javamoney.moneta.Money)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)3 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2