Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advance.Exams.FinalExams;
- import java.text.DecimalFormat;
- import java.util.*;
- public class PlantDiscovery3 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // I`m adding DecimalFormat in case we get Double in the input tokens
- DecimalFormat df = new DecimalFormat("0.####");
- // Number of plants that we have to store
- int numberOfPlants = Integer.parseInt(scanner.nextLine());
- // Making Map where at the key i`m conserving the name of the plant, and
- // at the value making List of Double to store at index 0 rarity of the plant,
- // and at index 1 to store the rate of the plant.
- //----------------------------------------------
- // Here we`re making a new Map too, of <String, Integer> to store how many times
- // every plant rate is updated - we need it at the end for making calculation
- // of what is the average rate of every plant.
- LinkedHashMap<String, List<Double>> plantsMap = new LinkedHashMap<>();
- LinkedHashMap<String, Integer> countMap = new LinkedHashMap<>();
- // Input data, {plant}<->{rarity}
- for (int i = 1; i <= numberOfPlants; i++) {
- String[] inputTokens = scanner.nextLine().split("<->");
- String namePlant = inputTokens[0];
- double rarity = Double.parseDouble(inputTokens[1]);
- addingPlant(countMap, plantsMap, namePlant, rarity);
- }
- // Making a while loop until we receive "Exhibition".
- String command = scanner.nextLine();
- while (!command.equals("Exhibition")) {
- // "Rate: {plant} - {rating}" – add the given rating to the plant (store all ratings)
- // "Update: {plant} - {new_rarity}" – update the rarity of the plant with the new one
- // "Reset: {plant}" – remove all the ratings of the given plant
- String[] commandTokenArr = command.split(":\\s+");
- String currentCommand = commandTokenArr[0];
- String[] plantNameArr = commandTokenArr[1].split("\\s+-\\s+");
- String currentNamePlant = plantNameArr[0];
- // Checking if the given plant name exist in out Map of plants
- // If it`s not in, print "error" and get new command
- if (!plantsMap.containsKey(currentNamePlant)) {
- System.out.println("error");
- command = scanner.nextLine();
- continue;
- }
- execution(countMap, plantsMap, currentCommand, plantNameArr, currentNamePlant);
- command = scanner.nextLine();
- }
- printMap(df, plantsMap, countMap);
- }
- private static void printMap(DecimalFormat df, LinkedHashMap<String, List<Double>> plantsMap, LinkedHashMap<String, Integer> countMap) {
- // Here we have to print
- // "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}"
- System.out.println("Plants for the exhibition:");
- for (Map.Entry<String, List<Double>> item : plantsMap.entrySet()) {
- double average = item.getValue().get(1) / countMap.get(item.getKey());
- System.out.printf("- %s; Rarity: %s; Rating: %.2f%n", item.getKey(), df.format(item.getValue().get(0)), average);
- }
- }
- private static void execution(LinkedHashMap<String, Integer> countMap, LinkedHashMap<String, List<Double>> plantsMap, String currentCommand, String[] plantNameArr, String currentNamePlant) {
- // checking what is the currentCommand.
- if (currentCommand.equals("Rate")) {
- // We have to UPGRADE the new rate with the old one.
- // oldRate + newRate and set it at the current key, on value at index 1.
- double oldRate = plantsMap.get(currentNamePlant).get(1);
- double newRate = Double.parseDouble(plantNameArr[1]);
- plantsMap.get(currentNamePlant).set(1, oldRate + newRate);
- countMap.put(currentNamePlant, countMap.get(currentNamePlant) + 1);
- } else if (currentCommand.equals("Update")) {
- // Here we have to UPDATE the new rarity with new one. NOT SUMMING, just replace it, with the new.
- double newRarity = Double.parseDouble(plantNameArr[1]);
- plantsMap.get(currentNamePlant).set(0, newRarity);
- } else if (currentCommand.equals("Reset")) {
- // Here we have to just set the currentPlantName ratings to 0;
- plantsMap.get(currentNamePlant).set(1, 0.0);
- }
- }
- private static void addingPlant(LinkedHashMap<String, Integer> countMap, LinkedHashMap<String, List<Double>> plantsMap, String namePlant, double rarity) {
- // checking if the plant is already listed, if it is - we update the rarity with the new one.
- if (plantsMap.containsKey(namePlant)) {
- plantsMap.get(namePlant).set(0, rarity);
- } else {
- // if it`s not listed already, we're doing to add a new plant.
- // Putting a key value = namePlant and as value new Arraylist.
- // On the new ArrayList we add at index 0 the rarity variable.
- // On the index 1 we`re putting default 0.0.
- // On the countMap we`re just adding the name ot the plant and set default 0.
- plantsMap.put(namePlant, new ArrayList<>());
- plantsMap.get(namePlant).add(0, rarity);
- plantsMap.get(namePlant).add(1, 0.0);
- countMap.put(namePlant, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment