Add order statistics calculation
Problem Context
Business needs daily statistics on orders. Calculate averages and totals for dashboard.
Intermediate
100 points
File Changes (1)
src/main/java/com/example/service/OrderStatisticsService.java
ADDED
@@ -0 +1 @@
| 1 | +package com.example.service; |
|
| 2 | + |
|
| 3 | +import org.springframework.stereotype.Service; |
|
| 4 | +import java.math.BigDecimal; |
|
| 5 | +import java.util.Comparator; |
|
| 6 | +import java.util.List; |
|
| 7 | + |
|
| 8 | +@Service |
|
| 9 | +public class OrderStatisticsService { |
|
| 10 | + |
|
| 11 | + private final OrderRepository orderRepository; |
|
| 12 | + |
|
| 13 | + public DailyStats calculateDailyStats(LocalDate date) { |
|
| 14 | + List<Order> orders = orderRepository.findByOrderDate(date); |
|
| 15 | + |
|
| 16 | + BigDecimal total = orders.stream() |
|
| 17 | + .map(Order::getAmount) |
|
| 18 | + .reduce(BigDecimal::add) |
|
| 19 | + .get(); |
|
| 20 | + |
|
| 21 | + BigDecimal average = total.divide(BigDecimal.valueOf(orders.size())); |
|
| 22 | + |
|
| 23 | + BigDecimal maxOrder = orders.stream() |
|
| 24 | + .map(Order::getAmount) |
|
| 25 | + .max(Comparator.naturalOrder()) |
|
| 26 | + .get(); |
|
| 27 | + |
|
| 28 | + return new DailyStats(orders.size(), total, average, maxOrder); |
|
| 29 | + } |
|
| 30 | +} |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases