dinheiro em java joda money money api e al m
play

Dinheiro em Java: Joda-Money, Money API e alm Otvio Santana - PowerPoint PPT Presentation

Dinheiro em Java: Joda-Money, Money API e alm Otvio Santana @otaviojava otaviojava@java.net otaviojava@apache.org BigDecimal Double Integer Float Long Double e Float double val = 1.03 - .42; System.out.println(val); //0.610000000000


  1. Dinheiro em Java: Joda-Money, Money API e além Otávio Santana @otaviojava otaviojava@java.net otaviojava@apache.org

  2. BigDecimal Double Integer Float Long

  3. Double e Float double val = 1.03 - .42; System.out.println(val); //0.610000000000 0001

  4. Integer e Long Economia de memória Velocidade Chance de dar problemas é alta Não passar de 8 casas decimais

  5. public class Produto { private String nome; private int dinheiro; //getter and setter } Produto banana = new Produto("banana", 1_00); Produto macarrao = new Produto("macarrao", 1_00); int total = banana.getDinheiro() + macarrao.getDinheiro();

  6. BigDecimal ● Maior Controle ● Mais intuitivo

  7. BigDecimal bd1 = new BigDecimal(.01); //0.01000000000000000020816681... BigDecimal bd2 = new BigDecimal(.3); //0.29999999999999998889776975... BigDecimal total = bd1.add(bd2)); //30999999999999998910593...

  8. public class Produto { private String nome; private BigDecimal dinheiro; //getter and setter } Produto caneta = new Produto("caneta", BigDecimal.valueOf(10.21)); Produto lapis = new Produto("lapis", BigDecimal.valueOf(12.21)); BigDecimal total = caneta.getDinheiro().add(lapis.getDinheiro());

  9. Mais desafios para o Dinheiro ● Internacionalização ● Sair na tela

  10. public class Produto { private String nome; private String moeda; private BigDecimal dinheiro; //getter and setter }

  11. public class Produto { private String nome; private Moeda moeda; private BigDecimal dinheiro; //getter and setter } enum Moeda { REAL, DOLLAR, EURO; }

  12. public class Produto { private String nome; private Currency moeda; private BigDecimal dinheiro; //getter and setter } Currency dollar = Currency.getInstance(Locale.US); Currency real = Currency.getInstance("BRL");

  13. Produto celular = //instância; Produto notebook = //instância; BigDecimal valor = celular.getValor().add(notebook.getValor()); Produto celular = //instância; Produto notebook = //instância; if(celular.getMoeda().equals(notebook.getMoeda())) { BigDecimal valor = celular.getValor().add(notebook.getValor()); } //exception

  14. public class ProdutoUtils { public static BigDecimal somar(Produto pA, Produto pB) { if(pA.getMoeda().equals(pB.getMoeda())) { return pA.getValor().add(pB.getValor()); } return null; } } BigDecimal valor = ProdutoUtils.somar(celular, notebook);

  15. public class MoneyUtils { public static BigDecimal somar(Currency moedaA, BigDecimal valorA, Currency moedaB, BigDecimal valorB) { //... } public class AssinaturaUtils {} public class ServicoUtils {} ...

  16. public interface Relatorio { void printHeader(); void printBody(); void printFooter(); } Relatorio relatorio = //; relatorio.printHeader(); relatorio.printBody(); relatorio.printFooter();

  17. public interface Relatorio { void print(); } Relatorio relatorio = //; relatorio.print();

  18. public class Produto { private String nome; private Money money; //getter and setter } public class Money extends BigDecimal { private Currency money; }

  19. Money valorBanana = new Money("12.00", dollar); Money valorAbacaxi = new Money("12.00", dollar); Produto banana = new Produto("banana", valorBanana); Produto abacaxi = new Produto("abacaxi", valorAbacaxi); Money money = banana.getMoney().add(valorBanana);

  20. public class Mar { public Sal getSal(){ //...} public boolean temPeixe() { return true; } //... } public class Biscoito extends Mar { }

  21. public class ClassPai { public void bug(){ //... } //... } public class ClasseFilho extends ClassePai { }

  22. public class Money { private Currency currency; private BigDecimal value; } Produto banana = new Produto("banana", valorBanana); Produto abacaxi = new Produto("abacaxi", valorAbacaxi); Money money = banana.getMoney().add(abacaxi.getMoney());

  23. Joda-Time JSR 310 Joda-Money JSR 354

  24. Joda-Money CurrencyUnit usd = CurrencyUnit.of("USD"); Money m = Money.of(usd, 12.43d); Money money = Money.parse("USD 23.87"); Money result = money.plus(m);

  25. Joda-Money BigMoney bigMoney = money.toBigMoney(); bigMoney.isGreaterThan(b2); bigMoney.isLessThan(b2); bigMoney.isNegativeOrZero(); bigMoney.isPositiveOrZero(); bigMoney.plus(b2);

  26. Money-API

  27. CurrencyUnit currency = MonetaryCurrencies.getCurrency(Locale.US); MonetaryAmount money = Money.of(BigDecimal.valueOf(10), currency); MonetaryAmount fastMoney = FastMoney.of(BigDecimal.valueOf(10), currency);

  28. CurrencyUnit real = MonetaryCurrencies.getCurrency("BRL"); MonetaryAmount m1 = Money.of(BigDecimal.valueOf(12D), real); MonetaryAmount m2 = Money.of(BigDecimal.valueOf(10D), real); MonetaryAmount m3 = m1.add(m2); MonetaryAmount m4 = m1.subtract(m2); MonetaryAmount m5 = m1.divide(2L);

  29. m1. isLessThan (m2); m1. isLessThanOrEqualTo (m2); m1. isGreaterThanOrEqualTo (m2); m1. isNegative (); m1. isPositive (); m1. isZero ();

  30. CurrencyUnit dollar = MonetaryCurrencies.getCurrency(Locale.US); MonetaryAmount m = Money.of(120, dollar); NumberValue number = m.getNumber(); BigDecimal value = number.numberValue(BigDecimal.class);

  31. MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.US); String texto = format.format(m);//12,50 USD MonetaryAmount money = Money.parse("EUR 25.25"); MonetaryAmount m2 = Money.parse(texto, format);

  32. MonetaryAmount noMoney = Money.of(0, euro); MonetaryAmount m1 = Money.of(10, euro); MonetaryAmount m2 = Money.of(30, euro); MonetaryAmount m3 = Money.of(40, euro); List<MonetaryAmount> moneys = Arrays.asList(m1, m2, m3); MonetaryAmount sum = moneys.stream().reduce(MonetaryFunctions.sum()).orEl se(noMoney);

  33. List<MonetaryAmount> justDollar = moneys.stream() .filter((MonetaryFunctions.isCurrency(DOLLAR))) .collect(Collectors.toList()); List<MonetaryAmount> notEuro = moneys.stream().filter((MonetaryFunctions.isNotCu rrency(EURO))).collect(Collectors.toList());

  34. MonetaryFunctions.isCurrency(DOLLAR). and ( MonetaryFunctions.isBetween(min, max))); MonetaryFunctions.containsCurrencies(EURO, DOLLAR)). or (MonetaryFunctions.isGreaterTha n(money)));

  35. List<MonetaryAmount> orderCurrencyValue = moneys.stream().sorted(MonetaryFunctions.s ortCurrencyUnit(). thenComparing (Monetary Functions.sortNumber())).collect(Collectors.to List()); List<MonetaryAmount> orderHighValue = moneys.stream().sorted(MonetaryFunctions.s ortCurrencyUnit(). thenComparing ( MonetaryFunctions.sortCurrencyUnitDesc())) .collect(Collectors.toList());

  36. Map<CurrencyUnit, List<MonetaryAmount>> groupBy = moneys.stream().collect(MonetaryFunctions.g roupByCurrencyUnit()); List<MonetaryAmount> notEuro = moneys.stream().filter((MonetaryFunctions.is NotCurrency(EURO))).collect(Collectors.toLis t());

  37. MonetarySummaryStatistics summary = moneys.stream().collect(MonetaryFunctions.s ummarizingMonetary(DOLLAR)); GroupMonetarySummaryStatistics groupSummary = moneys.stream().collect(MonetaryFunctions.g roupBySummarizingMonetary());

  38. MonetarySummaryStatistics summary = ... MonetaryAmount min = summary.getMin(); MonetaryAmount max = summary.getMax(); MonetaryAmount avarage = summary.getAverage(); Long count = summary.getCount();

  39. MoneyApi GroupMonetarySummaryStatistics groupSummary =... Map<CurrencyUnit, MonetarySummaryStatistics> map = groupSummary.get(); MonetarySummaryStatistics summaryDollar = map.get(DOLLAR);

  40. CurrencyUnit real = MonetaryCurrencies.getCurrency("BRL"); CurrencyUnit dollar = MonetaryCurrencies.getCurrency(Locale.US); MonetaryAmount m1 = Money.of(BigDecimal.valueOf(12D), real); MonetaryAmount m2 = Money.of(BigDecimal.valueOf(10D), dollar); m1.add(m2); javax.money.MonetaryException: Currency mismatch: BRL/USD

  41. ExchangeRateProvider prov = MonetaryConversions .getExchangeRateProvider("ECB"); MonetaryAmount d2e = prov.getCurrencyConversion(euro).apply(mDollar);

  42. ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider ("IMF"); MonetaryAmount d2e = prov.getCurrencyConversion(euro).apply(mDollar );

  43. ExchangeRateProvider provider = MonetaryConversions .getExchangeRateProvider(ExchangeRateType. ECB); ExchangeRateType.ECB ExchangeRateType.IMF ExchangeRateType.ECB_HIST90 ExchangeRateType.ECB_HIST ExchangeRateType.IDENTITY

  44. ExchangeRateProvider provider = MonetaryConversions .getExchangeRateProvider(ExchangeRateType. ECB); moneys.stream().sorted(MonetaryFunctions.sort Valuable(provider)); moneys.stream().sorted(MonetaryFunctions.sort ValuableDesc(provider));

  45. ● 7 EUR ● 9 BRL ● 8 USD List<MonetaryAmount> orderCurrencyValue = moneys.stream().sorted(MonetaryFunctions. sortV aluable (provider).collect(Collectors.toList()); ● 9 BRL ● 8 USD ● 7 EUR ● Motivo: ● 1 USD = 2.4203 BRL ● 1 EUR = 4.4203 BRL ● 1 USD = 0.79 EUR

  46. MonetarySummaryStatistics summary = moneys.stream().collect( MonetaryFunctions. summarizingMonetary (real, provider)); summary.getCount(); summary.getMax(); summary.getMin(); summary.getAverage();

Recommend


More recommend