Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import org.junit.Test;
  9.  
  10. public class BigDecimalTest
  11. {
  12. private BigDecimal sumAmounts(List<ExpressCheckout> myList)
  13. {
  14. BigDecimal total = new BigDecimal(BigInteger.ZERO);
  15. for (ExpressCheckout item : myList)
  16. {
  17. total.add(item.getAmount());
  18. }
  19. return total;
  20. }
  21.  
  22. @Test
  23. public void testBigDecimalTest()
  24. {
  25. try
  26. {
  27. List<ExpressCheckout> list = new ArrayList<>();
  28. for (int i = 0; i < 12; i++)
  29. {
  30. ExpressCheckout obj = new ExpressCheckout();
  31. obj.setCurrency("USD");
  32.  
  33. Random rand = new Random();
  34.  
  35. int n = rand.nextInt(50) + 1;
  36. obj.setAmount(new BigDecimal(n));
  37. obj.setQuantity(1);
  38. obj.setName("test name");
  39. obj.setDescription("test description");
  40. list.add(obj);
  41. }
  42.  
  43. BigDecimal sumAmounts = sumAmounts(list);
  44.  
  45. System.out.println(">>>>>>>>>>>>>>>>>>>> sumAmounts " + sumAmounts.toString());
  46.  
  47. }
  48. catch (Exception ex)
  49. {
  50. Logger.getLogger(BigDecimalTest.class.getName()).log(Level.SEVERE, null, ex);
  51. }
  52. }
  53.  
  54. public class ExpressCheckout
  55. {
  56. String currency;
  57. BigDecimal amount;
  58. int quantity;
  59. String name;
  60. String description;
  61.  
  62. public ExpressCheckout()
  63. {
  64. }
  65.  
  66. public ExpressCheckout(String currency, BigDecimal amount, int quantity, String name, String description)
  67. {
  68. this.currency = currency;
  69. this.amount = amount;
  70. this.quantity = quantity;
  71. this.name = name;
  72. this.description = description;
  73. }
  74.  
  75. public String getCurrency()
  76. {
  77. return currency;
  78. }
  79.  
  80. public void setCurrency(String currency)
  81. {
  82. this.currency = currency;
  83. }
  84.  
  85. public BigDecimal getAmount()
  86. {
  87. return amount;
  88. }
  89.  
  90. public void setAmount(BigDecimal amount)
  91. {
  92. this.amount = amount;
  93. }
  94.  
  95. public int getQuantity()
  96. {
  97. return quantity;
  98. }
  99.  
  100. public void setQuantity(int quantity)
  101. {
  102. this.quantity = quantity;
  103. }
  104.  
  105. public String getName()
  106. {
  107. return name;
  108. }
  109.  
  110. public void setName(String name)
  111. {
  112. this.name = name;
  113. }
  114.  
  115. public String getDescription()
  116. {
  117. return description;
  118. }
  119.  
  120. public void setDescription(String description)
  121. {
  122. this.description = description;
  123. }
  124. }
  125.  
  126. }
  127.  
  128. total.add(item.getAmount());
  129.  
  130. total = total.add(item.getAmount());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement