Advertisement
IvaAnd

3. Plant Discovery

Aug 9th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 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. if (!rating.containsKey(plant)) {
  36. System.out.println("error");
  37. continue;
  38. }
  39. rating.get(plant).add(currRating);
  40. break;
  41. case "Update":
  42. int newRarity = Integer.parseInt(plantActions[1]);
  43. rarity.put(plant, newRarity);
  44. break;
  45. case "Reset":
  46. if (!rating.containsKey(plant)) {
  47. System.out.println("error");
  48. continue;
  49. }
  50. rating.get(plant).clear();
  51. break;
  52.  
  53. }
  54. command = scanner.nextLine();
  55. }
  56.  
  57. Map<String, Double> averageRating = new LinkedHashMap<>();
  58.  
  59.  
  60. rating
  61. .entrySet()
  62. .stream()
  63. .forEach(element -> {
  64. int sum = 0;
  65. double average = 0.00;
  66. for (Integer currRating : element.getValue()) {
  67. sum += currRating;
  68. }
  69. if (element.getValue().size() == 0) {
  70. averageRating.put(element.getKey(), 0.00);
  71. } else {
  72. average = sum * 1.0 / element.getValue().size();
  73. averageRating.put(element.getKey(), average);
  74. }
  75. });
  76.  
  77.  
  78. System.out.println("Plants for the exhibition:");
  79.  
  80. rarity
  81. .entrySet()
  82. .stream()
  83. .sorted((f, s) -> {
  84. int result = s.getValue().compareTo(f.getValue());
  85.  
  86. if (result == 0) {
  87. result = averageRating.get(s.getKey()).compareTo(averageRating.get(f.getKey()));
  88. }
  89. return result;
  90. })
  91. .forEach(entry -> {
  92. System.out.println
  93. (String.format("- %s; Rarity: %d; Rating: %.2f"
  94. , entry.getKey(), entry.getValue(), averageRating.get(entry.getKey())));
  95.  
  96. });
  97.  
  98.  
  99. }
  100. }
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement