Advertisement
knoteva

Fees the animals

Jul 29th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03._Program
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string input;
  13.             var animals = new Dictionary<string, int>();
  14.             var areas = new Dictionary<string, int>();
  15.  
  16.             while ((input = Console.ReadLine()) != "Last Info")
  17.             {
  18.                 string command = input.Split(":")[0];
  19.                 string name = input.Split(":")[1];
  20.                 int food = int.Parse(input.Split(":")[2]);
  21.                 string area = input.Split(":")[3];
  22.  
  23.                 if (command == "Add")
  24.                 {
  25.                     if (!animals.ContainsKey(name))
  26.                     {
  27.                         animals.Add(name, 0);
  28.                         if (!areas.ContainsKey(area))
  29.                         {
  30.                             areas.Add(area, 0);
  31.                         }
  32.                         areas[area]++;
  33.                     }
  34.                     animals[name] += food;
  35.                 }
  36.  
  37.                 else
  38.                 {
  39.                     if (animals.ContainsKey(name))
  40.                     {
  41.                         animals[name] -= food;
  42.                         if (animals[name] <= 0)
  43.                         {
  44.                             Console.WriteLine($"{name} was successfully fed");
  45.                             animals.Remove(name);
  46.                             areas[area]--;
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.             Console.WriteLine($"Animals:");
  52.  
  53.             foreach (var an in animals.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  54.             {
  55.                 Console.WriteLine($"{an.Key} -> {an.Value}g");
  56.             }
  57.             Console.WriteLine($"Areas with hungry animals:");
  58.             foreach (var ar in areas.Where(x => x.Value > 0).OrderByDescending(x => x.Value))
  59.             {
  60.                 Console.WriteLine($"{ar.Key} : {ar.Value}");
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement