Advertisement
vlastomar

Untitled

Aug 10th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3.  
  4. public class PlanDiscovery {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7.  
  8. int number = Integer.parseInt(scan.nextLine());
  9.  
  10. Map<String, Integer> rarities = new HashMap<>();
  11. Map<String, List<Integer>> rates = new HashMap<>();
  12.  
  13. for (int i = 0; i < number ; i++) {
  14. String[] input = scan.nextLine().split("<->");
  15. rarities.putIfAbsent(input[0], 0);
  16. rates.putIfAbsent(input[0], new ArrayList<>());
  17. rarities.put(input[0], Integer.parseInt(input[1]));
  18. }
  19.  
  20. String input = scan.nextLine();
  21. while (!"Exhibition".equals(input)){
  22. String[] token = input.split(": ");
  23. switch (token[0]){
  24. case "Rate":
  25.  
  26. String[] option = token[1].split("\\s+\\-\\s+");
  27. if (rates.containsKey(option[0])){
  28. rarities.putIfAbsent(option[0], 0);
  29. rates.putIfAbsent(option[0], new ArrayList<>());
  30. rates.get(option[0]).add(Integer.parseInt(option[1]));
  31. }else {
  32. System.out.println("error");
  33. }
  34.  
  35. break;
  36. case "Update":
  37. option = token[1].split("\\s+\\-\\s+");
  38. if (rarities.containsKey(option[0])){
  39. rarities.put(option[0], Integer.parseInt(option[1]));
  40. }else{
  41. System.out.println("error");
  42. }
  43. break;
  44. case "Reset":
  45. option = token[1].split("\\s+\\-\\s+");
  46. if (rates.containsKey(option[0])){
  47. rates.put(option[0], new ArrayList<>());
  48. }else{
  49. System.out.println("error");
  50. }
  51. break;
  52. }
  53.  
  54. input = scan.nextLine();
  55. }
  56. Map<String, TreeMap<Integer,Double>> average = new HashMap<>();
  57.  
  58. for (Map.Entry<String, List<Integer>> entry : rates.entrySet()) {
  59. int sum = 0;
  60. Double aver = 0.00;
  61. if (!entry.getValue().isEmpty()){
  62. for(int s : entry.getValue() ){
  63. sum += s;
  64. }
  65. aver = sum * 1.00 / entry.getValue().size();
  66. }
  67.  
  68. Comparator<Integer> vlado = (c1,c2) -> Integer.compare(c2,c1);
  69.  
  70. average.putIfAbsent(entry.getKey(), new TreeMap<>(vlado));
  71. average.get(entry.getKey()).putIfAbsent(rarities.get(entry.getKey()), 0.0);
  72. average.get(entry.getKey()).put(rarities.get(entry.getKey()), aver);
  73. }
  74. System.out.println("Plants for the exhibition:");
  75. average.entrySet().stream()
  76. . sorted((s1, s2) -> {
  77. int[] first = {0};
  78. int[] second = {0};
  79. average.get(s1.getKey()).entrySet().stream()
  80. .forEach(f -> {
  81. first[0] = f.getKey();
  82. });
  83. average.get(s2.getKey()).entrySet().stream()
  84. .forEach(f -> {
  85. second[0] = f.getKey();
  86. });
  87. int res = Integer.compare(second[0], first[0]);
  88. if (res == 0){
  89. Double one = s2.getValue().get(rarities.get(s2.getKey()));
  90. Double two = s1.getValue().get(rarities.get(s1.getKey()));
  91. final int compare = Double.compare(one, two);
  92. return compare;
  93. }else{
  94. return res;
  95. }
  96.  
  97. })
  98. . forEach(p ->{
  99. System.out.println(String.format("- %s; Rarity: %d; Rating: %.2f", p.getKey(), rarities.get(p.getKey()), p.getValue().get(rarities.get(p.getKey()))));
  100. });
  101. }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement