package jinjieti; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 图形统计分析器(进阶题功能) */ public class ShapeStatisticsAnalyzer { /** * 统计每种图形的数量 */ public Map countShapeTypes(List shapes) { Map countMap = new HashMap<>(); if (shapes == null) return countMap; for (Shape shape : shapes) { String type = shape.getClass().getSimpleName(); countMap.put(type, countMap.getOrDefault(type, 0) + 1); } return countMap; } /** * 计算每种图形的面积占比 */ public Map calculateAreaRatio(List shapes) { Map areaMap = new HashMap<>(); double totalArea = 0.0; // 计算总面积和各类型面积 if (shapes != null) { for (Shape shape : shapes) { String type = shape.getClass().getSimpleName(); double area = shape.getArea(); areaMap.put(type, areaMap.getOrDefault(type, 0.0) + area); totalArea += area; } } // 计算占比(避免除零) if (totalArea > 0) { for (Map.Entry entry : areaMap.entrySet()) { areaMap.put(entry.getKey(), entry.getValue() / totalArea); } } return areaMap; } }