Skip to content

Instantly share code, notes, and snippets.

@QRemark
Last active July 16, 2024 16:04
Show Gist options
  • Save QRemark/e7b5b074ef3e963b0df71be9c1c253c2 to your computer and use it in GitHub Desktop.
Save QRemark/e7b5b074ef3e963b0df71be9c1c253c2 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();
PoliceStation policeStation = new PoliceStation(humanFactory);
policeStation.Work();
}
}
class PoliceStation
{
private List<Human> _humans;
public PoliceStation(HumanFactory humanFactory)
{
_humans = humanFactory.Create();
}
public void Work()
{
const string CommandShowAllHuman = "1";
const string CommandSerchHuman = "2";
const string CommandExit = "3";
bool isRunning = true;
string userInput;
while (isRunning)
{
Console.WriteLine("Здравствуйте! \nВам доступно: ");
Console.WriteLine($"{CommandShowAllHuman} - посмотреть весь список людей");
Console.WriteLine($"{CommandSerchHuman} - начать поиск людей по параметрам");
Console.WriteLine($"{CommandExit} - выйти из приложения");
Console.Write("Выберите действие: ");
userInput = Console.ReadLine();
switch (userInput)
{
case CommandShowAllHuman:
ShowAllHumans();
break;
case CommandSerchHuman:
SerchHuman();
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 SerchHuman()
{
int high;
int weight;
string nation;
Console.Clear();
Console.WriteLine("Вы решили найти человека по параметрам, введите их все: ");
Console.WriteLine("Введите рост искомого: ");
high = UserUtils.ReadInt(Console.ReadLine());
Console.WriteLine("Введите вес искомого: ");
weight = UserUtils.ReadInt(Console.ReadLine());
Console.WriteLine("Введите национальность искомого: ");
nation = Console.ReadLine();
var result = _humans.Where(human => human.High == high && human.Weight == weight && human.Nation.ToUpper() == nation.ToUpper() && human.IsArrested == false).ToList();
UserUtils.ShowLine();
if (result.Count > 0)
{
Console.WriteLine("По заданным параметрам найдены следующие люди: ");
Show(result);
}
else
{
Console.WriteLine("Люди с указанными параметрами не найдены.");
}
UserUtils.ShowLine();
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
private void ShowAllHumans()
{
Console.WriteLine("Вы выбрали посмотреть список всех людей, вот он: ");
Show(_humans);
Console.WriteLine("Для продолжения нажмите любую клавишу...");
Console.ReadKey();
}
}
class HumanFactory
{
private List<Human> _humans = new List<Human>();
public HumanFactory() {}
public List<Human> Create()
{
_humans.Add(new Human("Кузнецов Дмитрий Владимирович", false, 193, 110, "Француз"));
_humans.Add(new Human("Соколова Наталья Ивановна", false, 159, 61, "Француз"));
_humans.Add(new Human("Михайлова Елена Викторовна", false, 210, 99, "Немец"));
_humans.Add(new Human("Попова Ольга Сергеевна", false, 155, 48, "Англичанин"));
_humans.Add(new Human("Морозова Мария Михайловна", true, 161, 58, "Немец"));
_humans.Add(new Human("Иванов Иван Иванович", true, 193, 110, "Француз"));
_humans.Add(new Human("Смирнова Анна Александровна", true, 159, 61, "Француз"));
_humans.Add(new Human("Федоров Федр Федорвич", true, 210, 99, "Немец"));
_humans.Add(new Human("Петров Петр Петрович", true, 155, 48, "Англичанин"));
_humans.Add(new Human("Савельев Дмитрий Владимирович", false, 193, 110, "Француз"));
_humans.Add(new Human("Пушкина Наталья Михайловна", false, 159, 61, "Француз"));
_humans.Add(new Human("Ренн Елена Викторовна", false, 210, 99, "Немец"));
_humans.Add(new Human("Тучкова Наталья Ивановна", false, 155, 48, "Англичанин"));
return new List<Human>(_humans);
}
}
class Human
{
public Human(string name, bool isArrested, int high, int weight, string nation)
{
Name = name;
IsArrested = isArrested;
High = high;
Weight = weight;
Nation = nation;
}
public string Name { get; private set; }
public bool IsArrested { get; private set; }
public int High { get; private set; }
public int Weight { get; private set; }
public string Nation { get; private set; }
public void ShowStats()
{
Console.WriteLine($"ФИО: {Name}, арестован(а): {IsArrested}, рост: {High}, вес: {Weight}, национальность: {Nation}");
}
}
static class UserUtils
{
public static void HandleInvalidInput()
{
Console.Clear();
Console.WriteLine("Команда не была распознана, нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
public static int ReadInt(string userInput)
{
int number;
while (int.TryParse(userInput, out number) == false)
{
Console.WriteLine("Вы ввели не число, введите число: ");
userInput = Console.ReadLine();
}
return number;
}
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