using System;
namespace ConsoleApp7;
class Program
{
static void Main(string[] args)
{
List<Animal> zoo1Animals = new List<Animal>()
{
new Animal("медведь", true),
new Animal("гепард", true)
};
Zoo zoo1 = new Zoo("зоопарк1", zoo1Animals);
List<Animal> zoo2Animals = new List<Animal>()
{
new Animal("волк", true),
new Animal("гиена", true),
new Animal("ворон", false)
};
Zoo zoo2 = new Zoo("зоопарк2", zoo2Animals);
List<Zoo> zoos = new List<Zoo> { zoo1, zoo2 };
//Console.WriteLine($"{zoo1.Title}: {zoo1.Animals.Count} животных");
//foreach (var a in zoo1.Animals)
//{
// Console.WriteLine($"{a.Name}");
//}
//Console.WriteLine($"\n{zoo2.Title}: {zoo2.Animals.Count} животных");
//foreach (var a in zoo2.Animals)
//{
// Console.WriteLine($"{a.Name}");
//}
Zoo zooWithAtLeast3Animals = zoos.First(z => z.Animals.Count >= 3);
Console.WriteLine($"зоопарк с 3: {zooWithAtLeast3Animals.Title}");
Zoo zooWithAllPredators = zoos.First(z => z.Animals.All(a => a.IsPredator));
Console.WriteLine($"\nзоопарк с хищниками: {zooWithAllPredators.Title}");
string searchTitle = "зоопарк2";
Zoo zooWithSpecificName = zoos.First(z => z.Title == searchTitle);
Console.WriteLine($"\nзоопарк с названием '{searchTitle}': {zooWithSpecificName.Title}");
var predatorsInZoo1 = zoo1.Animals.Where(a => a.IsPredator == true);
foreach (var animal in predatorsInZoo1)
{
Console.WriteLine($" {animal.Name} - хищник");
}
var herbivoresInZoo2 = zoo2.Animals.Where(a => a.IsPredator == false);
if (herbivoresInZoo2.Any())
{
foreach (var animal in herbivoresInZoo2)
{
Console.WriteLine($" {animal.Name} - травоядное");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
public class Animal
{
public string Name { get; set; }
public bool IsPredator { get; set; }
public Animal(string name, bool isPredator)
{
this.Name = name;
this.IsPredator = isPredator;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
public class Zoo
{
public string Title { get; set; }
public List<Animal> Animals { get; set; } = new List<Animal>();
public Zoo(string title, List<Animal> animals)
{
this.Title = title;
this.Animals = animals;
}
}
}