关于Java文件处理的问题

定义一个名为WorldCupGroup的类,用于实现所提供的Group接口。这个类将被用来确定哪些球队可以进入下一轮比赛,以及生成结果表。
WorldCupGroup类中的方法应该基于以下描述实现:
public Team getWinner() - 返回组中最先出现的团队的团队对象
public Team getRunnerUp() - 返回组中排名第二的团队的团队对象
public String getTable() - 返回包含组的当前表的字符串
getTable方法应该有如下格式的输出。
组名应该在第一行,列标题应该在第二行,团队及其统计数据应该在其余行。这里给出了一个输出示例:

img

有以下缩写:
W -表示球队获胜的场次
D -球队平局的场次
L -球队输掉的比赛场次
F -球队的进球数
A -对这支球队的进球数
P-代表球队的得分参赛队应该按照分数从大到小进行排序。
如果两队积分相等,那么他们应该以净胜球(F - A)来排序。最后,如果所有这些都相等,那么它们应该是基于它们的名称的顺序。这个类中的所有方法都依赖于以这种方式正确排列团队的能力。考虑您将使用什么方法将对象排序到正确的顺序。为了在测试系统中运行,您还必须添加具有以下签名的构造函数:
public WorldCupGroup (String groupName, Team a, Team b, Team c, Team d)

下面是一个使用Java语言实现示例代码和对应的注解,望采纳:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class WorldCupGroup implements Group {
    private final String groupName;
    private final List<Team> teams;

    public WorldCupGroup(String groupName, Team a, Team b, Team c, Team d) {
        this.groupName = groupName;
        this.teams = new ArrayList<>();
        this.teams.add(a);
        this.teams.add(b);
        this.teams.add(c);
        this.teams.add(d);

        // 按照分数、净胜球数、名称顺序排序
        Collections.sort(teams, (team1, team2) -> {
            if (team1.getPoints() != team2.getPoints()) {
                return team2.getPoints() - team1.getPoints();
            } else if (team1.getGoalDifference() != team2.getGoalDifference()) {
                return team2.getGoalDifference() - team1.getGoalDifference();
            } else {
                return team1.getName().compareTo(team2.getName());
            }
        });
    }

    @Override
    public Team getWinner() {
        return teams.get(0);
    }

    @Override
    public Team getRunnerUp() {
        return teams.get(1);
    }

    @Override
    public String getTable() {
        StringBuilder table = new StringBuilder();

        // 添加组名
        table.append(groupName + "\n");

        // 添加列标题
        table.append("Team W D L F A P\n");

        // 添加每个团队的信息
        for (Team team : teams) {
            table.append(team.getName() + " " + team.getWins() + " " + team.getDraws() + " " + team.getLoses() + " "
                    + team.getGoalsFor() + " " + team.getGoalsAgainst() + " " + team.getPoints() + "\n");
        }

        return table.toString();
    }
}

代码中构造函数在创建WorldCupGroup对象时使用传入的团队参数构建了一个团队列表,并使用Java的Collections.sort方法将列表按照上述规则进行排序。


在getWinner和getRunnerUp方法中,分别返回了排序后的团队列表中的第一个和第二个团队。


getTable方法则构建并返回了包含组内当前表格的字符串。在构建表格时,它会首先添加组名,然后添加列标题,最后添加每个团队的信息。

请结合你上个问题的类一起使用

interface Group {
    Team getWinner();

    Team getRunnerUp();

    String getTable();
}

public class WorldCupGroup implements Group {
    private String groupName;
    private Team a;
    private Team b;
    private Team c;
    private Team d;

    public WorldCupGroup(String groupName, Team a, Team b, Team c, Team d) {
        this.groupName = groupName;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    @Override
    public Team getWinner() {
        return getSortTeam()[3];
    }

    private Team[] getSortTeam() {
        final Team[] teams = {a, b, c, d};
        for (int i = 0; i < teams.length; i++) {
            for (int j = 0; j < teams.length - 1; j++) {
                if (teams[j].getPoints() > teams[j + 1].getPoints() ||
                        (teams[j].getPoints() == teams[j + 1].getPoints() && teams[j].getGoalsFor() - teams[j].getGoalsAgainst() > teams[j + 1].getGoalsFor() - teams[j + 1].getGoalsAgainst()) ||
                        (teams[j].getPoints() == teams[j + 1].getPoints() && teams[j].getGoalsFor() - teams[j].getGoalsAgainst() == teams[j + 1].getGoalsFor() - teams[j + 1].getGoalsAgainst() && teams[j].getName().compareTo(teams[j + 1].getName()) < 0)
                ) {
                    Team tmp;
                    tmp = teams[j];
                    teams[j] = teams[j + 1];
                    teams[j + 1] = tmp;
                }
            }
        }
        return teams;
    }

    @Override
    public Team getRunnerUp() {
        return getSortTeam()[2];
    }

    @Override
    public String getTable() {
        String result = this.groupName + ":" + "\n";
        result += String.format("%-20s %-3s %-3s %-3s %-3s %-3s %-3s\n", "Team", "W", "D", "L", "F", "A", "P");
        final Team[] sortTeam = getSortTeam();
        for (int i = 3; i >= 0; i--) {
            final Team team = sortTeam[i];
            result += String.format("%-20s %-3s %-3s %-3s %-3s %-3s %-3s\n", team.getName(), team.getWins(), team.getDraws(), team.getLosses(), team.getGoalsFor(), team.getGoalsAgainst(), team.getPoints());
        }
        return result;
    }

    public static void main(String[] args) {
        final FootballTeam netherlands = new FootballTeam("Netherlands");
        netherlands.addGame(new Game(5, 2));
        final WorldCupGroup worldCupGroup = new WorldCupGroup("Group A:",
                netherlands,
                new FootballTeam("Senegal"),
                new FootballTeam("Ecuador"),
                new FootballTeam("Qatar"));
        System.out.println(worldCupGroup.getTable());
    }
}