Java 如何将POJO的属性绑定到内部ArrayList的字段总和<;另一个_POJO>;
希望有人能帮助我。我需要两个POJO,一个用于发票标题,另一个用于详细信息Java 如何将POJO的属性绑定到内部ArrayList的字段总和<;另一个_POJO>;,java,arraylist,binding,pojo,Java,Arraylist,Binding,Pojo,希望有人能帮助我。我需要两个POJO,一个用于发票标题,另一个用于详细信息 public class Invoice{ private SimpleStringProperty docNum; private SimpleStringProperty customer; private ArrayList<InvoiceDetails> invoiceDetails; public Invoice(String docNum, String customer) { this.d
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*/
}
问题是如何将POJOInvoices
字段绑定到POJOinvoicesdetails
字段amount
的总和。大概是这样的:
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*/
}
公共类发票{
私有SimpleStringProperty文档;
私人物业客户;
私有ArrayList发票详细信息;
私有简单不动产总额;
公共发票(字符串docNum,字符串customer){
this.docNum=新的SimpleStringProperty(docNum);
this.customer=新的SimpleStringProperty(客户);
this.invoiceDetails=newArrayList();
此.totalAmount.bind(..)
}
/*接球手和接球手*/
}
这是实现这一目标的更好方法。也许收集流中的数据并绑定到字段totalAmount?
提前感谢您的时间。我认为您可以扩展列表(示例显示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;
}
}
公共类演示列表{
公共静态void main(字符串[]args)引发异常{
DetailList=新的DetailList();
发票i=新发票(列表);
System.out.println(i.getTotalAmount());
增加(新细节(42));
增加(新细节(42));
System.out.println(i.getTotalAmount());
列表。删除(0);
System.out.println(i.getTotalAmount());
}
}
类别发票{
最终明细表明细;
公共发票(明细表明细){
this.details=详细信息;
}
公共int getTotalAmount(){
返回详细信息。getTotalAmount();
}
}
课程详情{
最终整数金额;
公共详细信息(整数金额){
这个。金额=金额;
}
}
类DetailList扩展了ArrayList{
int totalAmount=0;
公共详细信息列表(){
}
@凌驾
公共布尔添加(详细信息t){
布尔res=super.add(t);
重新计算总和();
返回res;
}
@凌驾
公共void添加(int索引,Details元素){
super.add(索引、元素);
重新计算总和();
}
@凌驾
公共详细信息删除(int索引){
详细信息res=超级删除(索引);
重新计算总和();
返回res;
}
@凌驾
公共布尔删除(对象o){
布尔res=super.remove(o);
重新计算总和();
返回res;
}
private void RecomateTotal(){
总数=0;
this.stream().forEach(项->{
合计金额+=项目金额;
});
}
公共int getTotalAmount(){
返回总金额;
}
}
谢谢大家抽出时间。我终于找到了另一个我认为更简单的解决方案。
我将SimpleDoubleProperty totalAmount
绑定到一个DoubleBinding
,这是通过ArrayList 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);
}
公共类发票{
私有SimpleStringProperty文档;
私人物业客户;
私有ArrayList发票详细信息;
私有简单不动产总额;
公共发票(字符串docNum,字符串customer){
this.docNum=新的SimpleStringProperty(docNum);
this.customer=新的SimpleStringProperty(客户);
this.invoiceDetails=newArrayList();
}
/*接球手和接球手*/
公共SimpleDupleProperty totalAmountProperty(){
如果(this.totalAmount==null){
this.totalAmount=新的SimpleDoubleProperty();
}
DoubleBinding ta=新的DoubleBinding(){
@凌驾
受保护的双计算值(){
Double result=detalleIvaList.stream()
.mapToDouble(Detaleiva::GetBaseImpinible).reduce(0.0,Double::sum);
返回新的BigDecimal(结果).setScale(2,取整模式.HALF_UP).doubleValue();
}
};
此.totalAmount.bind(ta);
返回此.totalAmount;
}
公共双getTotalAmount(){
返回此.totalAmountProperty().get();
}
公共作废集合总金额(双倍总金额){
this.totalAmountProperty().set(totalAmount);
}
再次感谢您抽出时间。
关于
totalAmount
应该是每个InvoiceDetail
的每个amount
的总和?是的,让我们假设一张发票有一个包含3个InvoiceDetails的数组列表,其值为amount;1、2和3。父POJOInvoice
中的总金额应该是6。好主意,但我不会总是调用重新计算添加新的详细信息时,您只需执行totalAmount+=item.amount
操作,并且在删除元素时totalAmount-=item.amount
。您应该调用“重新计算”的唯一情况是在更改详细信息的金额时(这应该是可能的(不是最终的))。否则,它只会占用不需要的处理时间needed@XtremeBaumer我同意。计算差异更容易。示例是简化的。金额应该是最终的,或者我们应该添加相同的逻辑(通知更改以重新计算总额)是的,如果你更改了金额,你必须通知我。我认为在某些情况下,金额应该可以更改
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);
}