Advertisement
veronikaaa86

03. Plant Discovery

Jul 19th, 2023
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package finalExamPrep;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class P03PlantDiscovery {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int n = Integer.parseInt(scanner.nextLine());
  12.  
  13.         Map<String, Integer> plantRarityMap = new HashMap<>();
  14.         Map<String, Double> rateMap = new HashMap<>();
  15.  
  16.         for (int i = 0; i < n; i++) {
  17.             String[] tokens = scanner.nextLine().split("<->");
  18.             String plant = tokens[0];
  19.             int rarity = Integer.parseInt(tokens[1]);
  20.  
  21.             plantRarityMap.put(plant, rarity);
  22.  
  23.             rateMap.put(plant, 0.0);
  24.         }
  25.  
  26.         String inputLine = scanner.nextLine();
  27.         while (!inputLine.equals("Exhibition")) {
  28.             String[] tokens = inputLine.split("[: -]+");
  29.             String command = tokens[0];
  30.             String plant = tokens[1];
  31.  
  32.             boolean isValidCommand = command.equals("Rate") || command.equals("Update") || command.equals("Reset");
  33.  
  34.             if (!plantRarityMap.containsKey(plant) || !isValidCommand) {
  35.                 System.out.println("error");
  36.             } else {
  37.                 switch (command) {
  38.                     case "Rate":
  39.                         double rate = Double.parseDouble(tokens[2]);
  40.                         if (rateMap.get(plant) == 0) {
  41.                             rateMap.put(plant, rate);
  42.                         } else {
  43.                             double newRate = (rateMap.get(plant) + rate) / 2;
  44.                             rateMap.put(plant, newRate);
  45.                         }
  46.                         break;
  47.                     case "Update":
  48.                         int newRarity = Integer.parseInt(tokens[2]);
  49.  
  50.                         plantRarityMap.put(plant, newRarity);
  51.                         break;
  52.                     case "Reset":
  53.                         rateMap.put(plant, 0.0);
  54.                         break;
  55.                 }
  56.             }
  57.  
  58.             inputLine = scanner.nextLine();
  59.         }
  60.  
  61.         System.out.println("Plants for the exhibition:");
  62.         //- Woodii; Rarity: 5; Rating: 7.50
  63.         for (Map.Entry<String, Integer> entry : plantRarityMap.entrySet()) {
  64.             System.out.printf("- %s; Rarity: %d; Rating: %.2f%n", entry.getKey(), entry.getValue(), rateMap.get(entry.getKey()));
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement