Advertisement
Nemesis_War

Food

Aug 26th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp24
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool exit = false;
  14.             int presentYear = 2020;
  15.             List<CannedFood> cannedFoods = new List<CannedFood>();
  16.             CreateFoods(cannedFoods, presentYear);
  17.             while(exit == false)
  18.             {
  19.                 Console.Clear();
  20.                 Console.WriteLine($"Текущий год: {presentYear} \nПоказать список продуктов на складе - нажмите 1\nВывести список непросроченных продуктов - нажмите 2\nДля выхода - нажмите 3");
  21.                 bool correctInput = Int32.TryParse(Console.ReadLine(), out int resultInput);
  22.                 if (correctInput)
  23.                 {
  24.                     switch (resultInput)
  25.                     {
  26.                         case 1:
  27.                             ShowProducts(cannedFoods);
  28.                             break;
  29.                         case 2:
  30.                             var freshProducts = cannedFoods.Where(cannedfood => cannedfood.YearOfManufacture + cannedfood.ProductShelfLife >= presentYear);
  31.                             ShowProducts(freshProducts);
  32.                             break;
  33.                         case 3:
  34.                             exit = true;
  35.                             break;
  36.                         default:
  37.                             Error();
  38.                             break;
  39.                     }
  40.                 }
  41.                 else
  42.                     Error();
  43.             }
  44.         }
  45.  
  46.         static void CreateFoods(List<CannedFood> cannedFoods, int presentyear)
  47.         {
  48.             Random randomYear = new Random();
  49.             for (int i = 0; i < 10; i++)
  50.             {
  51.                 cannedFoods.Add(new CannedMeat(randomYear.Next(presentyear - 10, presentyear)));
  52.                 cannedFoods.Add(new CannedFish(randomYear.Next(presentyear - 10, presentyear)));
  53.             }
  54.         }
  55.  
  56.         static void ShowProducts(IEnumerable<CannedFood> filterResult)
  57.         {
  58.             foreach (var product in filterResult)
  59.             {
  60.                 product.ShowProduct();
  61.             }
  62.             Console.ReadKey();
  63.         }
  64.  
  65.         static void Error()
  66.         {
  67.             Console.WriteLine($"Вы ввели неверную команду или число - попробуйте еще раз.");
  68.             Console.ReadKey();
  69.         }
  70.     }
  71.  
  72.     class CannedFood
  73.     {
  74.         public string Name { get; private set; }
  75.         public int YearOfManufacture { get; private set; }
  76.         public int ProductShelfLife { get; private set; }
  77.         public CannedFood(string name, int yearOfManufacture, int productShelfLife)
  78.         {
  79.             Name = name;
  80.             YearOfManufacture = yearOfManufacture;
  81.             ProductShelfLife = productShelfLife;
  82.         }
  83.  
  84.         public void ShowProduct()
  85.         {
  86.             Console.WriteLine($"Наименование консервы: {Name} | Год производства: {YearOfManufacture} | Срок годности: {ProductShelfLife} года");
  87.         }
  88.  
  89.     }
  90.  
  91.     class CannedMeat : CannedFood
  92.     {
  93.         static private string _name = "Тущенка";
  94.         static private int _productShelfLife = 6;
  95.         public CannedMeat(int yearOfManufacture) : base(_name, yearOfManufacture, _productShelfLife) { }
  96.     }
  97.  
  98.     class CannedFish : CannedFood
  99.     {
  100.         static private string _name = "Консервы";
  101.         static private int _productShelfLife = 3;
  102.         public CannedFish(int yearOfManufacture) : base(_name, yearOfManufacture, _productShelfLife) { }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement