2 回答
TA贡献1860条经验 获得超9个赞
您可以使用投注金额的总和来映射日期。然后过滤它们。
public List<String> findLimits(List<Bet> bets) { return bets.stream() .collect(Collectors.toMap( b -> b.getCustId() + LocalDate.ofInstant(b.getTimestamp(), ZoneOffset.UTC).toString(), Bet::getAmount, (o1, o2) -> o1 + o2)) .entrySet().stream() .filter(e -> e.getValue() > 100.0) .map(e -> e.getKey().substring(0, e.getKey().length() - LocalDate.EPOCH.toString().length())) .collect(Collectors.toList()); }
TA贡献1862条经验 获得超6个赞
首先,您可以按客户ID对数据进行分组,然后在24小时内分析总和。正如你所提到的,记录是按日期升序排序的,所以方法可以如下所示:findLimits
class Bet {
String ID;
LocalDateTime dateTime;
BigDecimal value;
}
public List<String> findLimits(List<Bet> bets) {
BigDecimal sumLimit = new BigDecimal(100);
Map<String, List<Bet>> map = new HashMap<String, List<Bet>>();
List<String> result = new ArrayList<String>();
for (Bet bet : bets) {
if (map.get(bet.ID) == null)
map.put(bet.ID, new ArrayList<Bet>());
map.get(bet.ID).add(bet);
}
for (String ID : map.keySet()) {
List<Bet> betListForCustomer = map.get(ID);
boolean customerExceededLimit = false;
for (int i = 0; i < betListForCustomer.size(); i++) {
LocalDateTime endOfPeriod = betListForCustomer.get(i).dateTime.plusDays(1); //calculating end of 24h period current data
BigDecimal sum = new BigDecimal(0);
for (int j = i; j < betListForCustomer.size() //move start period to next dateTime
&& endOfPeriod.isAfter(betListForCustomer.get(j).dateTime); j++) { //analyzing to the last date in 24h period or end data set
sum = sum.add(betListForCustomer.get(j).value);
}
if (sum.compareTo(sumLimit) >= 0) { //sum >= 100
customerExceededLimit = true;
break; //there is no need to analyze this customer, limit exceeded
}
}
if (customerExceededLimit) {
result.add(ID);
}
}
return result;
}
添加回答
举报