Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Collections.Generic;
- namespace _03
- {
- public class Animals
- {
- public Animals() { }
- public Animals(string name, int foodLimitDaily, string area)
- {
- Name = name;
- FoodLimitDaily = foodLimitDaily;
- Area = area;
- }
- public string Name { get; set; }
- public int FoodLimitDaily { get; set; }
- public string Area { get; set; }
- public Animals Feed(Animals animal, List<Animals> collection, string name, int food)
- {
- animal.FoodLimitDaily -= food;
- if (animal.FoodLimitDaily <= 0)
- {
- Console.WriteLine($"{animal.Name} was successfully fed");
- collection.Remove(animal);
- }
- return animal;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- List<Animals> animalCollection = new List<Animals>();
- Animals instance = new Animals();
- string cmd = Console.ReadLine();
- while (!cmd.Contains("EndDay"))
- {
- if (cmd.StartsWith("Add: "))
- {
- cmd = cmd.Remove(0, 5);
- string[] split = cmd.Split("-").ToArray();
- string name = split[0];
- int foodQuantity = int.Parse(split[1]);
- string area = split[2];
- Animals existingAnimal = animalCollection.FirstOrDefault(x => x.Name == name);
- if (existingAnimal == null)
- {
- animalCollection.Add(new Animals(name, foodQuantity, area));
- }
- else
- {
- existingAnimal.FoodLimitDaily += foodQuantity;
- }
- }
- if (cmd.StartsWith("Feed: "))
- {
- cmd = cmd.Remove(0, 6);
- string[] split = cmd.Split("-").ToArray();
- string name = split[0];
- int food = int.Parse(split[1]);
- Animals animal = animalCollection.FirstOrDefault(x => x.Name == name);
- if (animal != null)
- {
- animal = instance.Feed(animal, animalCollection, name, food);
- }
- }
- cmd = Console.ReadLine();
- }
- Console.WriteLine("Animals:");
- foreach (Animals an in animalCollection.OrderByDescending(x => x.FoodLimitDaily).ThenBy(x => x.Name))
- {
- Console.WriteLine($" {an.Name} -> {an.FoodLimitDaily}g");
- }
- animalCollection = animalCollection.Where(x => x.FoodLimitDaily > 0).ToList();
- string temp = string.Empty;
- Console.WriteLine("Areas with hungry animals:");
- foreach (Animals an in animalCollection.OrderByDescending(x => animalCollection.Where(x => x.FoodLimitDaily > 0).Count()).ThenBy(x => x.Area))
- {
- if (temp == null || temp != an.Area)
- {
- Console.WriteLine($" {an.Area}: {animalCollection.Where(x => x.Area == an.Area).Count()}");
- temp = an.Area;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment