Untitled

mail@pastecode.io avatar
unknown
java
5 months ago
1.4 kB
3
Indexable
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OrderPromotionMatcher {

    public static Map<Order, Promotion> matchOrdersToPromotions(List<Order> orders, List<Promotion> promotions) {
        return orders.stream()
            .collect(Collectors.toMap(
                order -> order,
                order -> findMatchingPromotion(order, promotions)
            ));
    }

    private static Promotion findMatchingPromotion(Order order, List<Promotion> promotions) {
        return promotions.stream()
            .filter(promotion -> isOrderInPromotionPeriod(order, promotion))
            .findFirst()
            .orElse(null);
    }

    private static boolean isOrderInPromotionPeriod(Order order, Promotion promotion) {
        LocalDateTime paidAt = order.getPaidAt();
        return !paidAt.isBefore(promotion.getStartTime()) && paidAt.isBefore(promotion.getEndTime());
    }
}

// Assuming you have these classes defined:
class Order {
    private LocalDateTime paidAt;
    // other fields and methods
    public LocalDateTime getPaidAt() { return paidAt; }
}

class Promotion {
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    // other fields and methods
    public LocalDateTime getStartTime() { return startTime; }
    public LocalDateTime getEndTime() { return endTime; }
}
Leave a Comment