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("Сработал универсальный обработчик событий, что-то указано неверно!");
            }
        }
    }
}