Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Stream;
- public class Ex3 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Map<String, Integer> rarity = new LinkedHashMap<>();
- Map<String, List<Integer>> rating = new LinkedHashMap<>();
- for (int i = 0; i < n; i++) {
- String[] rarityInput = scanner.nextLine().split("<->");
- String plant = rarityInput[0];
- int rarityCount = Integer.parseInt(rarityInput[1]);
- rarity.putIfAbsent(plant, rarityCount);
- rating.putIfAbsent(plant, new ArrayList<>());
- if (rarity.containsKey(plant)) {
- rarity.put(plant, rarityCount);
- }
- }
- String command = scanner.nextLine();
- while (!command.equals("Exhibition")) {
- String[] tokens = command.split(": ");
- command = tokens[0];
- String[] plantActions = tokens[1].split(" - ");
- String plant = plantActions[0];
- switch (command) {
- case "Rate":
- int currRating = Integer.parseInt(plantActions[1]);
- rating.get(plant).add(currRating);
- break;
- case "Update":
- int newRarity = Integer.parseInt(plantActions[1]);
- rarity.put(plant, newRarity);
- break;
- case "Reset":
- rating.get(plant).clear();
- break;
- }
- command = scanner.nextLine();
- }
- Map<String, Double> averageRating = new LinkedHashMap<>();
- rating
- .entrySet()
- .stream()
- .forEach(plant -> {
- int sum = 0;
- double average = 0.00;
- for (Integer currRating : plant.getValue()) {
- sum += currRating;
- }
- if (plant.getValue().size() == 0) {
- averageRating.put(plant.getKey(), 0.00);
- } else {
- average = sum * 1.0 / plant.getValue().size();
- averageRating.put(plant.getKey(), average);
- }
- });
- System.out.println("Plants for the exhibition:");
- rarity
- .entrySet()
- .stream()
- .sorted((f, s) -> {
- int result = s.getValue().compareTo(f.getValue());
- if (result ==0){
- result = averageRating.get(s.getKey()).compareTo(averageRating.get(f.getKey()));
- }
- return result;
- })
- .forEach(plant -> {
- System.out.println
- (String.format("- %s; Rarity: %d; Rating: %.2f"
- , plant.getKey(), plant.getValue(), averageRating.get(plant.getKey())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement