Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BelousovAD/3655e7f23112022dd54504dc2231146a to your computer and use it in GitHub Desktop.
Save BelousovAD/3655e7f23112022dd54504dc2231146a to your computer and use it in GitHub Desktop.
ДЗ: Определение просрочки
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharp
{
public class Program
{
private static void Main(string[] args)
{
List<CannedMeat> cannedMeats = new List<CannedMeat>()
{
new CannedMeat("Болгарская", 2000, 24),
new CannedMeat("Американская", 2015, 6),
new CannedMeat("Русская", 2020, 8),
new CannedMeat("Надёжная", 2002, 3),
new CannedMeat("Сочная", 2005, 25),
new CannedMeat("Равнины Алтая", 2008, 30),
new CannedMeat("Норвежская", 2010, 6),
new CannedMeat("Натуральная", 2018, 7),
new CannedMeat("Придворная", 2025, 5),
new CannedMeat("Луговая", 2025, 9)
};
Database database = new Database(cannedMeats);
DatabaseView databaseView = new DatabaseView(database);
Console.InputEncoding = System.Text.Encoding.Unicode;
Console.OutputEncoding = System.Text.Encoding.Unicode;
databaseView.Work();
}
}
public class Database
{
private IEnumerable<CannedMeat> _cannedMeats;
public Database(IEnumerable<CannedMeat> cannedMeats) =>
_cannedMeats = cannedMeats;
public IEnumerable<CannedMeat> CannedMeats =>
_cannedMeats;
public IEnumerable<CannedMeat> GetExpiredCannedMeat()
{
int currentYear = DateTime.Now.Year;
return _cannedMeats.Where(cannedMeat => cannedMeat.ProductionYear + cannedMeat.ShelfLife < currentYear);
}
}
public class DatabaseView : View
{
private readonly Database _database;
public DatabaseView(Database database)
{
_database = database;
MenuCommands = new List<Command>()
{
new Command(1, ShowCannedMeat, "Отобразить все консервы"),
new Command(2, ShowExpiredCannedMeat, "Отобразить просроченные консервы"),
new Command(3, Exit, "Выйти")
};
}
private void ShowCannedMeat() =>
ShowCannedMeat(_database.CannedMeats);
private void ShowCannedMeat(IEnumerable<CannedMeat> cannedMeats)
{
Console.WriteLine("Тушёнка:");
foreach (CannedMeat cannedMeat in cannedMeats)
{
Console.WriteLine($"\t{cannedMeat}");
}
}
private void ShowExpiredCannedMeat() =>
ShowCannedMeat(_database.GetExpiredCannedMeat());
}
public class CannedMeat
{
public CannedMeat(string name, int productionYear, int shelfLife)
{
Name = name;
ProductionYear = productionYear;
ShelfLife = shelfLife;
}
public string Name { get; }
public int ProductionYear { get; }
public int ShelfLife { get; }
public override string ToString()
{
return $"Наименование: {Name}, Год выпуска: {ProductionYear}, Срок хранения в годах: {ShelfLife}";
}
}
public class Command
{
private readonly Action _action;
private readonly string _description;
public Command(int id, Action action, string description)
{
Id = id;
_action = action;
_description = description;
}
public int Id { get; }
public void Execute() =>
_action?.Invoke();
public override string ToString() =>
$"{Id}. {_description}";
}
public class View
{
private List<Command> _menuCommands;
private bool _isWork = true;
public void Work()
{
while (_isWork && CheckWork())
{
ShowInfo();
ExecuteCommandFromUser();
}
}
protected List<Command> MenuCommands
{
set => _menuCommands = value;
}
protected virtual bool CheckWork() =>
true;
protected void Exit() =>
_isWork = false;
protected int ReadInt(string requestMessage)
{
bool isParsed;
int number;
do
{
Console.Write(requestMessage);
isParsed = int.TryParse(Console.ReadLine().Trim(), out number);
if (isParsed == false)
{
Console.WriteLine("Введённая последовательность символов не является числом или является слишком большим числом");
}
} while (isParsed == false);
return number;
}
protected string ReadString(string requestMessage)
{
Console.Write(requestMessage);
return Console.ReadLine().Trim();
}
protected virtual void ShowInfo()
{ }
private void ExecuteCommandFromUser()
{
Command command;
do
{
ShowMenu(_menuCommands.ToArray());
int commandId = ReadInt("Введите номер команды: ");
command = _menuCommands.Find(element => element.Id == commandId);
if (command == null)
{
Console.WriteLine("Такая команда отсутствует");
}
} while (command == null);
command.Execute();
}
private void ShowMenu(Command[] commands)
{
foreach (Command command in commands)
{
Console.WriteLine(command);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment