Yachkov

Untitled

Dec 5th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. namespace _03
  7. {
  8.     public class Animals
  9.     {
  10.         public Animals() { }
  11.  
  12.         public Animals(string name, int foodLimitDaily, string area)
  13.         {
  14.             Name = name;
  15.             FoodLimitDaily = foodLimitDaily;
  16.             Area = area;
  17.         }
  18.  
  19.         public string Name { get; set; }
  20.         public int FoodLimitDaily { get; set; }
  21.         public string Area { get; set; }
  22.  
  23.         public Animals Feed(Animals animal, List<Animals> collection, string name, int food)
  24.         {
  25.             animal.FoodLimitDaily -= food;
  26.             if (animal.FoodLimitDaily <= 0)
  27.             {
  28.                 Console.WriteLine($"{animal.Name} was successfully fed");
  29.                 collection.Remove(animal);
  30.             }
  31.  
  32.             return animal;
  33.         }
  34.     }
  35.  
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             List<Animals> animalCollection = new List<Animals>();
  41.             Animals instance = new Animals();
  42.  
  43.             string cmd = Console.ReadLine();
  44.             while (!cmd.Contains("EndDay"))
  45.             {
  46.                 if (cmd.StartsWith("Add: "))
  47.                 {
  48.                     cmd = cmd.Remove(0, 5);
  49.  
  50.                     string[] split = cmd.Split("-").ToArray();
  51.                     string name = split[0];
  52.                     int foodQuantity = int.Parse(split[1]);
  53.                     string area = split[2];
  54.  
  55.                     Animals existingAnimal = animalCollection.FirstOrDefault(x => x.Name == name);
  56.                     if (existingAnimal == null)
  57.                     {
  58.                         animalCollection.Add(new Animals(name, foodQuantity, area));
  59.                     }
  60.                     else
  61.                     {
  62.                         existingAnimal.FoodLimitDaily += foodQuantity;
  63.                     }
  64.                 }
  65.  
  66.                 if (cmd.StartsWith("Feed: "))
  67.                 {
  68.                     cmd = cmd.Remove(0, 6);
  69.  
  70.                     string[] split = cmd.Split("-").ToArray();
  71.                     string name = split[0];
  72.                     int food = int.Parse(split[1]);
  73.  
  74.                     Animals animal = animalCollection.FirstOrDefault(x => x.Name == name);
  75.                     if (animal != null)
  76.                     {
  77.                         animal = instance.Feed(animal, animalCollection, name, food);
  78.                     }
  79.                    
  80.                 }
  81.  
  82.                 cmd = Console.ReadLine();
  83.             }
  84.  
  85.             Console.WriteLine("Animals:");
  86.             foreach (Animals an in animalCollection.OrderByDescending(x => x.FoodLimitDaily).ThenBy(x => x.Name))
  87.             {
  88.                 Console.WriteLine($" {an.Name} -> {an.FoodLimitDaily}g");
  89.             }
  90.  
  91.             animalCollection = animalCollection.Where(x => x.FoodLimitDaily > 0).ToList();
  92.  
  93.             string temp = string.Empty;
  94.             Console.WriteLine("Areas with hungry animals:");
  95.             foreach (Animals an in animalCollection.OrderByDescending(x => animalCollection.Where(x => x.FoodLimitDaily > 0).Count()).ThenBy(x => x.Area))
  96.             {
  97.                 if (temp == null || temp != an.Area)
  98.                 {
  99.                     Console.WriteLine($" {an.Area}: {animalCollection.Where(x => x.Area == an.Area).Count()}");
  100.                     temp = an.Area;
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment