Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace FeedTheAnimals
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var animals = new Dictionary<string, int>();
  12.             var areas = new Dictionary<string, int>();
  13.  
  14.             string command;
  15.  
  16.             while((command=Console.ReadLine())!="Last Info")
  17.             {
  18.                 string[] commandArgs = command.Split(":");
  19.  
  20.                 string action = commandArgs[0];
  21.                 string name = commandArgs[1];
  22.                 int food = int.Parse(commandArgs[2]);
  23.                 string area = commandArgs[3];
  24.  
  25.                 if (action == "Add")
  26.                 {
  27.                     if (!animals.ContainsKey(name))
  28.                     {
  29.                         animals[name] = food;
  30.  
  31.                         if (!areas.ContainsKey(area))
  32.                         {
  33.                             areas[area] = 1;
  34.                         }
  35.                         else
  36.                         {
  37.                             areas[area]++;
  38.                         }
  39.                     }
  40.                     else
  41.                     {
  42.                         animals[name]+= food;
  43.                     }
  44.                 }
  45.                 else if (action == "Feed")
  46.                 {
  47.                     if (animals.ContainsKey(name))
  48.                     {
  49.                         animals[name] -= food;
  50.  
  51.                         if (animals[name] <= 0)
  52.                         {
  53.                             Console.WriteLine($"{name} was successfully fed");
  54.                             animals.Remove(name);
  55.                             areas[area]--;
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.             animals = animals
  61.                 .OrderByDescending(x => x.Value)
  62.                 .ThenBy(x => x.Key)
  63.                 .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
  64.  
  65.             Console.WriteLine("Animals: ");
  66.            
  67.                 foreach (var animal in animals)
  68.                 {
  69.                     Console.WriteLine($"{animal.Key} -> {animal.Value}g");
  70.                 }
  71.  
  72.             areas = areas
  73.                 .OrderByDescending(x => x.Value)
  74.                 .Where(x=>x.Value>0)
  75.                 .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
  76.  
  77.             Console.WriteLine("Areas with hungry animals: ");
  78.  
  79.             foreach (var area in areas)
  80.             {
  81.                 Console.WriteLine($"{area.Key} : {area.Value}");
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement