Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package AssociativeArrays;
  2.  
  3. import java.util.*;
  4. import java.util.stream.IntStream;
  5.  
  6. public class Rankings {
  7. public static class Contests{
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public int getPoints(){
  15. return points;
  16. }
  17. public void setPoints(int points){
  18. this.points=points;
  19. }
  20. String name;
  21. Integer points;
  22. Contests(String name,Integer points){
  23. this.name=name;
  24. this.points=points;
  25. }
  26. }
  27.  
  28. public static void main(String[] args) {
  29. TreeMap<String,String> contestsPasswords=new TreeMap<>();
  30. Map<String, List<Contests>> usersContests=new TreeMap<>();
  31. Scanner scanner = new Scanner(System.in);
  32. String firstLines=scanner.nextLine();
  33. while (!firstLines.equals("end of contests")){
  34. if(firstLines.contains(":")){
  35. String[] input=firstLines.split(":");
  36. String contest=input[0];
  37. String password=input[1];
  38. contestsPasswords.put(contest,password);
  39. }
  40. firstLines=scanner.nextLine();
  41. }
  42. String secondLine=scanner.nextLine();
  43. while (!"end of submissions".equals(secondLine)){
  44. String[] input=secondLine.split("=>");
  45. String contest=input[0];
  46. String password=input[1];
  47. String user=input[2];
  48. int points=Integer.parseInt(input[3]);
  49. if(password.equals(contestsPasswords.get(contest))) {
  50. Contests contestUser = new Contests(contest, points);
  51. if (!usersContests.containsKey(user)) {
  52. usersContests.put(user, new ArrayList<>());
  53. usersContests.get(user).add(contestUser);
  54. } else{
  55. boolean isContained=false;
  56. for (Contests e : usersContests.get(user)) {
  57. if (e.getName().equals(contest)) {
  58. isContained = true;
  59. break;
  60. }
  61. }
  62. if(!isContained){
  63. usersContests.get(user).add(contestUser);
  64. }
  65. }
  66. for (Contests e : usersContests.get(user)) {
  67. if (e.getName().equals(contest)) {
  68. if (e.getPoints() < points) {
  69. e.setPoints(points);
  70. }
  71. }
  72. }
  73. }
  74. secondLine=scanner.nextLine();
  75. }
  76. Map<String,Integer>bestStudent=new HashMap<>();
  77. int max=0;
  78. int currentMax=0;
  79. String currentStudent="";
  80.  
  81. for (Map.Entry<String, List<Contests>> stringListEntry : usersContests.entrySet()) {
  82. currentMax=stringListEntry.getValue().stream().map(Contests::getPoints)
  83. .mapToInt(Integer::intValue)
  84. .sum();
  85. if(max<currentMax){
  86. max=currentMax;
  87. currentStudent=stringListEntry.getKey();
  88. }
  89. }
  90. bestStudent.put(currentStudent,max);
  91. bestStudent.forEach((key, value) -> System.out.printf("Best candidate is %s with total %d points.%n",
  92. key, value));
  93. System.out.println("Ranking: ");
  94. usersContests.forEach((key, value) -> {
  95. System.out.printf("%s%n", key);
  96. value.stream()
  97. .sorted((a,b)->b.getPoints()-a.getPoints())
  98. .forEach(s -> System.out.printf("# %s -> %d%n", s.getName(), s.getPoints()));
  99. });
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement