Vapio

task42

Jun 22nd, 2021 (edited)
418
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main(string[] args)
  8.     {
  9.         Shop shop = new Shop();
  10.         shop.ShowStewOutDateExpiration();
  11.     }
  12. }
  13.  
  14. public class Shop
  15. {
  16.     private Random _random;
  17.     private List<Stew> _stews;
  18.  
  19.     private int _yearNow;
  20.  
  21.     public Shop()
  22.     {
  23.         _stews = new List<Stew>();
  24.         _random = new Random();
  25.         _yearNow = 2021;
  26.         CreateStews();
  27.     }
  28.  
  29.     public void ShowStewOutDateExpiration()
  30.     {
  31.         List<Stew> stewsFiltered = _stews.Where(stew => _yearNow - stew.YearManafacture > stew.DateExpiration)
  32.                                          .ToList();
  33.         Console.WriteLine("Stew out of date of expiration : ");
  34.         PrintStews(stewsFiltered);
  35.     }
  36.  
  37.     private void CreateStews()
  38.     {
  39.         string[] names = { "Grodfood", "Snov", "Meatoholic", "Kalinkovichy", "Stolbcy" };
  40.  
  41.         int stewsAmountMinimum = 10;
  42.         int stewsAmountMaximum = 100;
  43.  
  44.         int yearMinimum = 1960;
  45.         int yearMaximum = 2021;
  46.  
  47.         int dateExpirationMinimum = 1;
  48.         int dateExpirationMaximum = 40;
  49.  
  50.         int amountStews = _random.Next(stewsAmountMinimum, stewsAmountMaximum);
  51.  
  52.         string name;
  53.         int year;
  54.         int dateExpiration;
  55.  
  56.         for (int i = 0; i < amountStews; ++i)
  57.         {
  58.             name = names[_random.Next(names.Length)];
  59.             year = _random.Next(yearMinimum, yearMaximum);
  60.             dateExpiration = _random.Next(dateExpirationMinimum, dateExpirationMaximum);
  61.             _stews.Add(new Stew(name, year, dateExpiration));
  62.         }
  63.     }
  64.  
  65.     private void PrintStews(List<Stew> stews)
  66.     {
  67.         Console.WriteLine();
  68.  
  69.         foreach (Stew stew in stews)
  70.             Console.WriteLine(stew);
  71.     }
  72. }
  73.  
  74. public class Stew
  75. {
  76.     public string Name { get; private set; }
  77.     public int YearManafacture { get; private set; }
  78.     public int DateExpiration { get; private set; }
  79.  
  80.     public Stew(string name, int yearManafacture, int dateExpiration)
  81.     {
  82.         Name = name;
  83.         YearManafacture = yearManafacture;
  84.         DateExpiration = dateExpiration;
  85.     }
  86.  
  87.     public override string ToString()
  88.     {
  89.         return String.Format($"{Name} stew ({YearManafacture}) - {DateExpiration} years.");
  90.     }
  91. }
RAW Paste Data