如何将POJO的属性绑定到内部ArrayList <another_pojo>

时间:2017-05-23 06:41:49

标签: java arraylist binding pojo

希望有人可以帮助我。我有两个POJOS,一个用于Invoice Header,另一个用于详细信息

public class Invoice{
private SimpleStringProperty docNum;
private SimpleStringProperty customer;
private ArrayList<InvoiceDetails> invoiceDetails;

public Invoice(String docNum, String customer) {
  this.docNum = new SimpleStringProperty(docNum);
  this.customer = new SimpleStringProperty(customer);
  this.invoiceDetails= new ArrayList<>();
}

/* Getters and setters*/
}

第二个是......

public class InvoiceDetails{
private SimpleStringProperty taxRate;
private SimpleDoubleProperty taxAmount;
private SimpleDoubleProperty amount;

public InvoiceDetails(String taxRate, Double taxAmount, Double amount) {
  this.taxRate= new SimpleStringProperty(taxRate);
  this.taxAmount= new SimpleDoubleProperty(taxAmount);
  this.amount= new SimpleDoubleProperty(amount);
}

/* Getters and setters*/
}

问题是我如何将POJO Invoices的字段绑定到POJO amount的字段InvoiceDetails的总和。像这样:

public class Invoice{
private SimpleStringProperty docNum;
private SimpleStringProperty customer;
private ArrayList<InvoiceDetails> invoiceDetails;
private SimpleDoubleProperty totalAmount;

public Invoice(String docNum, String customer) {
  this.docNum = new SimpleStringProperty(docNum);
  this.customer = new SimpleStringProperty(customer);
  this.invoiceDetails= new ArrayList<>();
  this.totalAmount.bind(....)
}

/* Getters and setters*/
}

这将是实现这一目标的更好方法。也许收集流中的数据并绑定到字段totalAmount? 提前感谢您的时间。

2 个答案:

答案 0 :(得分:1)

我认为你可以扩展一个List(示例显示ArrayList扩展名)并更改列表成员更改的字段。

public class DemoList {
    public static void main(String[] args) throws Exception {
        DetailList list = new DetailList();
        Invoice i = new Invoice(list);

        System.out.println(i.getTotalAmount());

        list.add(new Details(42));
        list.add(new Details(42));
        System.out.println(i.getTotalAmount());
        list.remove(0);
        System.out.println(i.getTotalAmount());
    }

}

class Invoice {
    final DetailList details;

    public Invoice(DetailList details) {
        this.details = details;
    }

    public int getTotalAmount() {
        return details.getTotalAmount();
    }
}
class Details {
    final int amount;

    public Details(int amount) {
        this.amount = amount;
    }
}
class DetailList extends ArrayList<Details> {
    int totalAmount=0;
    public DetailList() {
    }

    @Override
    public boolean add(Details t) {
        boolean res = super.add(t);
        recalculateTotal();
        return res;
    }

    @Override
    public void add(int index, Details element) {
        super.add(index, element);
        recalculateTotal();
    }

    @Override
    public Details remove(int index) {
        Details res = super.remove(index);
        recalculateTotal();
        return res;
    }

    @Override
    public boolean remove(Object o) {
        boolean res = super.remove(o);
        recalculateTotal();
        return res;
    }

    private void recalculateTotal() {
        totalAmount=0;
        this.stream().forEach(item -> {
            totalAmount+=item.amount;
        });
    }
    public int getTotalAmount() {
        return totalAmount;
    }
}

答案 1 :(得分:0)

谢谢大家的时间。我终于找到了另一个我认为更简单的解决方案。 我将SimpleDoubleProperty totalAmount绑定到DoubleBinding,这个是ArrayList<InvoiceDetails> invoiceDetails的流的结果。

    public class Invoice{
    private SimpleStringProperty docNum;
    private SimpleStringProperty customer;
    private ArrayList<InvoiceDetails> invoiceDetails;
    private SimpleDoubleProperty totalAmount;

    public Invoice(String docNum, String customer) {
       this.docNum = new SimpleStringProperty(docNum);
       this.customer = new SimpleStringProperty(customer);
       this.invoiceDetails= new ArrayList<>();
    }

    /* Getters and setters */
     public SimpleDoubleProperty totalAmountProperty() {
          if (this.totalAmount == null) {
            this.totalAmount = new SimpleDoubleProperty();
           }
      DoubleBinding ta = new DoubleBinding() {
         @Override
         protected double computeValue() {
            Double result = detalleIvaList.stream()
             .mapToDouble(DetalleIva::getBaseImponible).reduce(0.0, Double::sum);
            return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
         }
      };
      this.totalAmount.bind(ta);
      return this.totalAmount;
    }

    public Double getTotalAmount() {
      return this.totalAmountProperty().get();
    }

    public void setTotalAmount(Double totalAmount) {
      this.totalAmountProperty().set(totalAmount);
    }

再次感谢您的时间。 问候