Advertisement
Guest User

P04_FootballStats

a guest
Jun 8th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class P04_FootballStats {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         // teamName -> List of games
  13.         Map<String, List<String>> games = new HashMap<>();
  14.  
  15.         //RealMadrid - Barcelona result 5:0
  16.         String line = reader.readLine();
  17.         Pattern pattern = Pattern
  18.                 .compile("([A-Za-z]+) - ([A-Za-z]+) result ([0-9]+):([0-9]+)");
  19.  
  20.         while (!"Season End".equals(line)){
  21.             Matcher matcher = pattern.matcher(line);
  22.  
  23.             matcher.find();
  24.             String homeTeam = matcher.group(1);
  25.             String guest = matcher.group(2);
  26.             String homeTeamResult = matcher.group(3);
  27.             String guestResult = matcher.group(4);
  28.  
  29.             if(!games.containsKey(homeTeam)){
  30.                 games.put(homeTeam, new ArrayList<>());
  31.             }
  32.  
  33.             if(!games.containsKey(guest)){
  34.                 games.put(guest, new ArrayList<>());
  35.             }
  36.  
  37.             String homeGame = " - " + guest + " -> " + homeTeamResult + ":" + guestResult;
  38.             String guestGame = " - " + homeTeam + " -> " + guestResult + ":" + homeTeamResult;
  39.  
  40.             games.get(homeTeam).add(homeGame);
  41.             games.get(guest).add(guestGame);
  42.  
  43.             line = reader.readLine();
  44.         }
  45.  
  46.         printResult(games, reader);
  47.     }
  48.  
  49.     private static void printResult(Map<String, List<String>> games, BufferedReader reader) throws IOException {
  50.         String[] teams = reader.readLine().split(", ");
  51.  
  52.         for (String team : teams) {
  53.             games.get(team)
  54.                     .stream()
  55.                     .sorted(Comparator.comparing(x -> x.split(" -> ")[0]))
  56.                     .forEach(game -> System.out.println(String.format("%s%s", team, game)));
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement