Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class PlantDiscovery {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Map<String, Integer> plantsRarity = new LinkedHashMap<>();
- Map<String, List<Double>> plantsRating = new LinkedHashMap<>();
- for (int index = 0; index < n; index++) {
- String[] dataPlant = scanner.nextLine().split("<->");
- String plant = dataPlant[0];
- int rarity = Integer.parseInt(dataPlant[1]);
- plantsRarity.put(plant, rarity);
- plantsRating.putIfAbsent(plant, new ArrayList<>());
- }
- String input = scanner.nextLine();
- while (!input.equals("Exhibition")) {
- String[] data = input.split(": ");
- String command = data[0];
- String[] information = data[1].split(" - ");
- String plant = information[0];
- if (!plantsRarity.containsKey(plant)) {
- System.out.println("error");
- input = scanner.nextLine();
- continue;
- }
- switch (command) {
- case "Rate":
- double rating = Double.parseDouble(information[1]);
- plantsRating.get(plant).add(rating);
- break;
- case "Update":
- int newRarity = Integer.parseInt(information[1]);
- plantsRarity.put(plant, newRarity);
- break;
- case "Reset":
- plantsRating.get(plant).clear();
- break;
- default:
- System.out.println("error");
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println("Plants for the exhibition:");
- plantsRarity.entrySet()
- .stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue()
- .thenComparingDouble(x -> plantsRating.get(x.getKey()).stream()
- .mapToDouble(Double::doubleValue)
- .average().orElse(0.0))
- .reversed())
- .forEach(e -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", e.getKey(), e.getValue(),
- plantsRating.get(e.getKey()).stream().mapToDouble(Double::doubleValue).average().orElse(0.0)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment