Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, Plant> plants = new HashMap<>();
- int n = Integer.parseInt(scan.nextLine());
- for (int i = 0; i < n; i++) {
- String[] tokens = scan.nextLine().split("<->");
- String plant = tokens[0];
- int rarity = Integer.parseInt(tokens[1]);
- plants.putIfAbsent(plant, new Plant());
- plants.get(plant).setRarity(rarity);
- }
- String input = scan.nextLine();
- while (!"Exhibition".equals(input)) {
- String[] tokens = input.split(": | - ");
- String command = tokens[0];
- String plant = tokens[1];
- if (!plants.containsKey(plant)) {
- input = scan.nextLine();
- System.out.println("error");
- continue;
- }
- switch (command) {
- case "Rate":
- int rating = Integer.parseInt(tokens[2]);
- plants.get(plant).addRating(rating);
- break;
- case "Update":
- int rarity = Integer.parseInt(tokens[2]);
- plants.get(plant).setRarity(rarity);
- break;
- case "Reset":
- plants.get(plant).removeAllRating();
- break;
- }
- input = scan.nextLine();
- }
- System.out.println("Plants for the exhibition:");
- plants
- .entrySet()
- .stream()
- .sorted((p1, p2) -> {
- int result = Integer.compare(p2.getValue().getRarity(), p1.getValue().getRarity());
- if (result == 0) {
- result = Double.compare(p2.getValue().getAverageRating(), p1.getValue().getAverageRating());
- }
- return result;
- })
- .forEach(p -> System.out.printf("- %s; Rarity: %s; Rating: %.2f%n",
- p.getKey(),
- p.getValue().getRarity(),
- p.getValue().getAverageRating()));
- }
- static class Plant {
- private int rarity;
- private List<Integer> ratings;
- public Plant() {
- this.ratings = new ArrayList<>();
- }
- public int getRarity() {
- return rarity;
- }
- public void setRarity(int rarity) {
- this.rarity = rarity;
- }
- public void addRating(int rating) {
- this.ratings.add(rating);
- }
- public void removeAllRating() {
- this.ratings.clear();
- }
- public double getAverageRating() {
- double avg = 0.0;
- for (Integer rating : this.ratings) {
- avg += rating;
- }
- return this.ratings.size() > 0 ? avg / this.ratings.size() : 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment