W1thr

LINQ-5

Jun 12th, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Hoomework5
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<StewCan> stewCans = new List<StewCan> { };
  12.  
  13.             for (int i = 0; i < 10; i++)
  14.             {
  15.                 stewCans.Add(new StewCan());
  16.             }
  17.  
  18.             Console.WriteLine("--Все Банки--");
  19.             foreach (var stewCan in stewCans)
  20.             {
  21.                 stewCan.ShowInfo();
  22.                 stewCan.Expire();
  23.             }
  24.  
  25.             var filteredStewCans = stewCans.Where(stewCan => stewCan.IsExpired == true);
  26.  
  27.             Console.WriteLine("--Просроченные Банки--");
  28.             foreach (var stewCan in filteredStewCans)
  29.             {
  30.                 stewCan.ShowInfo();
  31.             }
  32.         }
  33.     }
  34.  
  35.     class StewCan
  36.     {
  37.         private string _name;
  38.         private int _shelfLife;
  39.         private int _ageOfManufacture;
  40.         private readonly int _thisYear = 2021;
  41.         private static Random _random = new Random();
  42.  
  43.         public bool IsExpired { get; private set; }
  44.  
  45.         public StewCan(string name = "simple stew can")
  46.         {
  47.             _name = name;
  48.             _ageOfManufacture = _random.Next(1965, 2001);
  49.             _shelfLife = _random.Next(21, 60);
  50.         }
  51.  
  52.         public bool Expire()
  53.         {
  54.             IsExpired = _thisYear > (_ageOfManufacture + _shelfLife);
  55.             return IsExpired;
  56.         }
  57.  
  58.         public void ShowInfo()
  59.         {
  60.             Console.WriteLine($"Эта тушенка годится до {_ageOfManufacture + _shelfLife} года, а сейчас {_thisYear}.");
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment