Teo_Yanchev

PlantDiscovery

Dec 4th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._PlantDiscovery
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, Plants> dictPlants = new Dictionary<string, Plants>();
  12.             int n = int.Parse(Console.ReadLine());
  13.  
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string[] info = Console.ReadLine().Split("<->",
  17.                     StringSplitOptions.RemoveEmptyEntries);
  18.                 string plant = info[0];
  19.                 int rarity = int.Parse(info[1]);
  20.  
  21.                 Plants infoPlant = new Plants(rarity);
  22.  
  23.                 if (!dictPlants.ContainsKey(plant))
  24.                 {
  25.                     dictPlants.Add(plant, infoPlant);
  26.                 }
  27.  
  28.                 else
  29.                 {
  30.                     dictPlants[plant] = infoPlant;
  31.                 }
  32.             }
  33.  
  34.             string command = Console.ReadLine();
  35.             while (command != "Exhibition")
  36.             {
  37.                 string[] currentCommand = command.Split(new char[] { ':', '-', ' ' },
  38.                     StringSplitOptions.RemoveEmptyEntries);
  39.                 string instruction = currentCommand[0];
  40.                 string plant = currentCommand[1];
  41.  
  42.                 if (dictPlants.ContainsKey(plant))
  43.                 {
  44.                     if (instruction == "Rate")
  45.                     {
  46.                         double rating = double.Parse(currentCommand[2]);
  47.  
  48.                         dictPlants[plant].Rating.Add(rating);
  49.                     }
  50.  
  51.                     else if (instruction == "Update")
  52.                     {
  53.                         int newRarity = int.Parse(currentCommand[2]);
  54.  
  55.                         dictPlants[plant].Rarity = newRarity;
  56.                     }
  57.  
  58.                     else if (instruction== "Reset")
  59.                     {
  60.                         dictPlants[plant].Rating.Clear();
  61.                     }
  62.  
  63.                     else
  64.                     {
  65.                         Console.WriteLine("error");
  66.                     }
  67.                 }
  68.  
  69.                 else
  70.                 {
  71.                     Console.WriteLine("error");
  72.                 }
  73.                 command = Console.ReadLine();
  74.             }
  75.  
  76.             foreach (var plant in dictPlants)
  77.             {
  78.                 List<double> avrRating = plant.Value.Rating;
  79.                 int count = avrRating.Count;
  80.  
  81.                 if (count == 0)
  82.                 {
  83.                     plant.Value.AvrgRating = 0;
  84.                 }
  85.  
  86.                 else
  87.                 {
  88.                     plant.Value.AvrgRating = avrRating.Sum() / count;
  89.                 }
  90.             }
  91.  
  92.             var orderedPlants = dictPlants
  93.                 .OrderByDescending(rarity => rarity.Value.Rarity)
  94.                 .ThenByDescending(avrg => avrg.Value.AvrgRating)
  95.                 .ToDictionary(k => k.Key, v => v.Value);
  96.  
  97.  
  98.             Console.WriteLine("Plants for the exhibition:");
  99.             foreach (var plant in orderedPlants)
  100.             {
  101.                 string plantName = plant.Key;
  102.                 int rarity = plant.Value.Rarity;
  103.                 double avrRating = plant.Value.AvrgRating;
  104.  
  105.                 Console.WriteLine($"- {plantName}; Rarity: {rarity}; Rating: {avrRating:f2}");
  106.             }
  107.         }
  108.  
  109.         public class Plants
  110.         {
  111.             public int Rarity { get; set; }
  112.             public List<double> Rating { get; set; }
  113.             public double AvrgRating { get; set; }
  114.  
  115.             public Plants(int rarity)
  116.             {
  117.                 this.Rarity = rarity;
  118.                 this.Rating = new List<double>();
  119.             }
  120.         }
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment