Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demo;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.Scanner;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Map<String, String> plantInfo = new LinkedHashMap<>();
- Map<String, List<Double>> plantRatings = new LinkedHashMap<>();
- for (int i = 0; i < n; i++) {
- String[] plantData = scanner.nextLine().split("<->");
- String plantName = plantData[0];
- String rarity = plantData[1];
- plantInfo.put(plantName, rarity);
- }
- String commandInput = scanner.nextLine();
- while (!commandInput.equals("Exhibition")) {
- String[] commandData = commandInput.split(": ");
- String command = commandData[0];
- String[] commandInfo = commandData[1].split(" - ");
- String plant = commandInfo[0];
- if (command.equals("Rate")) {
- double rating = Double.parseDouble(commandInfo[1]);
- if (plantInfo.containsKey(plant)) {
- if (plantRatings.containsKey(plant)) {
- plantRatings.get(plant).add(rating);
- } else {
- plantRatings.put(plant, new ArrayList<>());
- plantRatings.get(plant).add(rating);
- }
- } else {
- System.out.println("error");
- }
- } else if (command.equals("Update")) {
- String newRarity = commandInfo[1];
- if (plantInfo.containsKey(plant)) {
- plantInfo.put(plant, newRarity);
- } else {
- System.out.println("error");
- }
- } else if (command.equals("Reset")) {
- if (plantInfo.containsKey(plant)) {
- plantRatings.get(plant).clear();
- } else {
- System.out.println("error");
- }
- }
- commandInput = scanner.nextLine();
- }
- System.out.println("Plants for the exhibition:");
- //- {plant_name1}; Rarity: {rarity}; Rating: {average_rating}
- //- {plant_name2}; Rarity: {rarity}; Rating: {average_rating}
- //…
- //- {plant_nameN}; Rarity: {rarity}; Rating: {average_rating}"
- plantInfo.entrySet().forEach(entry -> System.out.println(String.format("- %s; Rarity: %s; Rating: %.2f",
- entry.getKey(), entry.getValue(),
- plantRatings.get(entry.getKey()) == null || plantRatings.get(entry.getKey()).size() <= 0 ? 0.0 : plantRatings.get(entry.getKey())
- .stream()
- .mapToDouble(Double::doubleValue)
- .average().getAsDouble())));
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement