Advertisement
dzocesrce

[NP] English Premier League

Apr 24th, 2025
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10. import java.util.Comparator;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.concurrent.atomic.AtomicInteger;
  14.  
  15.  class FootballTable {
  16.     Map<String, Team> teams;
  17.  
  18.     public FootballTable() {
  19.         this.teams = new HashMap<>();
  20.     }
  21.  
  22.     public void addGame(String hTeam, String aTeam, int hGoals, int aGoals) {
  23.         teams.putIfAbsent(hTeam, new Team(hTeam, 0, 0, 0, 0));
  24.         teams.putIfAbsent(aTeam, new Team(aTeam, 0, 0, 0, 0));
  25.         teams.computeIfPresent(hTeam,
  26.                 (k, v) -> {
  27.                     v.setWins(v.getWins() + (hGoals > aGoals ? 1 : 0));
  28.                     v.setDraws(v.getDraws() + (hGoals == aGoals ? 1 : 0));
  29.                     v.setLosses(v.getLosses() + (hGoals < aGoals ? 1 : 0));
  30.                     v.setGoalsDifference(v.getGoalsDifference() + hGoals - aGoals);
  31.                     return v;
  32.                 });
  33.         teams.computeIfPresent(aTeam,
  34.                 (k, v) -> {
  35.                     v.setWins(v.getWins() + (aGoals > hGoals ? 1 : 0));
  36.                     v.setDraws(v.getDraws() + (aGoals == hGoals ? 1 : 0));
  37.                     v.setLosses(v.getLosses() + (aGoals < hGoals ? 1 : 0));
  38.                     v.setGoalsDifference(v.getGoalsDifference() + aGoals - hGoals);
  39.                     return v;
  40.                 });
  41.     }
  42.  
  43.     public void printTable() {
  44.         AtomicInteger atomicInteger = new AtomicInteger(1);
  45.  
  46.         teams.values().stream().sorted().forEach(i-> System.out.printf("%2d. %s",atomicInteger.getAndIncrement(),i));
  47.  
  48.     }
  49. }
  50. class Team implements Comparable<Team>{
  51.     String name;
  52.     int wins;
  53.     int draws;
  54.     int losses;
  55.     int goalsDifference;
  56.  
  57.     public Team(String name, int wins, int draws, int losses, int goalsDifference) {
  58.         this.name= name;
  59.         this.wins = wins;
  60.         this.draws = draws;
  61.         this.losses = losses;
  62.         this.goalsDifference= goalsDifference;
  63.     }
  64.  
  65.     public void setGoalsDifference(int goalsDifference) {
  66.         this.goalsDifference = goalsDifference;
  67.     }
  68.  
  69.     public int getGoalsDifference() {
  70.         return goalsDifference;
  71.     }
  72.  
  73.     public void setWins(int wins) {
  74.         this.wins = wins;
  75.     }
  76.  
  77.     public void setDraws(int draws) {
  78.         this.draws = draws;
  79.     }
  80.  
  81.     public void setLosses(int losses) {
  82.         this.losses = losses;
  83.     }
  84.  
  85.     public int getWins() {
  86.         return wins;
  87.     }
  88.  
  89.     public int getDraws() {
  90.         return draws;
  91.     }
  92.  
  93.     public int getLosses() {
  94.         return losses;
  95.     }
  96.  
  97.     public int getPoints(){
  98.         return 3*wins+1*draws;
  99.     }
  100.  
  101.     public String getName() {
  102.         return name;
  103.     }
  104.  
  105.     public int getNumberOfMatches(){
  106.         return wins+draws+losses;
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return String.format("%-15s%5d%5d%5d%5d%5d\n",getName(),getNumberOfMatches(),getWins(),getDraws(),getLosses(),getPoints());
  112.     }
  113.  
  114.     @Override
  115.     public int compareTo(Team o) {
  116.         return Comparator.comparing(Team::getPoints)
  117.                 .thenComparing(Team::getGoalsDifference).reversed()
  118.                 .thenComparing(Team::getName)
  119.                 .compare(this,o);
  120.     }
  121. }
  122.  
  123. /**
  124.  * Partial exam II 2016/2017
  125.  */
  126. public class FootballTableTest {
  127.     public static void main(String[] args) throws IOException {
  128.         FootballTable table = new FootballTable();
  129.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  130.         reader.lines()
  131.                 .map(line -> line.split(";"))
  132.                 .forEach(parts -> table.addGame(parts[0], parts[1],
  133.                         Integer.parseInt(parts[2]),
  134.                         Integer.parseInt(parts[3])));
  135.         reader.close();
  136.         System.out.println("=== TABLE ===");
  137.         System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
  138.         table.printTable();
  139.     }
  140. }
  141.  
  142. // Your code here
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement