Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamPreperationLastExamContests;
- 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> mapRarity = new HashMap<>();
- Map<String , List<Double>> mapRating= new HashMap<>();
- for (int i = 0; i < n; i++) {
- String input = scanner.nextLine();
- String[]inputArr = input.split("<->");
- String plant = inputArr[0];
- int rarity = Integer.parseInt(inputArr[1]);
- mapRarity.put(plant, rarity);
- mapRating.putIfAbsent(plant, new ArrayList<>());
- mapRating.get(plant).add(0.0);
- }
- String command = scanner.nextLine();
- while (!command.equals("Exhibition")){
- String [] commandArr = command.split(" - ");
- String commandName = commandArr[0];
- if (commandName.contains("Rate:")) {
- String plant = commandName.substring(6);
- double rarity = Double.parseDouble(commandArr[1]);
- if (mapRarity.containsKey(plant) && mapRating.size() >0){
- if (mapRating.get(plant).get(0) == 0){
- mapRating.get(plant).clear();
- mapRating.get(plant).add(rarity);
- } else {
- mapRating.get(plant).add(rarity);
- }
- }else {
- System.out.println("error");
- }
- } else if (commandName.contains("Update")) {
- String plant = commandName.substring(8);
- int newRarity = Integer.parseInt(commandArr[1]);
- if (mapRarity.containsKey(plant)){
- mapRarity.put(plant,newRarity);
- }else {
- System.out.println("error");
- }
- } else {
- String plant = commandName.substring(7);
- if (mapRarity.containsKey(plant)){
- mapRating.get(plant).clear();
- }else {
- System.out.println("error");
- }
- }
- command = scanner.nextLine();
- }
- System.out.println("Plants for the exhibition:");
- mapRarity.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()
- .thenComparingDouble(x -> mapRating.get(x.getKey()).stream()
- .mapToDouble(Double::doubleValue)
- .average().orElse(0.0))
- .reversed())
- .forEach(entry -> System.out.printf("- %s; Rarity: %d; Rating: %.2f%n",
- entry.getKey(), entry.getValue(),
- mapRating.get(entry.getKey()).stream()
- .mapToDouble(Double::doubleValue)
- .average().orElse(0.0)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment