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.
homework/AmountAnalysisStrategy.java

55 lines
1.8 KiB

import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AmountAnalysisStrategy implements OrderAnalysisStrategy {
@Override
public Map<String, Object> analyze(List<Order> orders) {
Map<String, Object> result = new HashMap<>();
if (orders == null || orders.isEmpty()) {
result.put("message", "没有订单数据可供分析");
return result;
}
double totalAmount = 0;
double maxAmount = Double.MIN_VALUE;
double minAmount = Double.MAX_VALUE;
double totalDiscount = 0;
for (Order order : orders) {
double finalAmount = order.getFinalAmount();
totalAmount += finalAmount;
totalDiscount += order.getDiscountAmount();
if (finalAmount > maxAmount) {
maxAmount = finalAmount;
}
if (finalAmount < minAmount) {
minAmount = finalAmount;
}
}
double averageAmount = totalAmount / orders.size();
result.put("totalOrders", orders.size());
result.put("totalAmount", totalAmount);
result.put("averageAmount", averageAmount);
result.put("maxAmount", maxAmount);
result.put("minAmount", minAmount);
result.put("totalDiscount", totalDiscount);
result.put("discountPercentage", (totalDiscount / (totalAmount + totalDiscount)) * 100);
return result;
}
@Override
public String getAnalysisName() {
return "金额统计分析";
}
@Override
public String getDescription() {
return "分析订单的金额分布情况,包括总金额、平均金额、最大/最小金额以及折扣统计";
}
}