Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LinqTask5ExpiredCannedFood
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- FoodStore foodStore = new FoodStore();
- foodStore.ShowExpiredCannedFoods();
- }
- }
- public class FoodStore
- {
- const int YearNow = 2024;
- private List<CannedFood> _cannedFoods = new List<CannedFood>();
- public FoodStore()
- {
- FillCannedFoods();
- }
- public void ShowExpiredCannedFoods()
- {
- Console.WriteLine("Все консервы:");
- ShowCannedFoods(_cannedFoods);
- List<CannedFood> expiredCannedFoods = _cannedFoods.Where(
- cannedFood => cannedFood.BirthYear + cannedFood.LifeLength < YearNow).ToList();
- Console.WriteLine("\nИз них просроченные:");
- ShowCannedFoods(expiredCannedFoods);
- Console.WriteLine();
- }
- private void ShowCannedFoods(List<CannedFood> cannedFoods)
- {
- foreach(var cannedFood in cannedFoods)
- {
- cannedFood.ShowInfo();
- }
- }
- private void FillCannedFoods()
- {
- const int MinimumBirthYear = 2000;
- const int MinimumLifeLength = 3;
- const int MaximumLifeLength = 20;
- string[] names = new string[] {
- "коровка",
- "барашка",
- "курочка",
- "индеечка",
- "сосиська",
- "свинное ушко",
- "собачья жопка",
- "кошачий язык",
- "мышиные яйца",
- "голубиные головы",
- "китовые жабры",
- "дреска",
- "птеродактель"
- };
- Random random = new Random();
- foreach (string name in names)
- {
- _cannedFoods.Add(new CannedFood(name,
- random.Next(MinimumBirthYear, YearNow),
- random.Next(MinimumLifeLength, MaximumLifeLength)));
- }
- }
- }
- public class CannedFood
- {
- private string _name;
- public CannedFood(string name, int birthYear, int lifeLength)
- {
- _name = name;
- BirthYear = birthYear;
- LifeLength = lifeLength;
- }
- public int BirthYear { get; private set; }
- public int LifeLength { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{_name}, год производства: {BirthYear}, срок годности: {LifeLength}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement