veronikaaa86

03. Plant Discovery

Nov 24th, 2021 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. package examPrepFinal;
  2.  
  3. import java.util.*;
  4.  
  5. public class P03PlantDiscovery {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int n = Integer.parseInt(scanner.nextLine());
  10.  
  11. Map<String, Integer> plantRarityMap = new HashMap<>();
  12. Map<String, Double> rateMap = new HashMap<>();
  13.  
  14. for (int i = 0; i < n; i++) {
  15. String[] tokens = scanner.nextLine().split("<->");
  16. String plant = tokens[0];
  17. int rarity = Integer.parseInt(tokens[1]);
  18.  
  19. plantRarityMap.putIfAbsent(plant, 0);
  20. rateMap.putIfAbsent(plant, 0.0);
  21.  
  22. //K -> plant ------ V -> rarity
  23. plantRarityMap.put(plant, rarity);
  24. // if (plantRarityMap.get(plant) < rarity) {
  25. // }
  26.  
  27. // plantRarityMap.put(plant, rarity);
  28. }
  29.  
  30. String inputLine = scanner.nextLine();
  31. while (!inputLine.equals("Exhibition")) {
  32. String[] tokens = inputLine.split("[: -]+");
  33. String command = tokens[0];
  34. String plant = tokens[1];
  35.  
  36. if (!rateMap.containsKey(plant)) {
  37. System.out.println("error");
  38. } else {
  39. switch (command) {
  40. case "Rate":
  41. double currentRate = Double.parseDouble(tokens[2]);
  42. if (rateMap.get(plant) == 0) {
  43. rateMap.put(plant, currentRate);
  44. } else {
  45. double newRate = (rateMap.get(plant) + currentRate) / 2;
  46. rateMap.put(plant, newRate);
  47. }
  48. break;
  49. case "Update":
  50. int newRarity = Integer.parseInt(tokens[2]);
  51.  
  52. plantRarityMap.put(plant, newRarity);
  53. break;
  54. case "Reset":
  55. rateMap.put(plant, 0.0);
  56. break;
  57. default:
  58. System.out.println("error");
  59. }
  60. }
  61.  
  62.  
  63. inputLine = scanner.nextLine();
  64. }
  65.  
  66. System.out.println("Plants for the exhibition:");
  67. plantRarityMap.entrySet()
  68. .stream()
  69. .sorted((p1, p2) -> {
  70. int result = p2.getValue() - p1.getValue();
  71.  
  72. //asd -> 10 -> rate 70
  73. //veronika -> 10 -> rate 100
  74. if (result == 0) {
  75. //70 - 100
  76. double res = (rateMap.get(p2.getKey()) - rateMap.get(p1.getKey()));
  77. return (int)res;
  78. }
  79. return result;
  80. })
  81. .forEach(entry -> {
  82. System.out.printf("- %s; Rarity: %d; Rating: %.2f%n",
  83. entry.getKey(), entry.getValue(), rateMap.get(entry.getKey()));
  84. });
  85. }
  86. }
  87.  
Add Comment
Please, Sign In to add comment