Advertisement
TeT91

ДЗ Поиск преступника

Jun 24th, 2024 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CSLight
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             PersonBase person = new PersonBase();
  12.             person.ShowBanditsInfo();
  13.         }
  14.     }
  15.  
  16.     class PersonBase
  17.     {
  18.         private List<Person> _bandits;
  19.  
  20.         public PersonBase()
  21.         {
  22.             GenerateBandits();
  23.         }
  24.  
  25.         public void ShowBanditsInfo()
  26.         {
  27.             Console.WriteLine("Введите рост:");
  28.             float height = SetFloat();
  29.  
  30.             Console.WriteLine("Введите вес:");
  31.             float weight = SetFloat();
  32.  
  33.             Console.WriteLine("Введите национальность");
  34.             string nationality = Console.ReadLine();
  35.  
  36.             var filteredList = _bandits.Where(bandit => bandit.Height > height && bandit.Weight > weight && bandit.Nationality == nationality && bandit.IsFree);
  37.  
  38.             Console.WriteLine($"Найденые совпадения:");
  39.             ShowList(filteredList);
  40.  
  41.             Console.ReadKey();
  42.         }
  43.  
  44.         private void ShowList(IEnumerable<Person> list)
  45.         {
  46.             foreach (Person person in list)
  47.             {
  48.                     Console.WriteLine($"{person.Surname} {person.Name} {person.Patronymic}");
  49.             }
  50.         }
  51.  
  52.         private float SetFloat()
  53.         {
  54.             bool isRunnig = true;
  55.             float value = 0;
  56.  
  57.             while (isRunnig)
  58.             {
  59.                 string userInput = Console.ReadLine();
  60.  
  61.                 if (float.TryParse(userInput, out float result))
  62.                 {
  63.                     value = result;
  64.                     isRunnig = false;
  65.                 }
  66.                 else
  67.                 {
  68.                     Console.WriteLine("Неверный ввод");
  69.                 }
  70.             }
  71.  
  72.             return value;
  73.         }
  74.  
  75.         private void GenerateBandits()
  76.         {
  77.             PersonCreator creator = new PersonCreator();
  78.             int dataBaseCapacity = 20;
  79.  
  80.             _bandits = new List<Person>();
  81.  
  82.             for (int i = 0; i < dataBaseCapacity; i++)
  83.             {
  84.                 _bandits.Add(creator.CreatePerson());
  85.             }
  86.         }
  87.     }
  88.  
  89.  
  90.     class PersonCreator
  91.     {
  92.         private List<string> _names;
  93.         private List<string> _surnames;
  94.         private List<string> _patronymic;
  95.         private List<string> _nationalities;
  96.  
  97.         public PersonCreator()
  98.         {
  99.             InitNames();
  100.             InitSurnames();
  101.             InitPatronymics();
  102.             InitNationalities();
  103.         }
  104.  
  105.         public Person CreatePerson()
  106.         {
  107.             int minHeight = 1;
  108.             int maxHeight = 2;
  109.  
  110.             int minWeight = 56;
  111.             int maxWeight = 100;
  112.  
  113.             string name = GetRandomValue(_names);
  114.             string surname = GetRandomValue(_surnames);
  115.             string patronymic = GetRandomValue(_patronymic);
  116.             string nationalities = GetRandomValue(_nationalities);
  117.             float weight = UserUtils.GenerateRandomValue(minWeight, maxWeight) + (float)UserUtils.GenerateRandomDouble();
  118.             float height = UserUtils.GenerateRandomValue(minHeight, maxHeight) + (float)UserUtils.GenerateRandomDouble();
  119.  
  120.             return new Person(name, surname, patronymic, height, weight, nationalities, SetStatus());
  121.         }
  122.  
  123.         private void InitSurnames()
  124.         {
  125.             _surnames = new List<string>();
  126.             _surnames.Add("Смирнов");
  127.             _surnames.Add("Иванов");
  128.             _surnames.Add("Кузнецов");
  129.             _surnames.Add("Попов");
  130.             _surnames.Add("Соколов");
  131.             _surnames.Add("Лебедев");
  132.             _surnames.Add("Козлов");
  133.             _surnames.Add("Новиков");
  134.             _surnames.Add("Морозов");
  135.             _surnames.Add("Козлов");
  136.             _surnames.Add("Волков");
  137.         }
  138.  
  139.         private void InitPatronymics()
  140.         {
  141.             _patronymic = new List<string>();
  142.             _patronymic.Add("Максимович");
  143.             _patronymic.Add("Михаилович");
  144.             _patronymic.Add("Александрович");
  145.             _patronymic.Add("Дмитриевич");
  146.             _patronymic.Add("Денисович");
  147.             _patronymic.Add("Ильич");
  148.             _patronymic.Add("Андреевич");
  149.             _patronymic.Add("Артемович");
  150.             _patronymic.Add("Иванович");
  151.             _patronymic.Add("Алексеевич");
  152.             _patronymic.Add("Никитич");
  153.         }
  154.  
  155.         private void InitNames()
  156.         {
  157.             _names = new List<string>();
  158.             _names.Add("Максим");
  159.             _names.Add("Михаил");
  160.             _names.Add("Александр");
  161.             _names.Add("Дмитрий");
  162.             _names.Add("Денис");
  163.             _names.Add("Илья");
  164.             _names.Add("Андрей");
  165.             _names.Add("Артем");
  166.             _names.Add("Иван");
  167.             _names.Add("Алексей");
  168.             _names.Add("Никита");
  169.         }
  170.  
  171.         private void InitNationalities()
  172.         {
  173.             _nationalities = new List<string>();
  174.             _nationalities.Add("Русский");
  175.             _nationalities.Add("Не русский");
  176.         }
  177.  
  178.         private string GetRandomValue(List<string> values)
  179.         {
  180.             int randomId = UserUtils.GenerateRandomValue(values.Count);
  181.             return values[randomId];
  182.         }
  183.  
  184.         private bool SetStatus()
  185.         {
  186.             int chance = 50;
  187.             int maxChance = 100;
  188.  
  189.             return UserUtils.GenerateRandomValue(maxChance) > chance;
  190.         }
  191.     }
  192.  
  193.     class Person
  194.     {
  195.         public Person(string name, string surname, string patronymic, float height, float weight, string nationality, bool isFree)
  196.         {
  197.             Name = name;
  198.             Surname = surname;
  199.             Patronymic = patronymic;
  200.             Height = height;
  201.             Weight = weight;
  202.             Nationality = nationality;
  203.             IsFree = isFree;
  204.         }
  205.  
  206.         public string Name { get; private set; }
  207.  
  208.         public string Surname { get; private set; }
  209.  
  210.         public string Patronymic { get; private set; }
  211.  
  212.         public float Height { get; private set; }
  213.  
  214.         public float Weight { get; private set; }
  215.  
  216.         public string Nationality { get; private set; }
  217.  
  218.         public bool IsFree { get; private set; }
  219.     }
  220.  
  221.     static class UserUtils
  222.     {
  223.         private static Random s_random = new Random();
  224.  
  225.         public static int GenerateRandomValue(int maxValue)
  226.         {
  227.             return s_random.Next(maxValue);
  228.         }
  229.  
  230.         public static int GenerateRandomValue(int minValue, int maxValue)
  231.         {
  232.             return s_random.Next(minValue, maxValue);
  233.         }
  234.  
  235.         public static double GenerateRandomDouble()
  236.         {
  237.             return s_random.NextDouble();
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement