java - 如何获得 for 循环中所有项目的总金额?

标签 java android loops data-structures

我有 6 个订单。我循环了 6 个订单以获得每个订单的总价。

What I want to do is to add every order's total price on the loop, and then compare it to the authorized amount.

假设,授权金额为 2000

What happens is it compares the authorized amount to the PER ORDER TOTAL PRICE, instead of the ALL ORDERS TOTAL PRICE.

这里是一个示例代码:

for (Entity checkout : checkouts) {
                List<Entity> selectedJOs = AppUtil.getEntities(AppUtil.getListIds(checkout, "job-orders"), jobOrders);
                long finalGrandTotalAmount = 0;

                for (Entity job_order : selectedJOs) {

                    grandTotalAmount = 0;
                    long totalJobOrderPurchasedAmount = 0;
                    long totalJobOrderDeliveryFee = 0;
                    long totalJobOrderShoppingFee = 0;
                    long totalJobOrderDiscount = 0;
                    totalJobOrderRecalled = 0;
                    long totalCampaignDiscount = 0;

                    if (!job_order.attributes.status.equals("cancelled")) {
                        totalJobOrderPurchasedAmount += (job_order.attributes.purchased_deliverables_amount_in_cents + job_order.attributes.non_membership_fee_in_cents);
                        totalJobOrderDeliveryFee += job_order.attributes.delivery_fee_in_cents;
                        totalJobOrderShoppingFee += job_order.attributes.shopping_fee_in_cents;
                        totalJobOrderRecalled += job_order.attributes.recalled_deliverables_amount_in_cents;

                        List<Entity> selectedCampaigns = AppUtil.getEntities(AppUtil.getListIds(job_order, "job-order-fmcg-campaign-vouchers"), campaignDiscounts);


                        for (Entity campaignDiscount : selectedCampaigns) {
                            totalCampaignDiscount += campaignDiscount.attributes.discount_in_cents;

                        }
                        for (Entity discount : discounts) {
                            long job_order_discount;

                            if (discount.relationships.job_order.data.id.equals(job_order.id)) {
                                long purchase_amount = job_order.attributes.purchased_deliverables_amount_in_cents;
                                if (discount.attributes.sum_in_cents_off <= 0) {
                                    job_order_discount = Math.round(purchase_amount * discount.attributes.percent_off);
                                } else {
                                    job_order_discount = discount.attributes.sum_in_cents_off;
                                }

                                totalJobOrderDiscount += job_order_discount;
                            }
                        }


                        long discounts_total = totalJobOrderDiscount > 0 ? totalJobOrderDiscount + totalCampaignDiscount : totalCampaignDiscount;
                        grandTotalAmount = (totalJobOrderPurchasedAmount - discounts_total) + (totalJobOrderShoppingFee + totalJobOrderDeliveryFee) - job_order.attributes.credit_amount_of_cents;

                        boolean isLoginPrimary = true;

                        List<Entity> selected_consignments = AppUtil.getEntities(AppUtil.getListIds(job_order, "consignments"), consignments);

                        for (Entity consignment : selected_consignments) {
                            if (consignment.attributes.primary) {
                                Entity user = AppUtil.getEntity(consignment.relationships.user.data.id, users);
                                isLoginPrimary = user.relationships.profile.data.type.equals("runner-profiles") && user.id.equals(Preferences.getString(Prefkey.session_user_id)) && isLoginPrimary;
                            }
                        }

                        if (job_order.relationships.completed_by_user.data != null) {
                            if (job_order.relationships.completed_by_user.data.id.equals(Preferences.getString(Prefkey.session_user_id))) {
                                if (checkout.attributes.payment_mode.equals("credit-card")) {
                                    if (checkout.attributes.authorized_amount_in_cents < grandTotalAmount) {
                                        grandTotalAmountCc += checkout.attributes.authorized_amount_in_cents - totalJobOrderRecalled;
                                        grandTotalAmountCash += checkout.attributes.paid_cash_difference_in_cents;
                                    } else {
                                        grandTotalAmountCc += grandTotalAmount - totalJobOrderRecalled;
                                        grandTotalAmountCash += 0;
                                    }
                                } else {
                                    grandTotalAmountCash += grandTotalAmount - totalJobOrderRecalled;
                                }
                            }
                        }
                    }
                }

            }

最佳答案

对于每个循环,您都会重置 grandTotalAmount。通过使其全局化,将解决您的问题。

关于java - 如何获得 for 循环中所有项目的总金额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58869754/

相关文章:

java - Android 库 card.io 在 Moto X 2nd Gen 上跳过摄像头且不扫描卡

r - 如何自动填充空白栏

java - Android 日历月日

android - 有没有办法自动触发通知以匹配当前日期?

php - 悬停时提交按钮不起作用

java - 将 ACM 图形窗口转换为图像文件

java - 如何解析 JSON 输入流

java - 判断一个数字是否是 Java 中的输入?

android - 更改 AlertDialog 中“确定”按钮的颜色

android - 如何修改芯片的样式?