2016-10-23 58 views
0

我创建了此JUnit测试以测试如何使用值BigDecimal来计算对象中的所有值。如何计算BigDecimal

import java.math.BigDecimal; 
import java.math.BigInteger; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.junit.Test; 

public class BigDecimalTest 
{ 
    private BigDecimal sumAmounts(List<ExpressCheckout> myList) 
    { 
     BigDecimal total = new BigDecimal(BigInteger.ZERO); 
     for (ExpressCheckout item : myList) 
     { 
      total.add(item.getAmount()); 
     } 
     return total; 
    } 

    @Test 
    public void testBigDecimalTest() 
    { 
     try 
     { 
      List<ExpressCheckout> list = new ArrayList<>(); 
      for (int i = 0; i < 12; i++) 
      { 
       ExpressCheckout obj = new ExpressCheckout(); 
       obj.setCurrency("USD"); 

       Random rand = new Random(); 

       int n = rand.nextInt(50) + 1; 
       obj.setAmount(new BigDecimal(n)); 
       obj.setQuantity(1); 
       obj.setName("test name"); 
       obj.setDescription("test description"); 
       list.add(obj); 
      } 

      BigDecimal sumAmounts = sumAmounts(list); 

      System.out.println(">>>>>>>>>>>>>>>>>>>> sumAmounts " + sumAmounts.toString()); 

     } 
     catch (Exception ex) 
     { 
      Logger.getLogger(BigDecimalTest.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public class ExpressCheckout 
    { 
     String currency; 
     BigDecimal amount; 
     int quantity; 
     String name; 
     String description; 

     public ExpressCheckout() 
     { 
     } 

     public ExpressCheckout(String currency, BigDecimal amount, int quantity, String name, String description) 
     { 
      this.currency = currency; 
      this.amount = amount; 
      this.quantity = quantity; 
      this.name = name; 
      this.description = description; 
     } 

     public String getCurrency() 
     { 
      return currency; 
     } 

     public void setCurrency(String currency) 
     { 
      this.currency = currency; 
     } 

     public BigDecimal getAmount() 
     { 
      return amount; 
     } 

     public void setAmount(BigDecimal amount) 
     { 
      this.amount = amount; 
     } 

     public int getQuantity() 
     { 
      return quantity; 
     } 

     public void setQuantity(int quantity) 
     { 
      this.quantity = quantity; 
     } 

     public String getName() 
     { 
      return name; 
     } 

     public void setName(String name) 
     { 
      this.name = name; 
     } 

     public String getDescription() 
     { 
      return description; 
     } 

     public void setDescription(String description) 
     { 
      this.description = description; 
     } 
    } 

} 

出于某种原因,我得到的结果sumAmounts 0任何想法,为什么我得到这样的结果,我该如何解决? 结果应该从Java列表中的所有BigDecimal值进行计算。

+0

而不是'BigDecimal的总=新的BigDecimal(BigInteger.ZERO) ;'写如下:'BigDecimal total = BigDecimal.ZERO;'...不需要每次都创建一个新的对象,一个零对象已经存在。 –

回答

3

在java中BigDecimal是不可改变的,因此您在sumAmounts方法如下声明:

total.add(item.getAmount()); 

需要改变到:

total = total.add(item.getAmount());