Skip to content

Instantly share code, notes, and snippets.

@QRemark
Created July 17, 2024 12:11
Show Gist options
  • Save QRemark/73f086d5aa3b65205253630e5492387e to your computer and use it in GitHub Desktop.
Save QRemark/73f086d5aa3b65205253630e5492387e 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();
Hospital hospital = new Hospital(humanFactory);
hospital.Work();
}
}
class Hospital
{
private List<Human> _humans;
public Hospital(HumanFactory humanFactory)
{
_humans = humanFactory.Create();
}
public void Work()
{
const string CommandShowAllHuman = "1";
const string CommandSortByName = "2";
const string CommandSortByAge = "3";
const string CommandFindByDisease = "4";
const string CommandExit = "5";
bool isRunning = true;
string userInput;
while (isRunning)
{
Console.WriteLine("Добро пожаловать в веселую больницу! \nВам доступно: ");
Console.WriteLine($"{CommandShowAllHuman} - посмотреть весь список больных людей");
Console.WriteLine($"{CommandSortByName} - отсортировать список по ФИО");
Console.WriteLine($"{CommandSortByAge} - отсортировать список по возрасту от меньшего к большему");
Console.WriteLine($"{CommandFindByDisease} - найти по заболеванию");
Console.WriteLine($"{CommandExit} - выйти из приложения");
Console.Write("Выберите действие: ");
userInput = Console.ReadLine();
switch (userInput)
{
case CommandShowAllHuman:
ShowAllHumans();
break;
case CommandSortByName:
SortByName();
break;
case CommandSortByAge:
SortByAge();
break;
case CommandFindByDisease:
FindByDisease();
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 FindByDisease()
{
string diesease;
Console.WriteLine("Вы решили провести Поиск по названию заболевания. Введите его:");
diesease = Console.ReadLine();
var result = _humans.Where(human => human.Disease.ToLower() == diesease.ToLower()).ToList();
if (result.Count > 0)
{
Console.WriteLine("Список подходящих больных: ");
Show(result);
}
else
{
Console.WriteLine("Такой болезни нет, надо будет подумать над ее включением в список...");
}
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
private void SortByName()
{
Console.WriteLine("Вы решили провести Сортировку по ФИО: ");
Show(_humans);
var result = _humans.OrderBy(human => human.Name).ToList();
Console.WriteLine("Сортировка по ФИО произведена:");
Show(result);
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey();
}
private void SortByAge()
{
Console.WriteLine("Вы решили провести Сортировку по возрасту: ");
Show(_humans);
var result = _humans.OrderBy(human => human.Age).ToList();
Console.WriteLine("Сортировка по возрасту произведена:");
Show(result);
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("Кузнецов Дмитрий Владимирович", 48, "Лежебокитис обострённый"),
new Human("Соколова Наталья Ивановна", 11, "Кофейный синдром утреннего зомби"),
new Human("Михайлова Елена Викторовна", 92, "Шопоголизм безудержный"),
new Human("Попова Ольга Сергеевна", 60, "Сериаломания запущенная"),
new Human("Морозова Мария Михайловна", 31, "Обнимательный дефицит"),
new Human("Иванов Иван Иванович", 27, "Интернетозависимость хроническая"),
new Human("Смирнова Анна Александровна", 16, "Селфиит в квадрате"),
new Human("Федоров Федр Федорвич", 24, "Селфиит в квадрате"),
new Human("Петров Петр Петрович", 48, "Интернетозависимость хроническая"),
new Human("Савельев Дмитрий Владимирович", 61, "Обнимательный дефицит"),
new Human("Пушкина Наталья Михайловна", 90, "Сериаломания запущенная"),
new Human("Ренн Елена Викторовна", 24, "Шопоголизм безудержный"),
new Human("Тучкова Наталья Ивановна", 39, "Лежебокитис обострённый")
};
return new List<Human>(humans);
}
}
class Human
{
public Human(string name, int age, string disease)
{
Name = name;
Age = age;
Disease = disease;
}
public string Name { get; private set; }
public int Age { get; private set; }
public string Disease { get; private set; }
public void ShowStats()
{
Console.WriteLine($"ФИО: {Name}, Возраст: {Age}, Диагноз: {Disease}");
}
}
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