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;
}
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());
}
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);
}
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);
}
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());
}
Aggregations