borovaneca

Plant Discovery

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