Advertisement
holllowknight

В больнице

May 1st, 2023
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Anarchy
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             const int CommandShowSortedByFullname = 1;
  12.             const int CommandShowSortedByAge = 2;
  13.             const int CommandShowWithDesease = 3;
  14.             const int CommandExit = 4;
  15.             bool isRunning = true;
  16.             Database database = new Database();
  17.  
  18.             Console.WriteLine($"{ CommandShowSortedByFullname} - Отсортировать всех больных по фио");
  19.             Console.WriteLine($"{ CommandShowSortedByAge} - Отсортировать всех больных по возрасту");
  20.             Console.WriteLine($"{ CommandShowWithDesease} - Вывести больных с определенным заболеванием");
  21.             Console.WriteLine($"{ CommandExit} - Закрыть программу");
  22.  
  23.             while (isRunning)
  24.             {
  25.                 Console.Write("\nВведите команду: ");
  26.                 int userInput = UserUtils.GetNumberFromUser();
  27.  
  28.                 switch (userInput)
  29.                 {
  30.                     case CommandShowSortedByFullname:
  31.                         database.ShowSortedByFullname();
  32.                         break;
  33.  
  34.                     case CommandShowSortedByAge:
  35.                         database.ShowSortedByAge();
  36.                         break;
  37.  
  38.                     case CommandShowWithDesease:
  39.                         database.ShowWithDesease();
  40.                         break;
  41.  
  42.                     case CommandExit:
  43.                         isRunning = false;
  44.                         break;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.  
  50.     class UserUtils
  51.     {
  52.         public static int GetNumberFromUser()
  53.         {
  54.             string userInput;
  55.             int userNumber;
  56.             bool isNumber = false;
  57.  
  58.             do
  59.             {
  60.                 userInput = Console.ReadLine();
  61.                 isNumber = int.TryParse(userInput, out userNumber);
  62.             }
  63.             while (isNumber == false);
  64.  
  65.             return userNumber;
  66.         }
  67.     }
  68.  
  69.     class Patient
  70.     {
  71.         public Patient(string fullname, int age, string disease)
  72.         {
  73.             Fullname = fullname;
  74.             Age = age;
  75.             Desease = disease;
  76.         }
  77.  
  78.         public string Fullname { get; private set; }
  79.         public int Age { get; private set; }
  80.         public string Desease { get; private set; }
  81.  
  82.         public void ShowInfo()
  83.         {
  84.             Console.WriteLine($"{Fullname} возраст:{Age}  заболевание:{Desease}");
  85.         }
  86.     }
  87.  
  88.     class Database
  89.     {
  90.         private List<Patient> _patients = new List<Patient>();
  91.  
  92.         public Database()
  93.         {
  94.             _patients.Add(new Patient("Сидоров Иван Иванович", 34, "коронавирус"));
  95.             _patients.Add(new Patient("Петров Пётр Петрович", 45, "грипп"));
  96.             _patients.Add(new Patient("Сидоров Сидор Сидорович", 64, "мигрень"));
  97.             _patients.Add(new Patient("Антонов Антон Антонович", 37, "грипп"));
  98.             _patients.Add(new Patient("Джигурда Валентин Исакевич", 73, "грипп"));
  99.             _patients.Add(new Patient("Кожедубов Артём Игоревич", 49, "анемия"));
  100.             _patients.Add(new Patient("Натулько Алексей Петрович", 15, "коронавирус"));
  101.             _patients.Add(new Patient("Карбонный Антон Петрович", 45, "мигрень"));
  102.             _patients.Add(new Patient("Филь Артём Александрович", 37, "грипп"));
  103.             _patients.Add(new Patient("Кожедубов Пётр Сидорович", 34, "коронавирус"));
  104.             _patients.Add(new Patient("Ларетти Антон Александрович", 56, "анемия"));
  105.             _patients.Add(new Patient("Ковальски Иван Моисеевич", 93, "анемия"));
  106.         }
  107.  
  108.         public void ShowPatients(IEnumerable<Patient> patients)
  109.         {
  110.             Console.WriteLine("");
  111.  
  112.             foreach (var patient in patients)
  113.                 patient.ShowInfo();
  114.         }
  115.  
  116.         public void ShowSortedByFullname()
  117.         {
  118.             Console.WriteLine("Отсортированы по ФИО:");
  119.             var patientsSortedByFullname = _patients.OrderBy(patient => patient.Fullname);
  120.             ShowPatients(patientsSortedByFullname);
  121.         }
  122.  
  123.         public void ShowSortedByAge()
  124.         {
  125.             Console.WriteLine("Отсортированы по возрасту:");
  126.             var patientsSortedByAge = _patients.OrderBy(patient => patient.Age);
  127.             ShowPatients(patientsSortedByAge);
  128.         }
  129.  
  130.         public void ShowWithDesease()
  131.         {
  132.             Console.WriteLine("Введите название заболевания:");
  133.             string desease = Console.ReadLine();
  134.             var patientsWithDesease = _patients.Where(patient => patient.Desease == desease);
  135.  
  136.             if (patientsWithDesease.Count() == 0)
  137.                 Console.WriteLine("Больных не найдено");
  138.             else
  139.                 ShowPatients(patientsWithDesease);
  140.         }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement