Advertisement
IvaAnd

Exam_03

Aug 9th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Stream;
  3.  
  4. public class Ex3 {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int n = Integer.parseInt(scanner.nextLine());
  9.  
  10. Map<String, Integer> rarity = new LinkedHashMap<>();
  11. Map<String, List<Integer>> rating = new LinkedHashMap<>();
  12.  
  13. for (int i = 0; i < n; i++) {
  14. String[] rarityInput = scanner.nextLine().split("<->");
  15. String plant = rarityInput[0];
  16. int rarityCount = Integer.parseInt(rarityInput[1]);
  17. rarity.putIfAbsent(plant, rarityCount);
  18. rating.putIfAbsent(plant, new ArrayList<>());
  19.  
  20. if (rarity.containsKey(plant)) {
  21. rarity.put(plant, rarityCount);
  22. }
  23.  
  24. }
  25. String command = scanner.nextLine();
  26. while (!command.equals("Exhibition")) {
  27. String[] tokens = command.split(": ");
  28. command = tokens[0];
  29. String[] plantActions = tokens[1].split(" - ");
  30. String plant = plantActions[0];
  31.  
  32. switch (command) {
  33. case "Rate":
  34. int currRating = Integer.parseInt(plantActions[1]);
  35. rating.get(plant).add(currRating);
  36. break;
  37. case "Update":
  38. int newRarity = Integer.parseInt(plantActions[1]);
  39. rarity.put(plant, newRarity);
  40. break;
  41. case "Reset":
  42. rating.get(plant).clear();
  43. break;
  44.  
  45. }
  46. command = scanner.nextLine();
  47. }
  48.  
  49. Map<String, Double> averageRating = new LinkedHashMap<>();
  50.  
  51.  
  52. rating
  53. .entrySet()
  54. .stream()
  55. .forEach(plant -> {
  56. int sum = 0;
  57. double average = 0.00;
  58. for (Integer currRating : plant.getValue()) {
  59. sum += currRating;
  60. }
  61. if (plant.getValue().size() == 0) {
  62. averageRating.put(plant.getKey(), 0.00);
  63. } else {
  64. average = sum * 1.0 / plant.getValue().size();
  65. averageRating.put(plant.getKey(), average);
  66. }
  67. });
  68.  
  69.  
  70. System.out.println("Plants for the exhibition:");
  71.  
  72. rarity
  73. .entrySet()
  74. .stream()
  75. .sorted((f, s) -> {
  76. int result = s.getValue().compareTo(f.getValue());
  77.  
  78. if (result ==0){
  79. result = averageRating.get(s.getKey()).compareTo(averageRating.get(f.getKey()));
  80. }
  81. return result;
  82. })
  83. .forEach(plant -> {
  84. System.out.println
  85. (String.format("- %s; Rarity: %d; Rating: %.2f"
  86. , plant.getKey(), plant.getValue(), averageRating.get(plant.getKey())));
  87.  
  88. });
  89.  
  90.  
  91.  
  92. }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement