parent
aa93d01788
commit
8b56c28004
@ -0,0 +1,180 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ShapeStatisticsAnalyzer {
|
||||
private List<Shape> shapes;
|
||||
|
||||
public ShapeStatisticsAnalyzer(List<Shape> shapes) {
|
||||
this.shapes = shapes;
|
||||
}
|
||||
|
||||
// Count shapes by their type
|
||||
public Map<String, Integer> countShapesByType() {
|
||||
Map<String, Integer> typeCount = new HashMap<>();
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
String typeName = shape.getClass().getSimpleName();
|
||||
typeCount.put(typeName, typeCount.getOrDefault(typeName, 0) + 1);
|
||||
}
|
||||
|
||||
return typeCount;
|
||||
}
|
||||
|
||||
// Count shapes by their color
|
||||
public Map<String, Integer> countShapesByColor() {
|
||||
Map<String, Integer> colorCount = new HashMap<>();
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
String color = shape.getColor().toLowerCase();
|
||||
colorCount.put(color, colorCount.getOrDefault(color, 0) + 1);
|
||||
}
|
||||
|
||||
return colorCount;
|
||||
}
|
||||
|
||||
// Calculate the total area of all shapes
|
||||
public double calculateTotalArea() {
|
||||
double totalArea = 0;
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
totalArea += shape.calculateArea();
|
||||
}
|
||||
|
||||
return totalArea;
|
||||
}
|
||||
|
||||
// Calculate area percentage by shape type
|
||||
public Map<String, Double> calculateAreaPercentageByType() {
|
||||
Map<String, Double> typeArea = new HashMap<>();
|
||||
double totalArea = calculateTotalArea();
|
||||
|
||||
if (totalArea == 0) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
// First calculate total area for each type
|
||||
for (Shape shape : shapes) {
|
||||
String typeName = shape.getClass().getSimpleName();
|
||||
double currentArea = typeArea.getOrDefault(typeName, 0.0);
|
||||
typeArea.put(typeName, currentArea + shape.calculateArea());
|
||||
}
|
||||
|
||||
// Calculate percentages
|
||||
Map<String, Double> percentageMap = new HashMap<>();
|
||||
for (Map.Entry<String, Double> entry : typeArea.entrySet()) {
|
||||
percentageMap.put(entry.getKey(), (entry.getValue() / totalArea) * 100);
|
||||
}
|
||||
|
||||
return percentageMap;
|
||||
}
|
||||
|
||||
// Calculate area percentage by color
|
||||
public Map<String, Double> calculateAreaPercentageByColor() {
|
||||
Map<String, Double> colorArea = new HashMap<>();
|
||||
double totalArea = calculateTotalArea();
|
||||
|
||||
if (totalArea == 0) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
// First calculate total area for each color
|
||||
for (Shape shape : shapes) {
|
||||
String color = shape.getColor().toLowerCase();
|
||||
double currentArea = colorArea.getOrDefault(color, 0.0);
|
||||
colorArea.put(color, currentArea + shape.calculateArea());
|
||||
}
|
||||
|
||||
// Calculate percentages
|
||||
Map<String, Double> percentageMap = new HashMap<>();
|
||||
for (Map.Entry<String, Double> entry : colorArea.entrySet()) {
|
||||
percentageMap.put(entry.getKey(), (entry.getValue() / totalArea) * 100);
|
||||
}
|
||||
|
||||
return percentageMap;
|
||||
}
|
||||
|
||||
// Get the shape with the largest area
|
||||
public Shape getLargestShape() {
|
||||
if (shapes == null || shapes.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Shape largest = shapes.get(0);
|
||||
for (Shape shape : shapes) {
|
||||
if (shape.calculateArea() > largest.calculateArea()) {
|
||||
largest = shape;
|
||||
}
|
||||
}
|
||||
|
||||
return largest;
|
||||
}
|
||||
|
||||
// Get the shape with the smallest area
|
||||
public Shape getSmallestShape() {
|
||||
if (shapes == null || shapes.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Shape smallest = shapes.get(0);
|
||||
for (Shape shape : shapes) {
|
||||
if (shape.calculateArea() < smallest.calculateArea()) {
|
||||
smallest = shape;
|
||||
}
|
||||
}
|
||||
|
||||
return smallest;
|
||||
}
|
||||
|
||||
// Print all statistics
|
||||
public void printStatistics() {
|
||||
System.out.println("=== Shape Statistics ===");
|
||||
|
||||
// Print type statistics
|
||||
System.out.println("\nShape count by type:");
|
||||
for (Map.Entry<String, Integer> entry : countShapesByType().entrySet()) {
|
||||
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
|
||||
// Print color statistics
|
||||
System.out.println("\nShape count by color:");
|
||||
for (Map.Entry<String, Integer> entry : countShapesByColor().entrySet()) {
|
||||
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
|
||||
// Print total area
|
||||
System.out.println("\nTotal area: " + calculateTotalArea());
|
||||
|
||||
// Print area percentage by type
|
||||
System.out.println("\nArea percentage by type:");
|
||||
for (Map.Entry<String, Double> entry : calculateAreaPercentageByType().entrySet()) {
|
||||
System.out.printf(" %s: %.2f%%\n", entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// Print area percentage by color
|
||||
System.out.println("\nArea percentage by color:");
|
||||
for (Map.Entry<String, Double> entry : calculateAreaPercentageByColor().entrySet()) {
|
||||
System.out.printf(" %s: %.2f%%\n", entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// Print largest and smallest shapes
|
||||
Shape largest = getLargestShape();
|
||||
Shape smallest = getSmallestShape();
|
||||
|
||||
if (largest != null) {
|
||||
System.out.println("\nLargest shape:");
|
||||
System.out.printf(" Type: %s, Color: %s, Area: %.2f\n",
|
||||
largest.getClass().getSimpleName(),
|
||||
largest.getColor(),
|
||||
largest.calculateArea());
|
||||
}
|
||||
|
||||
if (smallest != null) {
|
||||
System.out.println("\nSmallest shape:");
|
||||
System.out.printf(" Type: %s, Color: %s, Area: %.2f\n",
|
||||
smallest.getClass().getSimpleName(),
|
||||
smallest.getColor(),
|
||||
smallest.calculateArea());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue