Untitled
unknown
java
2 years ago
2.1 kB
6
Indexable
Never
import java.util.*; public class Program { enum GameResult { WIN, LOSE, TIE; public static GameResult otherTeamResult(GameResult result) { switch (result) { case WIN: return LOSE; case LOSE: return WIN; case TIE: return TIE; default: return null; } } } class Team { private int score = 0; private String name; public Team(String name) { this.name = name; } public void applyGameResult(GameResult result) { switch (result) { case WIN: score += 3; break; case LOSE: score -= 1; break; case TIE: score += 1; break; } } public int getScore() { return score; } public String getName() { return name; } } class Game { public String team1; public String team2; public GameResult team1Result; } public static void main(String[] args) { List<Game> games = new ArrayList(); // add all the game data to games HashMap<String, Team> teams = new HashMap(); // add all the teams to the hashmap for (Game game : games) { Team team1 = teams.get(game.team1); Team team2 = teams.get(game.team2); team1.applyGameResult(game.team1Result); team2.applyGameResult(GameResult.otherTeamResult(game.team1Result)); } Team[] leaderboard = teams.values().toArray(new Team[0]); Arrays.sort( leaderboard, (Team team1, Team team2) -> ((Integer)team1.getScore()).compareTo(team2.getScore()) ); // do whatever with your sorted leaderboard } }