Advertisement
MZlatev

Untitled

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