Guest User

Plant Discovery

a guest
Dec 12th, 2021
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package ExamPreperationLastExamContests;
  2.  
  3. import java.util.*;
  4.  
  5. public class PlantDiscovery {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int n = Integer.parseInt(scanner.nextLine());
  9. Map<String, Integer> mapRarity = new HashMap<>();
  10. Map<String , List<Double>> mapRating= new HashMap<>();
  11. for (int i = 0; i < n; i++) {
  12. String input = scanner.nextLine();
  13. String[]inputArr = input.split("<->");
  14. String plant = inputArr[0];
  15. int rarity = Integer.parseInt(inputArr[1]);
  16. mapRarity.put(plant, rarity);
  17. mapRating.putIfAbsent(plant, new ArrayList<>());
  18. mapRating.get(plant).add(0.0);
  19. }
  20.  
  21. String command = scanner.nextLine();
  22. while (!command.equals("Exhibition")){
  23. String [] commandArr = command.split(" - ");
  24. String commandName = commandArr[0];
  25. if (commandName.contains("Rate:")) {
  26. String plant = commandName.substring(6);
  27. double rarity = Double.parseDouble(commandArr[1]);
  28. if (mapRarity.containsKey(plant) && mapRating.size() >0){
  29. if (mapRating.get(plant).get(0) == 0){
  30.  
  31. mapRating.get(plant).clear();
  32. mapRating.get(plant).add(rarity);
  33. } else {
  34. mapRating.get(plant).add(rarity);
  35. }
  36. }else {
  37. System.out.println("error");
  38. }
  39. } else if (commandName.contains("Update")) {
  40. String plant = commandName.substring(8);
  41. int newRarity = Integer.parseInt(commandArr[1]);
  42. if (mapRarity.containsKey(plant)){
  43. mapRarity.put(plant,newRarity);
  44. }else {
  45. System.out.println("error");
  46. }
  47.  
  48. } else {
  49. String plant = commandName.substring(7);
  50.  
  51. if (mapRarity.containsKey(plant)){
  52.  
  53. mapRating.get(plant).clear();
  54.  
  55. }else {
  56. System.out.println("error");
  57. }
  58.  
  59. }
  60.  
  61.  
  62. command = scanner.nextLine();
  63. }
  64. System.out.println("Plants for the exhibition:");
  65. mapRarity.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()
  66. .thenComparingDouble(x -> mapRating.get(x.getKey()).stream()
  67. .mapToDouble(Double::doubleValue)
  68. .average().orElse(0.0))
  69. .reversed())
  70.  
  71. .forEach(entry -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n",
  72. entry.getKey(), entry.getValue(),
  73. mapRating.get(entry.getKey()).stream()
  74. .mapToDouble(Double::doubleValue)
  75. .average().orElse(0.0)));
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment