RGAlex

O3. Plant Discovery / FinalExam - O2

Mar 26th, 2021
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package FinalExam.O2;
  2.  
  3. import java.util.*;
  4.  
  5. public class O3PlantDiscovery {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.         Map<String, Map<String, List<Double>>> plantInfo = new HashMap<>();
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             String[] input = scanner.nextLine().split("<->");
  14.             plantInfo.put(input[0], new HashMap<>() {{
  15.                 put(input[1], new ArrayList<>());
  16.             }});
  17.         }
  18.         String input;
  19.  
  20.         while (!"Exhibition".equals(input = scanner.nextLine())) {
  21.             String[] command = input.split(": ");
  22.             String[] commands = command[1].split(" - ");
  23.             switch (command[0]) {
  24.                 case "Rate":
  25.                     if (plantInfo.containsKey(commands[0])) {
  26.                         plantInfo.get(commands[0]).get(plantInfo.get(commands[0]).keySet().toString()
  27.                                 .replaceAll("[\\[\\]]", "")).add(Double.parseDouble(commands[1]));
  28.                     } else {
  29.                         System.out.println("error");
  30.                     }
  31.                     break;
  32.                 case "Update":
  33.                     if (plantInfo.containsKey(commands[0])) {
  34.                         plantInfo.put(commands[0], new HashMap<>() {{
  35.                             put(commands[1], plantInfo.get(commands[0]).get(plantInfo.get(commands[0]).keySet().toString()
  36.                                     .replaceAll("[\\[\\]]", "")));
  37.                         }});
  38.                     } else {
  39.                         System.out.println("error");
  40.                     }
  41.                     break;
  42.                 case "Reset":
  43.                     if (plantInfo.containsKey(commands[0])) {
  44.                         plantInfo.get(commands[0]).get(plantInfo.get(commands[0]).keySet().toString()
  45.                                 .replaceAll("[\\[\\]]", "")).clear();
  46.                     } else {
  47.                         System.out.println("error");
  48.                     }
  49.                     break;
  50.                 default:
  51.                     System.out.println("error");
  52.                     break;
  53.             }
  54.         }
  55.         System.out.println("Plants for the exhibition:");
  56.         plantInfo.entrySet().stream().sorted((a, b) -> {
  57.             int sort = Integer.compare(Integer.parseInt(b.getValue().keySet().toString()
  58.                             .replaceAll("[\\[\\]]", "")),
  59.                     Integer.parseInt((a.getValue().keySet().toString().replaceAll("[\\[\\]]", ""))));
  60.             if (sort == 0) {
  61.                 sort = Double.compare(b.getValue().get(b.getValue().keySet().toString()
  62.                                 .replaceAll("[\\[\\]]", ""))
  63.                                 .stream().mapToDouble(Double::doubleValue).average().orElse(0.00),
  64.                         a.getValue().get(a.getValue().keySet().toString()
  65.                                 .replaceAll("[\\[\\]]", "")).stream()
  66.                                 .mapToDouble(Double::doubleValue).average().orElse(0.00));
  67.             }
  68.             return sort;
  69.         }).forEach(e -> System.out.printf("- %s; Rarity: %s; Rating: %.2f%n",
  70.                 e.getKey(), e.getValue().keySet().toString().replaceAll("[\\[\\]]", ""),
  71.                 e.getValue().get(e.getValue().keySet().toString().replaceAll("[\\[\\]]", ""))
  72.                         .stream().mapToDouble(Double::doubleValue).average().orElse(0.00)));
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment