ralichka

FinalExam-18.04.2018-02.FeedAnimals

Dec 2nd, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.FeedYheAnimal
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> input = Console.ReadLine().Split(":").ToList();
  12.             string command = input[0];
  13.  
  14.             Dictionary<string, int> food = new Dictionary<string, int>();
  15.             Dictionary<string, string> area = new Dictionary<string, string>();
  16.             //Dictionary<string, int> hungry = new Dictionary<string, int>();
  17.  
  18.             int counter = 0;
  19.             while (command != "Last Info")
  20.             {
  21.                 string animal = input[1];
  22.                 string currentArea = input[3];
  23.  
  24.                 if (command == "Add")
  25.                 {
  26.                     int daylyFood = int.Parse(input[2]);
  27.                     if (!food.ContainsKey(animal))
  28.                     {
  29.                         food[animal] = 0;
  30.                         area[animal] = "";
  31.                     }
  32.                     food[animal] += daylyFood;
  33.                     area[animal] = currentArea;
  34.                 }
  35.                 else if (command == "Feed")
  36.                 {
  37.                     int currentFood = int.Parse(input[2]);
  38.  
  39.                     if (food.ContainsKey(animal))
  40.                     {
  41.                         food[animal] -= currentFood;
  42.  
  43.                     }
  44.                 }
  45.                 //ето товеа мисля е на погрешното място
  46.                 if (food[animal] <= 0)
  47.                 {
  48.                     Console.WriteLine($"{animal} was successfully fed");
  49.                     food.Remove(animal);
  50.                     area.Remove(animal);                  
  51.                 }
  52.  
  53.  
  54.                 input = Console.ReadLine().Split(":").ToList();
  55.                 command = input[0];
  56.             }
  57.  
  58.             food = food.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
  59.             area = area.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, y => y.Value);
  60.  
  61.             Console.WriteLine("Animals:");
  62.             foreach (var item in food)
  63.             {
  64.                 Console.WriteLine(item.Key + " -> " + item.Value + "g");
  65.             }
  66.             Console.WriteLine("Areas with hungry animals:");
  67.             foreach (var item in area)
  68.             {
  69.                 Console.WriteLine(item.Value + " : " + area.Count);
  70.             }
  71.  
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment