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