You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yilai/CustomerOrderAnalysisStrate...

42 lines
1.4 KiB

/**
* 客户订单分析策略,分析每个客户的订单数量和消费总额
*/
package com.orderprocessing;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CustomerOrderAnalysisStrategy implements OrderAnalysisStrategy {
@Override
public Map<String, Object> analyze(List<Order> orders) {
Map<String, Object> results = new HashMap<>();
Map<String, Integer> customerOrderCount = new HashMap<>();
Map<String, Double> customerSpending = new HashMap<>();
if (orders != null) {
for (Order order : orders) {
String customerName = order.getCustomerName();
// 统计每个客户的订单数量
customerOrderCount.put(customerName,
customerOrderCount.getOrDefault(customerName, 0) + 1);
// 统计每个客户的消费总额
customerSpending.put(customerName,
customerSpending.getOrDefault(customerName, 0.0) + order.getTotalAmount());
}
}
results.put("customerOrderCount", customerOrderCount);
results.put("customerSpending", customerSpending);
results.put("uniqueCustomerCount", customerOrderCount.size());
return results;
}
@Override
public String getStrategyName() {
return "客户订单分析";
}
}