我有 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/