svephoto

Plant Discovery [Java]

Apr 10th, 2021 (edited)
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. package svephoto.company;
  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.  
  9.         int number = Integer.parseInt(scanner.nextLine());
  10.  
  11.         HashMap<String, Integer> plants = new HashMap<>();
  12.         HashMap<String, List<Double>> ratings = new HashMap<>();
  13.  
  14.         while (number-- > 0) {
  15.             String[] input = scanner.nextLine().split("<->");
  16.  
  17.             String plant = input[0];
  18.             int rarity = Integer.parseInt(input[1]);
  19.  
  20.             plants.put(plant, rarity);
  21.             ratings.putIfAbsent(plant, new ArrayList<>());
  22.         }
  23.  
  24.         String input;
  25.  
  26.         while (!"Exhibition".equals(input = scanner.nextLine())) {
  27.             String[] tokens = input.split(": ");
  28.             String command = tokens[0];
  29.             String[] data = tokens[1].split(" - ");
  30.             String name = data[0];
  31.  
  32.             if (!plants.containsKey(data[0])) {
  33.                 System.out.println("error");
  34.                 continue;
  35.             }
  36.  
  37.             switch (command) {
  38.                 case "Rate": {
  39.                     double rating = Double.parseDouble(data[1]);
  40.                     ratings.get(name).add(rating);
  41.                     break;
  42.                 }
  43.                 case "Update": {
  44.                     int updatedRarity = Integer.parseInt(data[1]);
  45.                     plants.put(name, updatedRarity);
  46.                     break;
  47.                 }
  48.                 case "Reset": {
  49.                     ratings.get(name).clear();
  50.                     break;
  51.                 }
  52.                 default:
  53.                     System.out.println("error");
  54.             }
  55.         }
  56.  
  57.         System.out.println("Plants for the exhibition:");
  58.  
  59.         plants.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()
  60.                 .thenComparingDouble(x -> ratings.get(x.getKey()).stream()
  61.                         .mapToDouble(Double::doubleValue)
  62.                         .average().orElse(0.0))
  63.                 .reversed())
  64.                 .forEach(entry -> System.out.println(String.format("- %s; Rarity: %d; Rating: %.2f",
  65.                         entry.getKey(), entry.getValue(),
  66.                         ratings.get(entry.getKey()).stream()
  67.                                 .mapToDouble(Double::doubleValue)
  68.                                 .average().orElse(0.0))));
  69.     }
  70. }
  71.  
Add Comment
Please, Sign In to add comment