kalinikov

03. Football League

Dec 3rd, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class FootballLeague {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String key = scanner.nextLine();
  12.  
  13. Map<String, Integer> teamsPoints = new TreeMap<>();
  14. Map<String, Integer> teamsGoals = new TreeMap<>();
  15.  
  16. String input = scanner.nextLine();
  17.  
  18. while (!input.equals("final")) {
  19.  
  20. String regex = "(.*)?" + Pattern.quote(key) + "(?<teamA>[A-Za-z]*)(.*?)" +
  21. Pattern.quote(key) + "(.*?)" + Pattern.quote(key) +
  22. "(?<teamB>[A-Za-z]*)" + Pattern.quote(key) + "(.*?)(?<scoreTeamA>\\d+):(?<scoreTeamB>\\d+)";
  23.  
  24. Pattern pattern = Pattern.compile(regex);
  25. Matcher matcher = pattern.matcher(input);
  26.  
  27. if (matcher.find()) {
  28. String teamAReversed = matcher.group("teamA");
  29. String teamBReversed = matcher.group("teamB");
  30. int scoreTeamA = Integer.parseInt(matcher.group("scoreTeamA"));
  31. int scoreTeamB = Integer.parseInt(matcher.group("scoreTeamB"));
  32.  
  33. int pointsTeamA = 0;
  34. int pointsTeamB = 0;
  35.  
  36. if (scoreTeamA > scoreTeamB) {
  37. pointsTeamA = 3;
  38. } else if (scoreTeamA == scoreTeamB) {
  39. pointsTeamA = 1;
  40. pointsTeamB = 1;
  41. } else {
  42. pointsTeamB = 3;
  43. }
  44.  
  45. String teamA = reverse(teamAReversed).toUpperCase();
  46. String teamB = reverse(teamBReversed).toUpperCase();
  47.  
  48. if (!teamsPoints.containsKey(teamA)) {
  49. teamsPoints.put(teamA, pointsTeamA);
  50. teamsGoals.put(teamA, scoreTeamA);
  51. } else {
  52. teamsPoints.put(teamA, teamsPoints.get(teamA) + pointsTeamA);
  53. teamsGoals.put(teamA, teamsGoals.get(teamA) + scoreTeamA);
  54. }
  55.  
  56. if (!teamsPoints.containsKey(teamB)) {
  57. teamsPoints.put(teamB, pointsTeamB);
  58. teamsGoals.put(teamB, scoreTeamB);
  59. } else {
  60. teamsPoints.put(teamB, teamsPoints.get(teamB) + pointsTeamB);
  61. teamsGoals.put(teamB, teamsGoals.get(teamB) + scoreTeamB);
  62. }
  63. }
  64.  
  65. input = scanner.nextLine();
  66. }
  67.  
  68. if (!teamsPoints.isEmpty()) {
  69. final int[] position = {1};
  70. System.out.println("League standings:");
  71. teamsPoints.entrySet()
  72. .stream()
  73. .sorted((f, s) -> {
  74. return s.getValue() - f.getValue();
  75. })
  76. .forEach(e -> {
  77. System.out.printf("%d. %s %d%n", position[0], e.getKey(), e.getValue());
  78. position[0]++;
  79. });
  80.  
  81. System.out.println("Top 3 scored goals:");
  82. teamsGoals.entrySet()
  83. .stream()
  84. .sorted((f, s) -> {
  85. return s.getValue() - f.getValue();
  86. })
  87. .limit(3)
  88. .forEach(e -> {
  89. System.out.printf("- %s -> %d%n", e.getKey(), e.getValue());
  90. });
  91. }
  92.  
  93. }
  94.  
  95. private static String reverse(String text) {
  96. String result = "";
  97. for (int i = text.length() - 1; i >= 0; i--) {
  98. result += text.charAt(i);
  99. }
  100.  
  101. return result;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment