Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Storage storage = new Storage();
- storage.ShowDamagedProducts();
- }
- }
- class Storage
- {
- private ProductCreator _productCreator;
- private List<Product> _products;
- public Storage()
- {
- _productCreator = new ProductCreator();
- _products = new List<Product>();
- GenerateProducts();
- }
- public void ShowDamagedProducts()
- {
- foreach (Product product in GetDamagedProducts())
- {
- Console.WriteLine($"{product.Name} - {product.ProductionDate}");
- }
- Console.ReadKey();
- }
- private IEnumerable<Product> GetDamagedProducts()
- {
- int currentYear = 2024;
- var filteredList = _products.Where(product => currentYear - product.ProductionDate >= product.ExpirationDate);
- return filteredList;
- }
- private void GenerateProducts()
- {
- int productsCount = 20;
- _products = new List<Product>();
- for (int i = 0; i < productsCount; i++)
- {
- Product product = _productCreator.CreateProduct();
- _products.Add(product);
- }
- }
- }
- class ProductCreator
- {
- private string[] _names;
- public ProductCreator()
- {
- InitNames();
- }
- public Product CreateProduct()
- {
- int nameId = UserUtils.GenerateRandomValue(_names.Length);
- int minYear = 1990;
- int maxYear = 2024;
- string name = _names[nameId];
- int productionYear = UserUtils.GenerateRandomValue(minYear, maxYear);
- return new Product(name, productionYear);
- }
- private void InitNames()
- {
- int namesCount = 5;
- _names = new string[namesCount];
- for (int i = 0; i < namesCount; i++)
- {
- _names[i] = $"Тушенка {i}";
- }
- }
- }
- class Product
- {
- public Product(string name, int productionDate)
- {
- Name = name;
- ProductionDate = productionDate;
- ExpirationDate = 5;
- }
- public string Name { get; private set; }
- public int ProductionDate { get; private set; }
- public int ExpirationDate { get; private set; }
- }
- static class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomValue(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- public static int GenerateRandomValue(int maxValue)
- {
- return s_random.Next(maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement