Advertisement
Vlad_Savitskiy

Stew

Jun 30th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CSLightFirst
  6. {
  7.     class Program
  8.     {
  9.         private static void Main()
  10.         {
  11.             List<Stew> stewSet = GenerateStewSet();
  12.  
  13.             while (true)
  14.             {
  15.                 Console.WriteLine("Выберите, что Вам нужно сделать:\n" +
  16.                                   "  1 - Показать просроченную тушенку\n" +
  17.                                   "  2 - Показать весь набор тушенки\n");
  18.                 Console.Write("Ваш ответ: ");
  19.  
  20.                 switch (Console.ReadLine())
  21.                 {
  22.                     case "1":
  23.                         MakeShelfLifeSelection(stewSet);
  24.                         break;
  25.                     case "2":
  26.                         ShowSelection(stewSet, "Набор тушенки состоит из:\n");
  27.                         break;
  28.                     default:
  29.                         Console.WriteLine("Некорректное введенное значение, попробуйте ещё раз...");
  30.                         break;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         private static void MakeShelfLifeSelection(IEnumerable<Stew> stewSet)
  36.         {
  37.             int currentYear = 2020;
  38.  
  39.             ShowSelection(stewSet.Where(stew => stew.ProductionYear + stew.ShelfLife < currentYear),
  40.                 "Просроченная тушенка:\n");
  41.         }
  42.  
  43.         private static void ShowSelection(IEnumerable<Stew> selection, string text = "")
  44.         {
  45.             Console.Clear();
  46.             Console.WriteLine(text);
  47.  
  48.             foreach (Stew stew in selection)
  49.                 stew.ShowInfo();
  50.         }
  51.  
  52.         private static List<Stew> GenerateStewSet()
  53.         {
  54.             Random rand = new Random();
  55.  
  56.             string[] names = { "Честный продукт", "БАРС", "Главпродукт", "ГРОДФУД", "Ремит" };
  57.  
  58.             List<Stew> stewSet = new List<Stew>(rand.Next(5, 10));
  59.  
  60.             for (int i = 0; i < stewSet.Capacity; i++)
  61.                 stewSet.Add(new Stew(
  62.                     names[rand.Next(0, names.Length)],
  63.                     rand.Next(2016, 2019),
  64.                     rand.Next(1, 6)));
  65.  
  66.             return stewSet;
  67.         }
  68.     }
  69.  
  70.     class Stew
  71.     {
  72.         public string Name { get; }
  73.         public int ProductionYear { get; }
  74.         public int ShelfLife { get; }
  75.  
  76.         public Stew(string name, int productionYear, int shelfLife)
  77.         {
  78.             Name = name;
  79.             ProductionYear = productionYear;
  80.             ShelfLife = shelfLife;
  81.         }
  82.  
  83.         public void ShowInfo()
  84.         {
  85.             Console.WriteLine($"  Тушенка: {Name}\n" +
  86.                               $"  Год производства: {ProductionYear}\n" +
  87.                               $"  Срок годности: {ShelfLife}\n");
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement