-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrderService.java
More file actions
123 lines (109 loc) · 4.16 KB
/
OrderService.java
File metadata and controls
123 lines (109 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.enterprise.demo;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
/**
* Order Service - Enterprise Java Module
*
* Exercise: Use GitHub Copilot inline suggestions to complete
* each method. Write a comment describing what you want,
* then let Copilot suggest the implementation.
*
* Tips:
* - Press Tab to accept a suggestion
* - Press ⌘→ (Mac) / Ctrl→ (Win) to accept word-by-word
* - Press ⌥] / Alt] to see alternative suggestions
* - Press Esc to dismiss
*/
public class OrderService {
public enum OrderStatus {
PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED
}
public record OrderItem(String productId, String productName, BigDecimal price, int quantity) {}
public record Order(
String orderId,
String customerId,
List<OrderItem> items,
OrderStatus status,
LocalDate createdAt
) {}
public record CustomerSummary(String customerId, BigDecimal totalSpent, int orderCount) {}
private final List<Order> orders;
public OrderService(List<Order> orders) {
this.orders = orders != null ? new ArrayList<>(orders) : new ArrayList<>();
}
/**
* Calculate the total amount for a single order
* by summing up item.price() * item.quantity() for all items.
* Apply a discount percentage if provided (e.g., 10 means 10% off).
* Return BigDecimal.ZERO if the order has no items or order is null.
* Use BigDecimal arithmetic with RoundingMode.HALF_UP and scale of 2.
*
* @param order the order to calculate
* @param discountPercent the discount percentage (0-100), 0 means no discount
* @return the total amount after discount
*/
public BigDecimal calculateOrderTotal(Order order, int discountPercent) {
// TODO: Use Copilot to complete this method
}
/**
* Filter orders by status.
* Throw IllegalArgumentException if status is null.
* Return an empty list if no orders match.
*
* @param status the status to filter by
* @return filtered list of orders
*/
public List<Order> filterOrdersByStatus(OrderStatus status) {
// TODO: Use Copilot to complete this method
}
/**
* Get orders within a date range (inclusive on both ends).
* Compare using order.createdAt().
* Validate that startDate is before or equal to endDate; throw
* IllegalArgumentException if not, or if either date is null.
* Return orders sorted by createdAt ascending.
*
* @param startDate start of the date range (inclusive)
* @param endDate end of the date range (inclusive)
* @return orders within the range, sorted ascending by date
*/
public List<Order> getOrdersByDateRange(LocalDate startDate, LocalDate endDate) {
// TODO: Use Copilot to complete this method
}
/**
* Calculate the average order value across all orders.
* Use calculateOrderTotal for each order with 0 discount.
* Return BigDecimal.ZERO if there are no orders.
* Round to 2 decimal places with RoundingMode.HALF_UP.
*
* @return the average order value
*/
public BigDecimal getAverageOrderValue() {
// TODO: Use Copilot to complete this method
}
/**
* Group orders by customer ID.
* Return a Map where keys are customerIds and values are lists of orders.
* Sort each customer's orders by createdAt descending (newest first).
*
* @return orders grouped by customer ID
*/
public Map<String, List<Order>> groupOrdersByCustomer() {
// TODO: Use Copilot to complete this method
}
/**
* Find the top N customers by total spending.
* Return a list of CustomerSummary records sorted by totalSpent descending.
* If n is greater than the number of customers, return all customers.
* Throw IllegalArgumentException if n is less than 1.
*
* @param n number of top customers to return
* @return list of top N customers by spending
*/
public List<CustomerSummary> getTopCustomers(int n) {
// TODO: Use Copilot to complete this method
}
}