borovaneca

PlantsDiscovery

Jan 4th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. package Advance.Exams.FinalExams;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.*;
  5.  
  6. public class PlantDiscovery3 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         // I`m adding DecimalFormat in case we get Double in the input tokens
  11.         DecimalFormat df = new DecimalFormat("0.####");
  12.  
  13.  
  14.         // Number of plants that we have to store
  15.         int numberOfPlants = Integer.parseInt(scanner.nextLine());
  16.  
  17.         // Making Map where at the key i`m conserving the name of the plant, and
  18.         // at the value making List of Double to store at index 0 rarity of the plant,
  19.         // and at index 1 to store the rate of the plant.
  20.         //----------------------------------------------
  21.         // Here we`re making a new Map too, of <String, Integer> to store how many times
  22.         // every plant rate is updated - we need it at the end for making calculation
  23.         // of what is the average rate of every plant.
  24.         LinkedHashMap<String, List<Double>> plantsMap = new LinkedHashMap<>();
  25.         LinkedHashMap<String, Integer> countMap = new LinkedHashMap<>();
  26.  
  27.         // Input data, {plant}<->{rarity}
  28.         for (int i = 1; i <= numberOfPlants; i++) {
  29.             String[] inputTokens = scanner.nextLine().split("<->");
  30.             String namePlant = inputTokens[0];
  31.             double rarity = Double.parseDouble(inputTokens[1]);
  32.  
  33.             addingPlant(countMap, plantsMap, namePlant, rarity);
  34.         }
  35.  
  36.         // Making a while loop until we receive "Exhibition".
  37.         String command = scanner.nextLine();
  38.  
  39.         while (!command.equals("Exhibition")) {
  40.             //  "Rate: {plant} - {rating}" – add the given rating to the plant (store all ratings)
  41.             //  "Update: {plant} - {new_rarity}" – update the rarity of the plant with the new one
  42.             //  "Reset: {plant}" – remove all the ratings of the given plant
  43.             String[] commandTokenArr = command.split(":\\s+");
  44.             String currentCommand = commandTokenArr[0];
  45.             String[] plantNameArr = commandTokenArr[1].split("\\s+-\\s+");
  46.             String currentNamePlant = plantNameArr[0];
  47.  
  48.             // Checking if the given plant name exist in out Map of plants
  49.             // If it`s not in, print "error" and get new command
  50.             if (!plantsMap.containsKey(currentNamePlant)) {
  51.                 System.out.println("error");
  52.                 command = scanner.nextLine();
  53.                 continue;
  54.             }
  55.  
  56.             execution(countMap, plantsMap, currentCommand, plantNameArr, currentNamePlant);
  57.  
  58.  
  59.             command = scanner.nextLine();
  60.         }
  61.  
  62.         printMap(df, plantsMap, countMap);
  63.  
  64.  
  65.     }
  66.  
  67.     private static void printMap(DecimalFormat df, LinkedHashMap<String, List<Double>> plantsMap, LinkedHashMap<String, Integer> countMap) {
  68.         // Here we have to print
  69.         // "Plants for the exhibition:
  70.         //- {plant_name1}; Rarity: {rarity}; Rating: {average_rating}
  71.         //- {plant_name2}; Rarity: {rarity}; Rating: {average_rating}
  72.         //…
  73.         //- {plant_nameN}; Rarity: {rarity}; Rating: {average_rating}"
  74.  
  75.         System.out.println("Plants for the exhibition:");
  76.         for (Map.Entry<String, List<Double>> item : plantsMap.entrySet()) {
  77.             double average = item.getValue().get(1) / countMap.get(item.getKey());
  78.             // Here we`re checking if one of the two number are == 0
  79.             // If they are simply setting average variable to 0.0
  80.             if (item.getValue().get(1) == 0.0 || countMap.get(item.getKey()) == 0.0) {
  81.                 average = 0.0;
  82.             }
  83.             System.out.printf("- %s; Rarity: %s; Rating: %.2f%n", item.getKey(), df.format(item.getValue().get(0)), average);
  84.         }
  85.     }
  86.  
  87.     private static void execution(LinkedHashMap<String, Integer> countMap, LinkedHashMap<String, List<Double>> plantsMap, String currentCommand, String[] plantNameArr, String currentNamePlant) {
  88.         // checking what is the currentCommand.
  89.         if (currentCommand.equals("Rate")) {
  90.             // We have to UPGRADE the new rate with the old one.
  91.             // oldRate + newRate and set it at the current key, on value at index 1.
  92.  
  93.             double oldRate = plantsMap.get(currentNamePlant).get(1);
  94.             double newRate = Double.parseDouble(plantNameArr[1]);
  95.             plantsMap.get(currentNamePlant).set(1, oldRate + newRate);
  96.             countMap.put(currentNamePlant, countMap.get(currentNamePlant) + 1);
  97.            
  98.         } else if (currentCommand.equals("Update")) {
  99.             // Here we have to UPDATE the new rarity with new one. NOT SUMMING, just replace it, with the new.
  100.  
  101.             double newRarity = Double.parseDouble(plantNameArr[1]);
  102.             plantsMap.get(currentNamePlant).set(0, newRarity);
  103.            
  104.         } else if (currentCommand.equals("Reset")) {
  105.             // Here we have to just set the currentPlantName ratings to 0;
  106.            
  107.             plantsMap.get(currentNamePlant).set(1, 0.0);
  108.            
  109.         }
  110.     }
  111.  
  112.     private static void addingPlant(LinkedHashMap<String, Integer> countMap, LinkedHashMap<String, List<Double>> plantsMap, String namePlant, double rarity) {
  113.  
  114.         // checking if the plant is already listed, if it is - we update the rarity with the new one.
  115.         if (plantsMap.containsKey(namePlant)) {
  116.             plantsMap.get(namePlant).set(0, rarity);
  117.         } else {
  118.             // if it`s not listed already, we're doing to add a new plant.
  119.             // Putting a key value = namePlant and as value new Arraylist.
  120.             // On the new ArrayList we add at index 0 the rarity variable.
  121.             // On the index 1 we`re putting default 0.0.
  122.             // On the countMap we`re just adding the name ot the plant and set default 0.
  123.             plantsMap.put(namePlant, new ArrayList<>());
  124.             plantsMap.get(namePlant).add(0, rarity);
  125.             plantsMap.get(namePlant).add(1, 0.0);
  126.             countMap.put(namePlant, 0);
  127.         }
  128.     }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment