Skip to content

Instantly share code, notes, and snippets.

@wkdalsgh192
Last active September 8, 2021 02:02
Show Gist options
  • Save wkdalsgh192/5b1c7d51e02712f456a4cac99e9fe74b to your computer and use it in GitHub Desktop.
Save wkdalsgh192/5b1c7d51e02712f456a4cac99e9fe74b to your computer and use it in GitHub Desktop.
public class PayGroups {
private List<Pay> pays;
public PayGroups(List<Pay> pays) {
this.pays = pays;
}
public Long getPayPalSum() return getTotalSum(pay -> PayType.isPayPal(pay.getPayType()));
public Long getVisaSum() return getTatalSum(pay -> PayType.isVisa(pay.getPayType()));
public Long getTotalSum(Predicate<Pay> predicate) {
return pays.stream()
.filter(predicate)
.mapToLong(Pay::getAmount)
.sum();
}
}
public void managingStateAndAction() {
List<Pay> pays = Arrays.asList(
new Pay(PAYPAL, 1000),
new Pay(VISA, 2000),
new Pay(MASTER_CARD, 3000),
new Pay(UNION_CARD, 5000)
);
PayGroups payGroups = new PayGroups(pays);
Long visaExpenditure = payGroups.getVisaSum();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment