Advertisement
RedFlys

Home work - stews

Jan 21st, 2022
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Database database = new Database();
  14.             database.Work();
  15.         }
  16.     }
  17.  
  18.     class Database
  19.     {
  20.         private List<Stew> _stews;
  21.         private int _thisYear;
  22.  
  23.         public Database()
  24.         {
  25.             _stews = new List<Stew>();
  26.             Fill();
  27.             _thisYear = 2022;
  28.         }
  29.  
  30.         public void Work()
  31.         {
  32.             bool isWorking = true;
  33.             List<Stew> stews = _stews;
  34.             int number;
  35.  
  36.             while(isWorking == true)
  37.             {
  38.                 Console.WriteLine("Тушёнка: ");
  39.                 ShowSelection(stews);
  40.                 Console.WriteLine("\n1. Найти просроченные.\n2. Выход.");
  41.                 Console.Write("Введите команду: ");
  42.                 number = GetNumber();
  43.  
  44.                 switch (number)
  45.                 {
  46.                     case 1:
  47.                         stews = FindExpiredStews();
  48.                         break;                                          
  49.  
  50.                     case 2:
  51.                         isWorking = false;
  52.                         break;
  53.  
  54.                     default:
  55.                         Console.WriteLine("Неверная команда.");
  56.                         break;
  57.                 }
  58.  
  59.                 Console.ReadKey();
  60.                 Console.Clear();
  61.             }
  62.         }
  63.  
  64.         private int GetNumber()
  65.         {
  66.             bool isConverted = false;
  67.             int number = 0;
  68.             string userInput;
  69.  
  70.             while (isConverted == false)
  71.             {
  72.                 userInput = Console.ReadLine();
  73.                 isConverted = Int32.TryParse(userInput, out number);
  74.             }
  75.  
  76.             return number;
  77.         }
  78.  
  79.         private List<Stew> FindExpiredStews()
  80.         {
  81.             var sortedStews = _stews.Where(stew => stew.ProductionYear + stew.BestBeforeate < _thisYear).ToList();
  82.  
  83.             return sortedStews;
  84.         }
  85.        
  86.         private void ShowSelection(List<Stew> stews)
  87.         {
  88.             foreach (Stew stew in stews)
  89.             {
  90.                 stew.ShowInfo();
  91.             }
  92.         }
  93.  
  94.         private void Fill()
  95.         {
  96.             _stews.Add(new Stew());
  97.             _stews.Add(new Stew());
  98.             _stews.Add(new Stew());
  99.             _stews.Add(new Stew());
  100.             _stews.Add(new Stew());
  101.             _stews.Add(new Stew());
  102.             _stews.Add(new Stew());
  103.             _stews.Add(new Stew());
  104.             _stews.Add(new Stew());
  105.             _stews.Add(new Stew());
  106.             _stews.Add(new Stew());
  107.             _stews.Add(new Stew());
  108.             _stews.Add(new Stew());
  109.         }
  110.     }
  111.  
  112.     class Stew
  113.     {
  114.         private static Random _random;
  115.         private NameStew _fullName;
  116.         private int _productionYear;
  117.         private int _bestBeforeate;
  118.  
  119.         static Stew()
  120.         {
  121.             _random = new Random();
  122.         }
  123.  
  124.         public Stew()
  125.         {
  126.             int maxBestBeforeate = 10;
  127.             int startYearProduced = 2010;
  128.             int finishYearProduced = 2020;
  129.             int countName = Enum.GetNames(typeof(NameStew)).Length;
  130.  
  131.             _fullName = (NameStew)_random.Next(countName);
  132.             _bestBeforeate = _random.Next(maxBestBeforeate);
  133.             _productionYear = _random.Next(startYearProduced, finishYearProduced);
  134.         }
  135.  
  136.         public int ProductionYear => _productionYear;
  137.         public int BestBeforeate => _bestBeforeate;
  138.  
  139.         public void ShowInfo()
  140.         {
  141.             Console.WriteLine($"{_fullName}. Дата игротовления: {_productionYear}. Срок годности: {_bestBeforeate}.");
  142.         }
  143.     }
  144.  
  145.     enum NameStew
  146.     {
  147.         OVA,
  148.         MainProduct,
  149.         Juicy,
  150.         Finnika
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement