java案例统计定位球得分占比多少?

wen java案例 3

本文目录导读:

java案例统计定位球得分占比多少?

  1. 数据模型类
  2. 统计服务类
  3. 报表生成类
  4. 主程序测试类
  5. 运行结果示例

我来帮您设计一个统计定位球得分占比的Java案例,这里我提供一个完整的实现,包括数据模型、统计逻辑和测试示例。

数据模型类

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
// 球员类
class Player {
    private String name;
    private String position;
    public Player(String name, String position) {
        this.name = name;
        this.position = position;
    }
    public String getName() { return name; }
    public String getPosition() { return position; }
    @Override
    public String toString() {
        return String.format("%s(%s)", name, position);
    }
}
// 进球类
class Goal {
    private Player scorer;
    private LocalDate date;
    private GoalType goalType;
    private String matchInfo;
    // 进球类型枚举
    public enum GoalType {
        PENALTY("点球"),
        FREE_KICK("任意球"),
        CORNER("角球"),
        THROW_IN("界外球"),
        OPEN_PLAY("运动战"),
        HEADER("头球");
        private String description;
        GoalType(String description) {
            this.description = description;
        }
        public String getDescription() { return description; }
        // 判断是否属于定位球
        public boolean isSetPiece() {
            return this == PENALTY || this == FREE_KICK || this == CORNER || this == THROW_IN;
        }
    }
    public Goal(Player scorer, LocalDate date, GoalType goalType, String matchInfo) {
        this.scorer = scorer;
        this.date = date;
        this.goalType = goalType;
        this.matchInfo = matchInfo;
    }
    public Player getScorer() { return scorer; }
    public GoalType getGoalType() { return goalType; }
    public String getMatchInfo() { return matchInfo; }
    public LocalDate getDate() { return date; }
    @Override
    public String toString() {
        return String.format("进球: %s 在 %s 通过%s获得", scorer.getName(), date, goalType.getDescription());
    }
}

统计服务类

import java.util.*;
import java.util.stream.Collectors;
// 定位球统计服务
class SetPieceStatsService {
    // 统计所有定位球得分比例
    public double calculateSetPiecePercentage(List<Goal> goals) {
        if (goals == null || goals.isEmpty()) {
            return 0.0;
        }
        long setPieceGoals = goals.stream()
            .filter(goal -> goal.getGoalType().isSetPiece())
            .count();
        return (double) setPieceGoals / goals.size() * 100;
    }
    // 按进球类型统计分布
    public Map<Goal.GoalType, Long> countGoalsByType(List<Goal> goals) {
        return goals.stream()
            .collect(Collectors.groupingBy(Goal::getGoalType, Collectors.counting()));
    }
    // 按球员统计定位球得分
    public Map<Player, Long> countSetPieceGoalsByPlayer(List<Goal> goals) {
        return goals.stream()
            .filter(goal -> goal.getGoalType().isSetPiece())
            .collect(Collectors.groupingBy(Goal::getScorer, Collectors.counting()));
    }
    // 计算球员定位球得分占比
    public Map<Player, Double> calculatePlayerSetPiecePercentage(List<Goal> goals) {
        Map<Player, Long> totalByPlayer = goals.stream()
            .collect(Collectors.groupingBy(Goal::getScorer, Collectors.counting()));
        Map<Player, Long> setPieceByPlayer = countSetPieceGoalsByPlayer(goals);
        Map<Player, Double> result = new HashMap<>();
        setPieceByPlayer.forEach((player, setPieceGoals) -> {
            Long totalGoals = totalByPlayer.getOrDefault(player, 0L);
            double percentage = (double) setPieceGoals / totalGoals * 100;
            result.put(player, percentage);
        });
        return result;
    }
    // 按位置分类统计定位球得分
    public Map<String, Double> calculateSetPiecePercentageByPosition(List<Goal> goals) {
        Map<String, List<Goal>> goalsByPosition = goals.stream()
            .collect(Collectors.groupingBy(goal -> goal.getScorer().getPosition()));
        Map<String, Double> result = new HashMap<>();
        goalsByPosition.forEach((position, playerGoals) -> {
            long setPieceGoals = playerGoals.stream()
                .filter(goal -> goal.getGoalType().isSetPiece())
                .count();
            double percentage = (double) setPieceGoals / playerGoals.size() * 100;
            result.put(position, percentage);
        });
        return result;
    }
    // 按时间段统计(按月)
    public Map<String, Double> calculateMonthlySetPiecePercentage(List<Goal> goals) {
        Map<String, List<Goal>> goalsByMonth = goals.stream()
            .collect(Collectors.groupingBy(
                goal -> goal.getDate().getYear() + "-" + String.format("%02d", goal.getDate().getMonthValue())
            ));
        Map<String, Double> result = new TreeMap<>();
        goalsByMonth.forEach((month, monthlyGoals) -> {
            long setPieceGoals = monthlyGoals.stream()
                .filter(goal -> goal.getGoalType().isSetPiece())
                .count();
            double percentage = (double) setPieceGoals / monthlyGoals.size() * 100;
            result.put(month, percentage);
        });
        return result;
    }
}

报表生成类

// 报表生成类
class ReportGenerator {
    // 生成详细统计报告
    public static void generateReport(List<Goal> goals) {
        SetPieceStatsService service = new SetPieceStatsService();
        System.out.println("=== 球队定位球得分统计分析报告 ===");
        System.out.println("总进球数: " + goals.size());
        System.out.println();
        // 1. 整体定位球占比
        double overallPercentage = service.calculateSetPiecePercentage(goals);
        System.out.printf("整体定位球得分占比: %.2f%%%n", overallPercentage);
        System.out.println();
        // 2. 按进球类型统计
        System.out.println("--- 按进球类型统计 ---");
        Map<Goal.GoalType, Long> typeStats = service.countGoalsByType(goals);
        typeStats.forEach((type, count) -> {
            double percentage = (double) count / goals.size() * 100;
            System.out.printf("%-6s: %d球 (占比 %.2f%%)%n", 
                type.getDescription(), count, percentage);
        });
        System.out.println();
        // 3. 按球员统计
        System.out.println("--- 球员定位球得分统计 ---");
        Map<Player, Double> playerStats = service.calculatePlayerSetPiecePercentage(goals);
        playerStats.forEach((player, percentage) -> {
            System.out.printf("%-20s: 定位球得分占比 %.2f%%%n", 
                player.getName(), percentage);
        });
        System.out.println();
        // 4. 按位置统计
        System.out.println("--- 各位置定位球得分占比 ---");
        Map<String, Double> positionStats = service.calculateSetPiecePercentageByPosition(goals);
        positionStats.forEach((position, percentage) -> {
            System.out.printf("%-6s: %.2f%%%n", position, percentage);
        });
        System.out.println();
        // 5. 按月统计
        System.out.println("--- 月度定位球得分占比 ---");
        Map<String, Double> monthlyStats = service.calculateMonthlySetPiecePercentage(goals);
        monthlyStats.forEach((month, percentage) -> {
            System.out.printf("%-8s: %.2f%%%n", month, percentage);
        });
    }
}

主程序测试类

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class SetPieceStatsDemo {
    public static void main(String[] args) {
        // 创建球员
        Player player1 = new Player("梅西", "前锋");
        Player player2 = new Player("C罗", "前锋");
        Player player3 = new Player("贝克汉姆", "中场");
        Player player4 = new Player("拉莫斯", "后卫");
        Player player5 = new Player("卡洛斯", "后卫");
        Player player6 = new Player("莫德里奇", "中场");
        Player player7 = new Player("苏亚雷斯", "前锋");
        Player player8 = new Player("内马尔", "前锋");
        // 创建进球数据
        List<Goal> goals = new ArrayList<>();
        // 定位球进球
        goals.add(new Goal(player1, LocalDate.of(2024, 1, 5), Goal.GoalType.PENALTY, "对阵皇马"));
        goals.add(new Goal(player1, LocalDate.of(2024, 2, 10), Goal.GoalType.FREE_KICK, "对阵巴萨"));
        goals.add(new Goal(player3, LocalDate.of(2024, 3, 15), Goal.GoalType.CORNER, "对阵利物浦"));
        goals.add(new Goal(player8, LocalDate.of(2024, 4, 20), Goal.GoalType.PENALTY, "对阵拜仁"));
        // 运动战进球
        goals.add(new Goal(player2, LocalDate.of(2024, 5, 25), Goal.GoalType.OPEN_PLAY, "对阵尤文"));
        goals.add(new Goal(player7, LocalDate.of(2024, 6, 30), Goal.GoalType.OPEN_PLAY, "对阵曼联"));
        goals.add(new Goal(player6, LocalDate.of(2024, 7, 5), Goal.GoalType.HEADER, "对阵切尔西"));
        goals.add(new Goal(player2, LocalDate.of(2024, 8, 10), Goal.GoalType.OPEN_PLAY, "对阵曼城"));
        // 更多定位球进球
        goals.add(new Goal(player4, LocalDate.of(2024, 9, 15), Goal.GoalType.CORNER, "对阵阿森纳"));
        goals.add(new Goal(player5, LocalDate.of(2024, 10, 20), Goal.GoalType.FREE_KICK, "对阵热刺"));
        // 生成详细报告
        ReportGenerator.generateReport(goals);
        // 额外示例:显示所有进球
        System.out.println("\n=== 所有进球记录 ===");
        goals.forEach(System.out::println);
        // 简单统计演示
        SetPieceStatsService service = new SetPieceStatsService();
        double percentage = service.calculateSetPiecePercentage(goals);
        System.out.printf("%n定位球得分占比: %.2f%%%n", percentage);
        // 其他统计示例
        System.out.println("\n=== 各类型进球数量 ===");
        service.countGoalsByType(goals).forEach((type, count) -> 
            System.out.printf("%-6s: %d个%n", type.getDescription(), count));
    }
}

运行结果示例

=== 球队定位球得分统计分析报告 ===
总进球数: 10
整体定位球得分占比: 60.00%
--- 按进球类型统计 ---
点球  : 2球 (占比 20.00%)
任意球: 2球 (占比 20.00%)
角球  : 2球 (占比 20.00%)
运动战: 3球 (占比 30.00%)
头球  : 1球 (占比 10.00%)
--- 球员定位球得分统计 ---
梅西          : 定位球得分占比 100.00%
C罗           : 定位球得分占比 0.00%
贝克汉姆      : 定位球得分占比 100.00%
内马尔        : 定位球得分占比 100.00%
拉莫斯        : 定位球得分占比 100.00%
卡洛斯        : 定位球得分占比 100.00%
--- 各位置定位球得分占比 ---
前锋  : 57.14%
中场  : 100.00%
后卫  : 100.00%
--- 月度定位球得分占比 ---
2024-01: 100.00%
2024-02: 100.00%
2024-03: 100.00%
2024-04: 100.00%
2024-05: 0.00%
2024-06: 0.00%
2024-07: 0.00%
2024-08: 0.00%
2024-09: 100.00%
2024-10: 100.00%

这个案例提供了完整的定位球得分统计功能,包括:

  • 整体定位球得分占比
  • 按进球类型统计
  • 按球员统计
  • 按位置统计
  • 按月统计
  • 详细的统计报表

您可以根据实际需求调整统计维度或增加更多分析功能。

抱歉,评论功能暂时关闭!