Advertisement
TeT91

ДЗ Определение просрочки

Jun 26th, 2024
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CSLight
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Storage storage = new Storage();
  12.             storage.ShowDamagedProducts();
  13.         }
  14.     }
  15.  
  16.     class Storage
  17.     {
  18.         private ProductCreator _productCreator;
  19.         private List<Product> _products;
  20.  
  21.         public Storage()
  22.         {
  23.             _productCreator = new ProductCreator();
  24.             _products = new List<Product>();
  25.  
  26.             GenerateProducts();
  27.         }
  28.  
  29.         public void ShowDamagedProducts()
  30.         {
  31.             foreach (Product product in GetDamagedProducts())
  32.             {
  33.                 Console.WriteLine($"{product.Name} - {product.ProductionDate}");
  34.             }
  35.  
  36.             Console.ReadKey();
  37.         }
  38.  
  39.         private IEnumerable<Product> GetDamagedProducts()
  40.         {
  41.             int currentYear = 2024;
  42.  
  43.             var filteredList = _products.Where(product => currentYear - product.ProductionDate >= product.ExpirationDate);
  44.  
  45.             return filteredList;
  46.         }
  47.  
  48.         private void GenerateProducts()
  49.         {
  50.             int productsCount = 20;
  51.             _products = new List<Product>();
  52.  
  53.             for (int i = 0; i < productsCount; i++)
  54.             {
  55.                 Product product = _productCreator.CreateProduct();
  56.                 _products.Add(product);
  57.             }
  58.         }
  59.     }
  60.  
  61.     class ProductCreator
  62.     {
  63.         private string[] _names;
  64.  
  65.         public ProductCreator()
  66.         {
  67.             InitNames();
  68.         }
  69.  
  70.         public Product CreateProduct()
  71.         {
  72.             int nameId = UserUtils.GenerateRandomValue(_names.Length);
  73.  
  74.             int minYear = 1990;
  75.             int maxYear = 2024;
  76.  
  77.             string name = _names[nameId];
  78.             int productionYear = UserUtils.GenerateRandomValue(minYear, maxYear);
  79.  
  80.             return new Product(name, productionYear);
  81.         }
  82.  
  83.         private void InitNames()
  84.         {
  85.             int namesCount = 5;
  86.  
  87.             _names = new string[namesCount];
  88.  
  89.             for (int i = 0; i < namesCount; i++)
  90.             {
  91.                 _names[i] = $"Тушенка {i}";
  92.             }
  93.         }
  94.     }
  95.  
  96.     class Product
  97.     {
  98.         public Product(string name, int productionDate)
  99.         {
  100.             Name = name;
  101.             ProductionDate = productionDate;
  102.             ExpirationDate = 5;
  103.         }
  104.  
  105.         public string Name { get; private set; }
  106.  
  107.         public int ProductionDate { get; private set; }
  108.  
  109.         public int ExpirationDate { get; private set; }
  110.     }
  111.  
  112.     static class UserUtils
  113.     {
  114.         private static Random s_random = new Random();
  115.  
  116.         public static int GenerateRandomValue(int minValue, int maxValue)
  117.         {
  118.             return s_random.Next(minValue, maxValue);
  119.         }
  120.  
  121.         public static int GenerateRandomValue(int maxValue)
  122.         {
  123.             return s_random.Next(maxValue);
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement