Filip_Markoski

[NP] FootballTable

Dec 23rd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7.  
  8. /**
  9.  * Partial exam II 2016/2017
  10.  */
  11. public class FootballTableTest {
  12.     public static void main(String[] args) throws IOException {
  13.         FootballTable table = new FootballTable();
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.         reader.lines()
  16.                 .map(line -> line.split(";"))
  17.                 .forEach(parts -> table.addGame(parts[0], parts[1],
  18.                         Integer.parseInt(parts[2]),
  19.                         Integer.parseInt(parts[3])));
  20.         reader.close();
  21.         System.out.println("=== TABLE ===");
  22.         System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
  23.         table.printTable();
  24.     }
  25. }
  26.  
  27. class Team {
  28.     String name;
  29.  
  30.     int wins;
  31.     int loses;
  32.     int draws;
  33.     int played;
  34.  
  35.     int goalsScored;
  36.     /* Total number of goals that opponents have scored against the team */
  37.     int goalsTaken;
  38.  
  39.     public Team(String name) {
  40.         this.name = name;
  41.     }
  42.  
  43.     public String getName() {
  44.         return name;
  45.     }
  46.  
  47.     public int goalDifference() {
  48.         return goalsScored - goalsTaken;
  49.     }
  50.  
  51.     public int getPoints() {
  52.         return wins * 3 + draws;
  53.     }
  54.  
  55.     @Override
  56.     public String toString() {
  57.         return String.format("%-15s%5d%5d%5d%5d%5d",
  58.                 name, played, wins, draws, loses, getPoints());
  59.     }
  60. }
  61.  
  62. class FootballTable {
  63.  
  64.     /* Name -> Team */
  65.     Map<String, Team> teams;
  66.  
  67.     public static final Comparator<Team> TEAM_COMPARATOR =
  68.             Comparator.comparing(Team::getPoints)
  69.                     .thenComparing(Team::goalDifference).reversed()
  70.                     .thenComparing(Team::getName);
  71.  
  72.  
  73.     public FootballTable() {
  74.         this.teams = new HashMap<>();
  75.     }
  76.  
  77.     public void addGame(String homeTeam, String awayTeam, int homeGoals, int awayGoals) {
  78.         Team home = teams.computeIfAbsent(homeTeam, key -> new Team(homeTeam));
  79.         Team away = teams.computeIfAbsent(awayTeam, key -> new Team(awayTeam));
  80.  
  81.         home.goalsScored += homeGoals;
  82.         home.goalsTaken += awayGoals;
  83.  
  84.         away.goalsScored += awayGoals;
  85.         away.goalsTaken += homeGoals;
  86.  
  87.         home.played++;
  88.         away.played++;
  89.  
  90.         if (homeGoals > awayGoals) {
  91.             home.wins++;
  92.             away.loses++;
  93.         } else if (homeGoals < awayGoals) {
  94.             home.loses++;
  95.             away.wins++;
  96.         } else {
  97.             home.draws++;
  98.             away.draws++;
  99.         }
  100.     }
  101.  
  102.  
  103.     public void printTable() {
  104.         /* values() -
  105.         Returns a Collection view of the values contained in this map. */
  106.         List<Team> collected = teams.values().stream()
  107.                 .sorted(TEAM_COMPARATOR)
  108.                 .collect(Collectors.toList());
  109.         IntStream.range(0, collected.size())
  110.                 .forEach(i -> System.out.printf(
  111.                         "%2d. %s\n", i + 1, collected.get(i)
  112.                 ));
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment