简体   繁体   English

如何将POJO的属性绑定到内部ArrayList的字段总和<ANOTHER_POJO>

[英]How to bind a property of a POJO to the sum of the field of an inner ArrayList<ANOTHER_POJO>

hope someone can help me. 希望可以有人帮帮我。 I have to two POJOS, one for the Invoice Header and another one for the details 我必须准备两个POJOS,一个用于发票标题,另一个用于详细信息

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*/
}

the second one is... 第二个是...

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*/
}

The question is how can I bind a field of the POJO Invoices , to the sum of the field amount of the POJO InvoiceDetails . 现在的问题是我怎么能绑定POJO的现场Invoices ,到外地的总和amount的POJO的InvoiceDetails Something like this: 像这样:

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*/
}

Which would be the better way to achive this. 这将是达到此目的的更好方法。 Maybe collecting the data in a stream and binding to the field totalAmount? 也许在流中收集数据并绑定到字段totalAmount? Thanks in advance for you time. 预先感谢您的时间。

I think you can just extend a List (the example shows ArrayList extension) and change the field on list members changes. 我认为您可以扩展一个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;
    }
}

Thank you all for your time. 谢谢大家的时间。 I finally found out another solution that I think is more simple. 我终于找到了另一个我认为更简单的解决方案。 I bind the SimpleDoubleProperty totalAmount to a DoubleBinding , and this one is the result of a stream through the ArrayList<InvoiceDetails> invoiceDetails . 我将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);
    }

Thanks again for your time. 感谢你的宝贵时间。 Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM