Vapio

task38

Jun 21st, 2021
551
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.         Detective detective = new Detective();
  10.         detective.SearchCriminals();
  11.     }
  12. }
  13.  
  14. public class Detective
  15. {
  16.     private Random _random;
  17.     private List<Criminal> _criminals;
  18.  
  19.     private string[] _nationalities = { "American", "Englishman", "Canadian", "Russian", "Belarussian", "Ukranian"};
  20.     private string[] _names = { "Mike", "Nick", "Valera", "Virgil", "Dante"};
  21.    
  22.     private int _heightMinimum = 140;
  23.     private int _heightMaximum = 210;
  24.    
  25.     private int _weightMinimum = 45;
  26.     private int _weightMaximum = 180;
  27.    
  28.     private int _percentRange = 100;
  29.     private int _percentPrisoner = 50;
  30.  
  31.     private int _amountCriminalsMinimum = 10;
  32.     private int _amountCriminalMaximum = 50;
  33.  
  34.     public Detective()
  35.     {
  36.         _criminals = new List<Criminal>();
  37.         _random = new Random();
  38.         CreateCriminals();
  39.     }
  40.  
  41.     public void SearchCriminals()
  42.     {
  43.         bool isInput = true;
  44.         bool isWeightParse;
  45.         bool isHeightParse;
  46.         string nationality;
  47.         int height;
  48.         int weight;
  49.  
  50.         do
  51.         {
  52.             Console.Clear();
  53.             PrintNationalities();
  54.             nationality = Console.ReadLine();
  55.             PrintHeight();
  56.             isHeightParse = int.TryParse(Console.ReadLine(), out height);
  57.             PrintWeight();
  58.             isWeightParse = int.TryParse(Console.ReadLine(), out weight);
  59.  
  60.             if (HasNationality(nationality) && isWeightParse && isHeightParse)
  61.                 isInput = false;
  62.         }
  63.         while (isInput);
  64.  
  65.         var criminalsFiltered = from Criminal criminal in _criminals
  66.                                 where criminal.Nationality.Equals(nationality) &&
  67.                                 criminal.Weight == weight &&
  68.                                 criminal.Height == height &&
  69.                                 criminal.IsPrisoner == false
  70.                                 select criminal;
  71.  
  72.         Console.WriteLine("Searched criminals : ");
  73.  
  74.         foreach (var criminal in criminalsFiltered)
  75.             Console.WriteLine(criminal);
  76.     }
  77.  
  78.     private void PrintNationalities()
  79.     {
  80.         Console.WriteLine("List nationalities : ");
  81.  
  82.         foreach (string nationality in _nationalities)
  83.             Console.WriteLine(nationality);
  84.  
  85.         Console.WriteLine("Input a nationality");
  86.     }
  87.  
  88.     private void PrintHeight()
  89.     {
  90.         Console.WriteLine($"Minimum height : {_heightMinimum}");
  91.         Console.WriteLine($"Maximum height : {_heightMaximum}");
  92.         Console.WriteLine("Input a height");
  93.     }
  94.  
  95.     public void PrintWeight()
  96.     {
  97.         Console.WriteLine($"Minimum weight : {_weightMinimum}");
  98.         Console.WriteLine($"Maximum weight : {_weightMaximum}");
  99.         Console.WriteLine("Input a weight");
  100.     }
  101.  
  102.     private bool HasNationality(string nation)
  103.     {
  104.         bool isInArray = false;
  105.        
  106.         foreach (string nationality in _nationalities)
  107.         {
  108.             if (nationality.Equals(nation))
  109.             {
  110.                 isInArray = true;
  111.                 return isInArray;
  112.             }
  113.         }
  114.  
  115.         return isInArray;
  116.     }
  117.  
  118.     private void CreateCriminals()
  119.     {
  120.         int amountCriminals = _random.Next(_amountCriminalsMinimum, _amountCriminalMaximum);
  121.  
  122.         string name;
  123.         string nationality;
  124.         int weight;
  125.         int height;
  126.         bool isPrisoner;
  127.  
  128.         for (int i = 0; i < amountCriminals; ++i)
  129.         {
  130.             name = _names[_random.Next(_names.Length)];
  131.             nationality = _nationalities[_random.Next(_nationalities.Length)];
  132.             weight = _random.Next(_weightMinimum, _weightMaximum);
  133.             height = _random.Next(_heightMinimum, _heightMaximum);
  134.             isPrisoner = _random.Next(_percentRange) > _percentPrisoner;
  135.             _criminals.Add(new Criminal(name, isPrisoner, nationality, height, weight));
  136.         }
  137.     }
  138. }
  139.  
  140. public class Criminal
  141. {
  142.     public bool IsPrisoner { get; private set; }
  143.     public string Name { get; private set; }
  144.     public string Nationality { get; private set; }
  145.     public int Height { get; private set; }
  146.     public int Weight { get; private set; }
  147.  
  148.     public Criminal(string name, bool isPrisoner, string nationality, int height, int weight)
  149.     {
  150.         Name = name;
  151.         IsPrisoner = isPrisoner;
  152.         Nationality = nationality;
  153.         Height = height;
  154.         Weight = weight;
  155.     }
  156.  
  157.     public override string ToString()
  158.     {
  159.         string result = "";
  160.         result += String.Format($"{Name} - {Nationality}, {Height} m, {Weight} kg, is in prison : {IsPrisoner}");
  161.         return result;
  162.     }
  163. }
RAW Paste Data