@Test public void getTotalPrice_whenOneItemHasNoPrice_returnsExpectedValue() { Locale.setDefault(Locale.US); LineItem item1 = new LineItemBuilder("USD").setTotalPrice(1000L).build(); LineItem item2 = new LineItemBuilder("USD").build(); LineItem item3 = new LineItemBuilder("USD").setTotalPrice(3000L).build(); List<LineItem> items = new ArrayList<>(); items.add(item1); items.add(item2); items.add(item3); assertEquals(Long.valueOf(4000L), getTotalPrice(items, Currency.getInstance("USD"))); }
@Nullable static Long getTotalPrice(@NonNull Collection<LineItem> lineItems, @NonNull Currency currency) { Long totalPrice = null; for (LineItem lineItem : lineItems) { if (!currency.getCurrencyCode().equals(lineItem.getCurrencyCode())) { return null; } Long itemPrice = getPriceLong(lineItem.getTotalPrice(), currency); if (itemPrice != null) { if (totalPrice == null) { totalPrice = itemPrice; } else { totalPrice += itemPrice; } } } if (totalPrice == null) { return 0L; } return totalPrice; }
PaymentUtils { @Nullable static Long getTotalPrice(@NonNull Collection<LineItem> lineItems, @NonNull Currency currency) { Long totalPrice = null; for (LineItem lineItem : lineItems) { if (!currency.getCurrencyCode().equals(lineItem.getCurrencyCode())) { return null; } Long itemPrice = getPriceLong(lineItem.getTotalPrice(), currency); if (itemPrice != null) { if (totalPrice == null) { totalPrice = itemPrice; } else { totalPrice += itemPrice; } } } if (totalPrice == null) { return 0L; } return totalPrice; } }
PaymentUtils { @Nullable static Long getTotalPrice(@NonNull Collection<LineItem> lineItems, @NonNull Currency currency) { Long totalPrice = null; for (LineItem lineItem : lineItems) { if (!currency.getCurrencyCode().equals(lineItem.getCurrencyCode())) { return null; } Long itemPrice = getPriceLong(lineItem.getTotalPrice(), currency); if (itemPrice != null) { if (totalPrice == null) { totalPrice = itemPrice; } else { totalPrice += itemPrice; } } } if (totalPrice == null) { return 0L; } return totalPrice; }  }
PaymentUtils { @Nullable static Long getTotalPrice(@NonNull Collection<LineItem> lineItems, @NonNull Currency currency) { Long totalPrice = null; for (LineItem lineItem : lineItems) { if (!currency.getCurrencyCode().equals(lineItem.getCurrencyCode())) { return null; } Long itemPrice = getPriceLong(lineItem.getTotalPrice(), currency); if (itemPrice != null) { if (totalPrice == null) { totalPrice = itemPrice; } else { totalPrice += itemPrice; } } } if (totalPrice == null) { return 0L; } return totalPrice; }  static boolean matchesCurrencyPatternOrEmpty(@Nullable String priceString); static boolean matchesQuantityPatternOrEmpty(@Nullable String quantityString); @NonNull static List<CartError> validateLineItemList(
            List<LineItem> lineItems,
            @NonNull String currencyCode); static CartError searchLineItemForErrors(LineItem lineItem); @NonNull static String getPriceString(long price); @NonNull static List<CartError> removeErrorType(
            @NonNull List<CartError> errors,
            @NonNull @CartError.CartErrorType String errorType); @NonNull static String getPriceString(Long price, @NonNull Currency currency); static IsReadyToPayRequest getStripeIsReadyToPayRequest(); }
PaymentUtils { @Nullable static Long getTotalPrice(@NonNull Collection<LineItem> lineItems, @NonNull Currency currency) { Long totalPrice = null; for (LineItem lineItem : lineItems) { if (!currency.getCurrencyCode().equals(lineItem.getCurrencyCode())) { return null; } Long itemPrice = getPriceLong(lineItem.getTotalPrice(), currency); if (itemPrice != null) { if (totalPrice == null) { totalPrice = itemPrice; } else { totalPrice += itemPrice; } } } if (totalPrice == null) { return 0L; } return totalPrice; }  static boolean matchesCurrencyPatternOrEmpty(@Nullable String priceString); static boolean matchesQuantityPatternOrEmpty(@Nullable String quantityString); @NonNull static List<CartError> validateLineItemList(
            List<LineItem> lineItems,
            @NonNull String currencyCode); static CartError searchLineItemForErrors(LineItem lineItem); @NonNull static String getPriceString(long price); @NonNull static List<CartError> removeErrorType(
            @NonNull List<CartError> errors,
            @NonNull @CartError.CartErrorType String errorType); @NonNull static String getPriceString(Long price, @NonNull Currency currency); static IsReadyToPayRequest getStripeIsReadyToPayRequest();  }