using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace restless { sealed class Product { private static int nowYear = Convert.ToInt32(DateTime.Today.Year); public string Name; public double Cost; public string BestBefore; public string Grade; public DateTime DateOfIssue; public int ShelfLife; public Product(string n, double c, string BB, string g, string DOI, int SL) { try { Name = n; Cost = c; BestBefore = BB; Grade = g; DateOfIssue = DateTime.ParseExact(DOI, "dd.MM.yy", null); ShelfLife = SL; } catch { Console.WriteLine("Обработчик иÑключений Ñработал в конÑтрукторе!"); } } public void PrintInfo() { Console.WriteLine($"Ðаименование товара:{Name}\nЦена товара:{Cost}\nСрок хранениÑ: {BestBefore}\nСорт:{Grade}\nДата выпуÑка:{DateOfIssue}\nСрок годноÑти:{ShelfLife}\n"); } public bool SearchRancid() { if ((DateOfIssue.AddMonths(ShelfLife)).Year == nowYear) { Console.WriteLine("У данного продукта Ñрок годноÑти иÑтекает в Ñтом году!"); PrintInfo(); return true; } return false; } } class Program { static void Main(string[] args) { /*Читаем информацию из файла*/ try { string line; Console.WriteLine("Ðаш ÑпиÑок продуктов из файла: \n"); List<Product> listOfProducts = new List<Product>(); StreamReader fileRead = new StreamReader(@"C:\Users\User\source\repos\Sharp2\test.txt"); while ((line = fileRead.ReadLine()) != null) { string[] words = line.Split(' '); listOfProducts.Add(new Product(words[0], Convert.ToDouble(words[1]), words[2], words[3], words[4], Convert.ToInt32(words[5]))); Console.WriteLine($"{words[0]} {words[1]} {words[2]} {words[3]} {words[4]} {words[5]}"); } fileRead.Close(); /*Загружаем результат Ð²Ñ‹Ð¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ Ð² файл*/ Console.WriteLine(); StreamWriter fileWrite = new StreamWriter(@"C:\Users\User\source\repos\Sharp2\output.txt"); fileWrite.WriteLine("СпиÑок продуктов, у которых Ñрок годноÑти кончаетÑÑ Ð² Ñтом году\n"); for (int i = 0; i < listOfProducts.Count; i++) { if (listOfProducts[i].SearchRancid()) { fileWrite.WriteLine($"Ðаименование товара:{listOfProducts[i].Name}\nЦена товара:{listOfProducts[i].Cost}\nСрок хранениÑ: {listOfProducts[i].BestBefore}\nСорт:{listOfProducts[i].Grade}\nДата выпуÑка:{listOfProducts[i].DateOfIssue}\nСрок годноÑти:{listOfProducts[i].ShelfLife}\n"); } } fileWrite.Close(); Console.ReadKey(); } catch { Console.WriteLine("Сработал универÑальный обработчик Ñобытий, что-то указано неверно!"); } } } }