Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp24
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool exit = false;
- int presentYear = 2020;
- List<CannedFood> cannedFoods = new List<CannedFood>();
- CreateFoods(cannedFoods, presentYear);
- while(exit == false)
- {
- Console.Clear();
- Console.WriteLine($"Текущий год: {presentYear} \nПоказать список продуктов на складе - нажмите 1\nВывести список непросроченных продуктов - нажмите 2\nДля выхода - нажмите 3");
- bool correctInput = Int32.TryParse(Console.ReadLine(), out int resultInput);
- if (correctInput)
- {
- switch (resultInput)
- {
- case 1:
- ShowProducts(cannedFoods);
- break;
- case 2:
- var freshProducts = cannedFoods.Where(cannedfood => cannedfood.YearOfManufacture + cannedfood.ProductShelfLife >= presentYear);
- ShowProducts(freshProducts);
- break;
- case 3:
- exit = true;
- break;
- default:
- Error();
- break;
- }
- }
- else
- Error();
- }
- }
- static void CreateFoods(List<CannedFood> cannedFoods, int presentyear)
- {
- Random randomYear = new Random();
- for (int i = 0; i < 10; i++)
- {
- cannedFoods.Add(new CannedMeat(randomYear.Next(presentyear - 10, presentyear)));
- cannedFoods.Add(new CannedFish(randomYear.Next(presentyear - 10, presentyear)));
- }
- }
- static void ShowProducts(IEnumerable<CannedFood> filterResult)
- {
- foreach (var product in filterResult)
- {
- product.ShowProduct();
- }
- Console.ReadKey();
- }
- static void Error()
- {
- Console.WriteLine($"Вы ввели неверную команду или число - попробуйте еще раз.");
- Console.ReadKey();
- }
- }
- class CannedFood
- {
- public string Name { get; private set; }
- public int YearOfManufacture { get; private set; }
- public int ProductShelfLife { get; private set; }
- public CannedFood(string name, int yearOfManufacture, int productShelfLife)
- {
- Name = name;
- YearOfManufacture = yearOfManufacture;
- ProductShelfLife = productShelfLife;
- }
- public void ShowProduct()
- {
- Console.WriteLine($"Наименование консервы: {Name} | Год производства: {YearOfManufacture} | Срок годности: {ProductShelfLife} года");
- }
- }
- class CannedMeat : CannedFood
- {
- static private string _name = "Тущенка";
- static private int _productShelfLife = 6;
- public CannedMeat(int yearOfManufacture) : base(_name, yearOfManufacture, _productShelfLife) { }
- }
- class CannedFish : CannedFood
- {
- static private string _name = "Консервы";
- static private int _productShelfLife = 3;
- public CannedFish(int yearOfManufacture) : base(_name, yearOfManufacture, _productShelfLife) { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement