Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- /**
- * Partial exam II 2016/2017
- */
- public class FootballTableTest {
- public static void main(String[] args) throws IOException {
- FootballTable table = new FootballTable();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- reader.lines()
- .map(line -> line.split(";"))
- .forEach(parts -> table.addGame(parts[0], parts[1],
- Integer.parseInt(parts[2]),
- Integer.parseInt(parts[3])));
- reader.close();
- System.out.println("=== TABLE ===");
- System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
- table.printTable();
- }
- }
- class Team {
- String name;
- int wins;
- int loses;
- int draws;
- int played;
- int goalsScored;
- /* Total number of goals that opponents have scored against the team */
- int goalsTaken;
- public Team(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public int goalDifference() {
- return goalsScored - goalsTaken;
- }
- public int getPoints() {
- return wins * 3 + draws;
- }
- @Override
- public String toString() {
- return String.format("%-15s%5d%5d%5d%5d%5d",
- name, played, wins, draws, loses, getPoints());
- }
- }
- class FootballTable {
- /* Name -> Team */
- Map<String, Team> teams;
- public static final Comparator<Team> TEAM_COMPARATOR =
- Comparator.comparing(Team::getPoints)
- .thenComparing(Team::goalDifference).reversed()
- .thenComparing(Team::getName);
- public FootballTable() {
- this.teams = new HashMap<>();
- }
- public void addGame(String homeTeam, String awayTeam, int homeGoals, int awayGoals) {
- Team home = teams.computeIfAbsent(homeTeam, key -> new Team(homeTeam));
- Team away = teams.computeIfAbsent(awayTeam, key -> new Team(awayTeam));
- home.goalsScored += homeGoals;
- home.goalsTaken += awayGoals;
- away.goalsScored += awayGoals;
- away.goalsTaken += homeGoals;
- home.played++;
- away.played++;
- if (homeGoals > awayGoals) {
- home.wins++;
- away.loses++;
- } else if (homeGoals < awayGoals) {
- home.loses++;
- away.wins++;
- } else {
- home.draws++;
- away.draws++;
- }
- }
- public void printTable() {
- /* values() -
- Returns a Collection view of the values contained in this map. */
- List<Team> collected = teams.values().stream()
- .sorted(TEAM_COMPARATOR)
- .collect(Collectors.toList());
- IntStream.range(0, collected.size())
- .forEach(i -> System.out.printf(
- "%2d. %s\n", i + 1, collected.get(i)
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment