Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace DefinitionOverdue
- {
- internal class Program
- {
- static void Main()
- {
- Storage storage = new Storage();
- storage.Work();
- }
- }
- public class Storage
- {
- private readonly StorageBase _storageBase;
- public Storage()
- {
- _storageBase = new StorageBase();
- }
- public void Work()
- {
- List<Stew> stews = _storageBase.Fill();
- _storageBase.Show(stews);
- Console.WriteLine("\nBad stews:");
- List<Stew> badStews = _storageBase.Sort(stews);
- _storageBase.Show(badStews);
- Console.ReadKey();
- }
- }
- public class StorageBase
- {
- public List<Stew> Fill()
- {
- StewFactory stewFactory = new StewFactory();
- return stewFactory.Create();
- }
- public void Show(List<Stew> stews)
- {
- foreach (Stew stew in stews)
- {
- if (stew.Date.AddMonths(stew.ShelfLife) < DateTime.Today)
- {
- Console.ForegroundColor = ConsoleColor.DarkRed;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- }
- Console.WriteLine($"{stew.Name}\tmanufactured: {stew.Date.ToShortDateString()}\tshelf life: {stew.ShelfLife} months");
- Console.ResetColor();
- }
- }
- public List<Stew> Sort(List<Stew> stews)
- {
- return stews.Where(stew => stew.Date.AddMonths(stew.ShelfLife) < DateTime.Today).ToList();
- }
- }
- public class StewFactory
- {
- private readonly List<string> _names;
- private readonly List<int> _shelfLife;
- private readonly Random _random;
- public StewFactory()
- {
- _random = new Random();
- _names = new List<string>() { "Cow stew", "Pork stew", "Dog stew" };
- _shelfLife = new List<int>() { 12, 24, 30 };
- }
- public List<Stew> Create()
- {
- List<Stew> stews = new List<Stew>();
- int stewCount = 20;
- for (int i = 0; i < stewCount; i++)
- {
- stews.Add(new Stew(GetValue(_names), GetDate(), GetValue(_shelfLife)));
- }
- return stews;
- }
- private T GetValue<T>(List<T> values)
- {
- return values[_random.Next(values.Count)];
- }
- private DateTime GetDate()
- {
- int yearsPeriod = -3;
- DateTime startProductionDate = DateTime.Today.AddYears(yearsPeriod);
- var daysPeriod = DateTime.Today - startProductionDate;
- return startProductionDate.AddDays(_random.Next((int)daysPeriod.TotalDays));
- }
- }
- public class Stew
- {
- public Stew(string name, DateTime date, int shelfLife)
- {
- Name = name;
- Date = date;
- ShelfLife = shelfLife;
- }
- public string Name { get; private set; }
- public DateTime Date { get; private set; }
- public int ShelfLife { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement