Advertisement
Guest User

Untitled

a guest
Nov 11th, 2021
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.*;
  3. import java.util.function.Function;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.IntStream;
  8. import java.util.stream.Stream;
  9.  
  10. public class Test {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.  
  15.         int n = Integer.parseInt(scanner.nextLine());
  16.         Map<String, Integer> rarity = new HashMap<>();
  17.         Map<String, List<Double>> rating = new HashMap<>();
  18.  
  19.         for (int i = 1; i <= n; i++) {
  20.             String[] tokens = scanner.nextLine().split("<->");
  21.             String plant = tokens[0];
  22.             int rarityPlant = Integer.parseInt(tokens[1]);
  23.             rarity.put(plant, rarityPlant);
  24.             rating.putIfAbsent(plant, new ArrayList<>());
  25.         }
  26.         String input = scanner.nextLine();
  27.         while (!input.equals("Exhibition")) {
  28.             String[] command = input.split(": ");
  29.             //String commandName =command[0];
  30.             //String commandAdd =command[1];
  31.             String[] newToken = command[1].split(" - ");
  32.             String name = newToken[0];
  33.          
  34.             if (!rarity.containsKey(name)) { //here
  35.                 System.out.println("error");
  36.                 input = scanner.nextLine();
  37.                 continue;
  38.             }
  39.  
  40.             switch (command[0]) {
  41.                 case "Rate":        //add the given rating to the plant (store all ratings)
  42.                     double rating2 = Double.parseDouble(newToken[1]);
  43.                     rating.get(name).add(rating2);
  44.                     break;
  45.  
  46.                 case "Update":   //update the rarity of the plant with the new one
  47.                     int newRarity = Integer.parseInt(newToken[1]);
  48.                     rarity.put(name, newRarity);
  49.                     break;
  50.                 case "Reset":    //remove all the ratings of the given plant
  51.                     rating.get(name).clear();
  52.                     break;
  53.                 default:
  54.                     System.out.println("error");
  55.                     break;
  56.             }
  57.             input = scanner.nextLine();
  58.         }
  59.         System.out.println("Plants for the exhibition:");
  60.         /*rarity.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
  61.                 .forEach(entry -> {
  62.                     System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", entry.getKey(), entry.getValue(),
  63.                             rating.get(entry.getKey()).stream().mapToDouble(Double::doubleValue).average().orElse(0.0));
  64.  
  65.                 });*/
  66.         rarity.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()
  67.                         .thenComparingDouble(x -> rating.get(x.getKey()).stream()
  68.                                 .mapToDouble(Double::doubleValue)
  69.                                 .average().orElse(0.0))
  70.                         .reversed())
  71.                 .forEach(entry -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n",
  72.                         entry.getKey(), entry.getValue(),
  73.                         rating.get(entry.getKey()).stream()
  74.                                 .mapToDouble(Double::doubleValue)
  75.                                 .average().orElse(0.0)));
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement