Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advance.Exams.FinalExams;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class PlantDiscovery {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int rotation = Integer.parseInt(scanner.nextLine());
- LinkedHashMap<String, Integer> plantsRarity = new LinkedHashMap<>();
- LinkedHashMap<String, Double> plantsRating = new LinkedHashMap<>();
- LinkedHashMap<String, Integer> averageCounter = new LinkedHashMap<>();
- for (int i = 1; i <= rotation; i++) {
- String[] input = scanner.nextLine().split("<->");
- String name = input[0];
- int rarity = Integer.parseInt(input[1]);
- plantsRarity.put(name, rarity);
- plantsRating.put(name, 0.0);
- averageCounter.put(name, 0);
- }
- String command = scanner.nextLine();
- while (!command.equals("Exhibition")) {
- String[] commandArr = command.split(": | - ");
- String plant = commandArr[1];
- if (plantsRarity.containsKey(plant)) {
- if (command.contains("Rate")) {
- double currentRating = plantsRating.get(plant);
- double newRating = Double.parseDouble(commandArr[2]);
- plantsRating.put(plant, currentRating + newRating);
- averageCounter.put(plant, averageCounter.get(plant) + 1);
- } else if (command.contains("Update")) {
- int newRarity = Integer.parseInt(commandArr[2]);
- plantsRarity.put(plant, newRarity);
- } else if (command.contains("Reset")) {
- plantsRating.put(plant, 0.0);
- }
- } else {
- System.out.println("error");
- }
- command = scanner.nextLine();
- }
- System.out.println("Plants for the exhibition:");
- for (Map.Entry<String, Integer> item : plantsRarity.entrySet()) {
- double average = plantsRating.get(item.getKey()) / averageCounter.get(item.getKey());
- System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", item.getKey(), item.getValue(), average);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment