Advertisement
Guest User

Untitled

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