Advertisement
dragonbs

Plant Discovery

Mar 25th, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Plant
  6. {
  7.     public string Name { get; set; }
  8.     public string Rarity { get; set; }
  9.     public List<double> Rating { get; set; }
  10.  
  11.     public Plant(string name, string rarity)
  12.     {
  13.         this.Name = name;
  14.         this.Rarity = rarity;
  15.         this.Rating = new List<double>();
  16.     }
  17. }
  18.  
  19. class Program
  20. {
  21.     static void Main()
  22.     {
  23.         List<Plant> plants = new List<Plant>();
  24.         ReadInitialPlantEntries(plants);
  25.         string input;
  26.         while ((input = Console.ReadLine()) != "Exhibition")
  27.         {
  28.             string[] cmd = input.Split(new string[] { ": ", " - " }, StringSplitOptions.None);
  29.             if (!plants.Any(x => x.Name == cmd[1]))
  30.             {
  31.                 Console.WriteLine("error");
  32.                 continue;
  33.             }
  34.             Plant currentPlant = plants.Find(x => x.Name == cmd[1]);
  35.             switch (cmd[0])
  36.             {
  37.                 case "Rate":
  38.                     currentPlant.Rating.Add(double.Parse(cmd[2])); break;
  39.                 case "Update":
  40.                     currentPlant.Rarity = cmd[2]; break;
  41.                 case "Reset":
  42.                     currentPlant.Rating.Clear(); break;
  43.             }
  44.         }
  45.         Console.WriteLine("Plants for the exhibition:");
  46.         foreach (Plant plant in plants)
  47.         {
  48.             double rating = plant.Rating.Count > 0 ? plant.Rating.Average() : 0;
  49.             Console.WriteLine($"- {plant.Name}; Rarity: {plant.Rarity}; Rating: {rating:f2}");
  50.         }
  51.     }
  52.  
  53.     private static void ReadInitialPlantEntries(List<Plant> plants)
  54.     {
  55.         int numberOfEntries = int.Parse(Console.ReadLine());
  56.         for (int i = 0; i < numberOfEntries; i++)
  57.         {
  58.             string[] input = Console.ReadLine().Split("<->");
  59.             string plantName = input[0];
  60.             string plantRarity = input[1];
  61.  
  62.             if (plants.Any(x => x.Name == plantName))
  63.             {
  64.                 Plant plantToUpdate = plants.Find(x => x.Name == plantName);
  65.                 plantToUpdate.Rarity = plantRarity;
  66.             }
  67.             else
  68.                 plants.Add(new Plant(plantName, plantRarity));
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement