Advertisement
valkata

Problem 4 – Football Stats

Oct 14th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 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.  
  7. public class footballStats {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.         HashMap<String, List<String>> stats = new HashMap<>();
  11.         HashMap<String, Long> totalScore = new HashMap<>();
  12.         String team1 = "";
  13.         String team2 = "";
  14.         String score1 = "";
  15.         String score2 = "";
  16.         String input = "";
  17.         while (!"Season End".equals(input = reader.readLine())) {
  18.             String[] teams = input.split(" - ");
  19.             team1 = teams[0];
  20.             String[] teamStats = teams[1].split("\\s+");
  21.             team2 = teamStats[0];
  22.             String[] scores = teamStats[2].split(":");
  23.             score1 = scores[0];
  24.             score2 = scores[1];
  25.  
  26.             stats.putIfAbsent(team1, new ArrayList<>());
  27.             stats.get(team1).add(team2 + " -> " + teamStats[2]);
  28.             totalScore.putIfAbsent(team1,0L);
  29.             totalScore.replace(team1,totalScore.get(team1)+Long.parseLong(score1));
  30.  
  31.             stats.putIfAbsent(team2, new ArrayList<>());
  32.             stats.get(team2).add(team1 + " -> " + score2+":"+score1);
  33.             totalScore.putIfAbsent(team2,0L);
  34.             totalScore.replace(team2,totalScore.get(team2)+Long.parseLong(score2));
  35.         }
  36.  
  37.         LinkedHashMap<String,Long> sorted = totalScore.entrySet().stream()
  38.                 .sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
  39.                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2) -> e1 ,LinkedHashMap::new));
  40.  
  41.         List<String> searchedTeams = Arrays.stream(reader.readLine().split(", ")).collect(Collectors.toList());
  42.  
  43.         for(Map.Entry<String,Long> team : sorted.entrySet()){
  44.             if(searchedTeams.contains(team.getKey())){
  45.                 stats.get(team.getKey()).stream()
  46.                         .sorted((t1,t2) -> t1.substring(0,t1.indexOf(" "))
  47.                         .compareTo(t2.substring(0,t2.indexOf(" "))))
  48.                         .forEach(t -> System.out.printf("%s - %s\n",team.getKey(),t));
  49.             }
  50.         }
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement