Askor

Hw41

Dec 20th, 2021 (edited)
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Hospital
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Hospital hospital = new Hospital();
  14.             hospital.Work();
  15.         }
  16.     }
  17.  
  18.     class Hospital
  19.     {
  20.         private List<Patient> _patients = new List<Patient>();
  21.  
  22.         public Hospital()
  23.         {
  24.             Fill();
  25.             SortByName();
  26.         }
  27.  
  28.         public void Work()
  29.         {
  30.             ConsoleKeyInfo consoleKeyInfo;
  31.  
  32.             Console.WriteLine("Press Any Button To Start");
  33.             consoleKeyInfo = Console.ReadKey();
  34.  
  35.             while (consoleKeyInfo.Key != ConsoleKey.Escape)
  36.             {
  37.                 Console.WriteLine("1 - Sort by Name \n2 - Sort by Age \n3 - Sort by Disease");
  38.  
  39.                 switch (Console.ReadLine())
  40.                 {
  41.                     case "1":
  42.                         ShowPatients(SortByName());
  43.                         break;
  44.                     case "2":
  45.                         ShowPatients(SortByAge());
  46.                         break;
  47.                     case "3":
  48.                         ShowPatients(SortByDisease());
  49.                         break;
  50.                     default:
  51.                         Console.WriteLine("Wrong action");
  52.                         break;
  53.                 }
  54.  
  55.                 Console.WriteLine("Press any button to continue");
  56.                 consoleKeyInfo = Console.ReadKey();
  57.                 Console.Clear();
  58.             }
  59.         }
  60.  
  61.         private List<Patient> SortByDisease()
  62.         {
  63.             string userInput;
  64.  
  65.             Console.Write("Inpute Disease: ");
  66.             userInput = Console.ReadLine();
  67.  
  68.             var filteredByDisease = _patients.Where(patient => patient.Disease.ToUpper() == userInput.ToUpper());
  69.  
  70.             if(filteredByDisease.Count() == 0)
  71.             {
  72.                 Console.WriteLine("There are no patients with such a disease");
  73.                 return null;
  74.             }
  75.             else
  76.             {
  77.                 return filteredByDisease.ToList<Patient>();
  78.             }
  79.         }
  80.  
  81.         private List<Patient> SortByAge()
  82.         {
  83.             var filteredByAge = _patients.OrderBy(patient => patient.Age);
  84.  
  85.             return filteredByAge.ToList<Patient>();
  86.         }
  87.  
  88.         private List<Patient> SortByName()
  89.         {
  90.             var filteredByName = _patients.OrderBy(patient => patient.FullName);
  91.  
  92.             return filteredByName.ToList<Patient>();
  93.         }
  94.        
  95.         private void ShowPatients(List<Patient> patients)
  96.         {
  97.             if (patients != null)
  98.             {
  99.                 foreach (var patient in patients)
  100.                 {
  101.                     patient.ShowInfo();
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private void Fill()
  107.         {
  108.             Random random = new Random();
  109.  
  110.             _patients.Add(new Patient("John White", random.Next(1,100), TypeOfDisease.Ascariasis));
  111.             _patients.Add(new Patient("Alister Miler", random.Next(1, 100), TypeOfDisease.Malaria));
  112.             _patients.Add(new Patient("Nevada Ruchini", random.Next(1, 100), TypeOfDisease.Pneumonia));
  113.             _patients.Add(new Patient("Moksi Bord", random.Next(1, 100), TypeOfDisease.Typhoid));
  114.             _patients.Add(new Patient("Luchina Petrov", random.Next(1, 100), TypeOfDisease.Amoebiasis));
  115.             _patients.Add(new Patient("Kobalt Seriy", random.Next(1, 100), TypeOfDisease.Pneumonia));
  116.             _patients.Add(new Patient("Java Float", random.Next(1, 100), TypeOfDisease.Cancer));
  117.             _patients.Add(new Patient("Waterovich Neptun", random.Next(1, 100), TypeOfDisease.Ascariasis));
  118.             _patients.Add(new Patient("Abdul Juhamba", random.Next(1, 100), TypeOfDisease.Pneumonia));
  119.             _patients.Add(new Patient("Hooma Sapiens", random.Next(1, 100), TypeOfDisease.Malaria));
  120.  
  121.         }
  122.     }
  123.  
  124.     class Patient
  125.     {
  126.         private TypeOfDisease _disease;
  127.         public string FullName { get; private set; }
  128.         public int Age { get; private set; }
  129.  
  130.         public string Disease => _disease.ToString();
  131.  
  132.         public Patient(string name, int age, TypeOfDisease disease)
  133.         {
  134.             FullName = name;
  135.             Age = age;
  136.             _disease = disease;
  137.         }
  138.  
  139.         public void ShowInfo()
  140.         {
  141.             Console.WriteLine($"Full Name - {FullName}, Age - {Age}, Disease - {Disease}");
  142.         }
  143.     }
  144.  
  145.     enum TypeOfDisease
  146.     {
  147.         Typhoid,
  148.         Pneumonia,
  149.         Malaria,
  150.         Amoebiasis,
  151.         Ascariasis,
  152.         Cancer
  153.     }
  154. }
  155.  
Add Comment
Please, Sign In to add comment