Advertisement
desislava_topuzakova

Untitled

Mar 24th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package demo;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import java.util.Scanner;
  6.  
  7. public class Demo {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. int n = Integer.parseInt(scanner.nextLine());
  12. Map<String, String> plantInfo = new LinkedHashMap<>();
  13. Map<String, List<Double>> plantRatings = new LinkedHashMap<>();
  14.  
  15. for (int i = 0; i < n; i++) {
  16. String[] plantData = scanner.nextLine().split("<->");
  17. String plantName = plantData[0];
  18. String rarity = plantData[1];
  19. plantInfo.put(plantName, rarity);
  20. }
  21.  
  22. String commandInput = scanner.nextLine();
  23.  
  24. while (!commandInput.equals("Exhibition")) {
  25. String[] commandData = commandInput.split(": ");
  26. String command = commandData[0];
  27. String[] commandInfo = commandData[1].split(" - ");
  28. String plant = commandInfo[0];
  29.  
  30. if (command.equals("Rate")) {
  31. double rating = Double.parseDouble(commandInfo[1]);
  32.  
  33. if (plantInfo.containsKey(plant)) {
  34. if (plantRatings.containsKey(plant)) {
  35. plantRatings.get(plant).add(rating);
  36. } else {
  37. plantRatings.put(plant, new ArrayList<>());
  38. plantRatings.get(plant).add(rating);
  39. }
  40. } else {
  41. System.out.println("error");
  42. }
  43. } else if (command.equals("Update")) {
  44. String newRarity = commandInfo[1];
  45. if (plantInfo.containsKey(plant)) {
  46. plantInfo.put(plant, newRarity);
  47. } else {
  48. System.out.println("error");
  49. }
  50. } else if (command.equals("Reset")) {
  51. if (plantInfo.containsKey(plant)) {
  52. plantRatings.get(plant).clear();
  53. } else {
  54. System.out.println("error");
  55. }
  56. }
  57.  
  58.  
  59. commandInput = scanner.nextLine();
  60. }
  61. System.out.println("Plants for the exhibition:");
  62. //- {plant_name1}; Rarity: {rarity}; Rating: {average_rating}
  63. //- {plant_name2}; Rarity: {rarity}; Rating: {average_rating}
  64. //…
  65. //- {plant_nameN}; Rarity: {rarity}; Rating: {average_rating}"
  66. plantInfo.entrySet().forEach(entry -> System.out.println(String.format("- %s; Rarity: %s; Rating: %.2f",
  67. entry.getKey(), entry.getValue(),
  68. plantRatings.get(entry.getKey()) == null || plantRatings.get(entry.getKey()).size() <= 0 ? 0.0 : plantRatings.get(entry.getKey())
  69. .stream()
  70. .mapToDouble(Double::doubleValue)
  71. .average().getAsDouble())));
  72.  
  73. System.out.println("");
  74.  
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement