Skip to content

Instantly share code, notes, and snippets.

@QRemark
Last active July 17, 2024 07:39
Show Gist options
  • Save QRemark/7e2f6deaa40c3784493a0079687f61d7 to your computer and use it in GitHub Desktop.
Save QRemark/7e2f6deaa40c3784493a0079687f61d7 to your computer and use it in GitHub Desktop.
Амнистия
using System;
using System.Collections.Generic;
using System.Linq;
namespace Linq
{
internal class Program
{
static void Main(string[] args)
{
HumanFactory humanFactory = new HumanFactory();
JusticeMinistry policeStation = new JusticeMinistry(humanFactory);
policeStation.Work();
}
}
class JusticeMinistry
{
private List<Human> _humans;
public JusticeMinistry(HumanFactory humanFactory)
{
_humans = humanFactory.Create();
}
public void Work()
{
const string CommandShowAllHuman = "1";
const string CommandMakeAmnesty = "2";
const string CommandExit = "3";
bool isRunning = true;
string userInput;
while (isRunning)
{
Console.WriteLine("Добро пожаловать в министерство Справедливости! \nВам доступно: ");
Console.WriteLine($"{CommandShowAllHuman} - посмотреть весь список виновных людей");
Console.WriteLine($"{CommandMakeAmnesty} - провести амнистию");
Console.WriteLine($"{CommandExit} - выйти из приложения");
Console.Write("Выберите действие: ");
userInput = Console.ReadLine();
switch (userInput)
{
case CommandShowAllHuman:
ShowAllHumans();
break;
case CommandMakeAmnesty:
MakeAmnesty();
break;
case CommandExit:
isRunning = false;
break;
default:
UserUtils.HandleInvalidInput();
break;
}
Console.Clear();
}
Console.WriteLine("Всего доброго! \nДля закрытия приложения нажмите любую кнопку...");
Console.ReadKey();
}
private void Show(List<Human> humans)
{
int number = 1;
UserUtils.ShowLine();
foreach (var human in humans)
{
Console.Write(number++ + ") ");
human.ShowStats();
}
UserUtils.ShowLine();
}
private void MakeAmnesty()
{
int defoultCount = _humans.Count;
Console.WriteLine("Вы решили провести амнистию. Вот первоначальный список: ");
Show(_humans);
_humans = _humans.Where(human => human.Crime != "Антиправительственное").ToList();
if (defoultCount > _humans.Count)
{
Console.WriteLine("Амнистия проведена. Люди, совершившие антиправительственные преступления, исключены из списка, вот обновленный список:");
Show(_humans);
}
else
{
Console.WriteLine("Амниситировать было некого. Все и так хорошо.");
}
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
private void ShowAllHumans()
{
Console.WriteLine("Вы выбрали посмотреть список всех людей, вот он: ");
Show(_humans);
Console.WriteLine("Для продолжения нажмите любую клавишу...");
Console.ReadKey();
}
}
class HumanFactory
{
public List<Human> Create()
{
List<Human> humans = new List<Human>
{
new Human("Кузнецов Дмитрий Владимирович", "Антиправительственное"),
new Human("Соколова Наталья Ивановна", "Экономическое"),
new Human("Михайлова Елена Викторовна", "Антисоциальное"),
new Human("Попова Ольга Сергеевна", "Антиправительственное"),
new Human("Морозова Мария Михайловна", "Экономическое"),
new Human("Иванов Иван Иванович", "Антиправительственное"),
new Human("Смирнова Анна Александровна", "Антисоциальное"),
new Human("Федоров Федр Федорвич", "Антиправительственное"),
new Human("Петров Петр Петрович", "Антисоциальное"),
new Human("Савельев Дмитрий Владимирович", "Антиправительственное"),
new Human("Пушкина Наталья Михайловна", "Антиправительственное"),
new Human("Ренн Елена Викторовна", "Антиправительственное"),
new Human("Тучкова Наталья Ивановна", "Экономическое")
};
return new List<Human>(humans);
}
}
class Human
{
public Human(string name, string crime)
{
Name = name;
Crime = crime;
}
public string Name { get; private set; }
public string Crime { get; private set; }
public void ShowStats()
{
Console.WriteLine($"ФИО: {Name}, преступление: {Crime}");
}
}
static class UserUtils
{
public static void HandleInvalidInput()
{
Console.Clear();
Console.WriteLine("Команда не была распознана, нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
public static void ShowLine() => Console.WriteLine("\n** " + new string('-', 40) + " **");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment