borovaneca

PlantDiscovery2

Jan 3rd, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package Advance.Exams.FinalExams;
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. public class DestinationMapper2 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         LinkedHashMap<String, List<Double>> plantsMap = new LinkedHashMap<>();
  12.         for (int i = 1; i <= n; i++) {
  13.             String[] inputArr = scanner.nextLine().split("<->");
  14.             String name = inputArr[0];
  15.             double rarity = Double.parseDouble(inputArr[1]);
  16.             if (plantsMap.containsKey(name)) {
  17.                 plantsMap.get(name).set(0, rarity);
  18.             } else {
  19.                 plantsMap.put(name, new ArrayList<>());
  20.                 plantsMap.get(name).add(0, rarity);
  21.                 plantsMap.get(name).add(1, 0.0);
  22.             }
  23.         }
  24.  
  25.         String command = scanner.nextLine();
  26.         while (!command.equals("Exhibition")) {
  27.             String[] commandArr = command.split(":\\s+|\\s+-\\s+");
  28.             String currentCommand = commandArr[0];
  29.             String name = commandArr[1];
  30.  
  31.             if (plantsMap.containsKey(name)) {
  32.  
  33.                 if (currentCommand.equals("Rate")) {
  34.                     double oldRate = plantsMap.get(name).get(1);
  35.                     double newRate = Double.parseDouble(commandArr[2]);
  36.                     plantsMap.get(name).set(1, newRate + oldRate);
  37.                     if (plantsMap.get(name).size() <= 2) {
  38.                         plantsMap.get(name).add(2, 1.0);
  39.                     } else {
  40.                         double oldCount = plantsMap.get(name).get(2);
  41.                         plantsMap.get(name).set(2, oldCount + 1);
  42.                     }
  43.  
  44.                 } else if (currentCommand.equals("Update")) {
  45.                     double newRarity = Double.parseDouble(commandArr[2]);
  46.                     plantsMap.get(name).set(0, newRarity);
  47.  
  48.                 } else if (currentCommand.equals("Reset")) {
  49.                     plantsMap.get(name).set(1, 0.0);
  50.  
  51.                 }
  52.  
  53.             } else {
  54.                 System.out.println("error");
  55.             }
  56.  
  57.             command = scanner.nextLine();
  58.         }
  59.  
  60.         System.out.println("Plants for the exhibition:");
  61.         for (Map.Entry<String, List<Double>> item : plantsMap.entrySet()) {
  62.             double average = item.getValue().get(1) / item.getValue().get(2);
  63.             System.out.printf("- %s; Rarity: %.0f; Rating: %.2f%n", item.getKey(), item.getValue().get(0), average);
  64.         }
  65.  
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment