Advertisement
Taniaaleksandrova

03. Plant Discovery

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