Vapio

task40

Jun 21st, 2021 (edited)
870
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main(string[] args)
  8.     {
  9.         Hospital hospital = new Hospital();
  10.         bool isWork = true;
  11.  
  12.         while (isWork)
  13.             isWork = hospital.Update();
  14.     }
  15. }
  16.  
  17. public class Hospital
  18. {
  19.     private Random _random;
  20.     private List<Patient> _patients;
  21.  
  22.     private string[] _diseaseNames = { "Cold", "Diarrhea", "Covid", "Tuberculosis" };
  23.     private string[] _names = { "Mike", "Nick", "Valera", "Virgil", "Dante" };
  24.     private string[] _choicesNames = { "Order By Age.", "Order By Name.", "Order By Name of Disease.", "Exit." };
  25.  
  26.     private int _ageMinimum = 14;
  27.     private int _ageMaximum = 89;
  28.  
  29.     private int _amountPrisonersMinimum = 10;
  30.     private int _amountPrisonersMaximum = 50;
  31.  
  32.     public Hospital()
  33.     {
  34.         _patients = new List<Patient>();
  35.         _random = new Random();
  36.         CreatePatients();
  37.     }
  38.  
  39.     public bool Update()
  40.     {
  41.         int choice = InputChoice();
  42.         bool isWork = true;
  43.  
  44.         switch (choice)
  45.         {
  46.             case 0:
  47.                 OrderByAge();
  48.                 PrintPatients(_patients);
  49.                 break;
  50.             case 1:
  51.                 OrderByName();
  52.                 PrintPatients(_patients);
  53.                 break;
  54.             case 2:
  55.                 string diseaseName = InputDiseaseName();
  56.                 var patientsFiltered = OrderByDiseaseName(diseaseName);
  57.                 PrintPatients(patientsFiltered);
  58.                 break;
  59.             case 3:
  60.                 isWork = false;
  61.                 break;
  62.         }
  63.  
  64.         return isWork;
  65.     }
  66.  
  67.     private string InputDiseaseName()
  68.     {
  69.         string diseaseName = "";
  70.         bool isInput = true;
  71.  
  72.         do
  73.         {
  74.             PrintDesisease();
  75.             diseaseName = Console.ReadLine();
  76.             isInput = !HasDiseaseName(diseaseName);
  77.         }
  78.         while (isInput);
  79.  
  80.         return diseaseName;
  81.     }
  82.  
  83.     private int InputChoice()
  84.     {
  85.         int choice;
  86.         bool isInput = true;
  87.         bool isParse = false;
  88.  
  89.         do
  90.         {
  91.             PrintChoises();
  92.             isParse = int.TryParse(Console.ReadLine(), out choice);
  93.  
  94.             if (isParse && choice >= 0 && choice < _choicesNames.Length)
  95.                 isInput = false;
  96.         }
  97.         while (isInput);
  98.  
  99.         return choice;
  100.     }
  101.  
  102.     private bool HasDiseaseName(string disease)
  103.     {
  104.         bool hasDiseaseName = false;
  105.  
  106.         foreach (string diseaseName in _diseaseNames)
  107.         {
  108.             if (diseaseName.Equals(disease))
  109.             {
  110.                 hasDiseaseName = true;
  111.                 break;
  112.             }
  113.         }
  114.  
  115.         return hasDiseaseName;
  116.     }
  117.  
  118.     private void OrderByName()
  119.     {
  120.         _patients = _patients.OrderBy(patient => patient.Name).ToList();
  121.     }
  122.  
  123.     private void OrderByAge()
  124.     {
  125.         _patients = _patients.OrderBy(patient => patient.Age).ToList();
  126.     }
  127.  
  128.     private List<Patient> OrderByDiseaseName(string diseaseName)
  129.     {
  130.         return _patients.Where(pateint => pateint.DiseaseName.Equals(diseaseName)).ToList();
  131.     }
  132.  
  133.     private void PrintPatients(List<Patient> patients)
  134.     {
  135.         Console.WriteLine();
  136.  
  137.         foreach (Patient prisoner in patients)
  138.             Console.WriteLine(prisoner);
  139.     }
  140.  
  141.     private void PrintDesisease()
  142.     {
  143.         Console.WriteLine();
  144.  
  145.         foreach (string diseaseName in _diseaseNames)
  146.             Console.WriteLine(diseaseName);
  147.  
  148.         Console.WriteLine("Input name of disease : ");
  149.     }
  150.  
  151.     private void PrintChoises()
  152.     {
  153.         Console.WriteLine();
  154.  
  155.         for (int i = 0; i < _choicesNames.Length; ++i)
  156.             Console.WriteLine($"{i}. {_choicesNames[i]}");
  157.  
  158.         Console.WriteLine("Input your choice : ");
  159.     }
  160.  
  161.     private void CreatePatients()
  162.     {
  163.         int amountCriminals = _random.Next(_amountPrisonersMinimum, _amountPrisonersMaximum);
  164.  
  165.         string name;
  166.         string diseaseName;
  167.         int age;
  168.  
  169.         for (int i = 0; i < amountCriminals; ++i)
  170.         {
  171.             name = _names[_random.Next(_names.Length)];
  172.             diseaseName = _diseaseNames[_random.Next(_diseaseNames.Length)];
  173.             age = _random.Next(_ageMinimum, _ageMaximum);
  174.             _patients.Add(new Patient(name, diseaseName, age));
  175.         }
  176.     }
  177. }
  178.  
  179. public class Patient
  180. {
  181.     public string Name { get; private set; }
  182.     public int Age { get; private set; }
  183.     public string DiseaseName { get; private set; }
  184.  
  185.     public Patient(string name, string diseaseName, int age)
  186.     {
  187.         Name = name;
  188.         DiseaseName = diseaseName;
  189.         Age = age;
  190.     }
  191.  
  192.     public override string ToString()
  193.     {
  194.         string result = "";
  195.         result += String.Format($"{Name},{Age} - {DiseaseName}");
  196.         return result;
  197.     }
  198. }
RAW Paste Data