Advertisement
cyecize

Animals

Jun 15th, 2019
4,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. /*
  2.     Примерен вход
  3.     3
  4.     Bear, 2, 200, Sofia
  5.     Machka, 1, 2, Mihailovgrad
  6.     Koza, 4, 30, Berkovica
  7.  
  8.  
  9.     exit -> exit   
  10.  
  11.     3 -> Броя на животни за попълване
  12.     Формат на попълване: Тип на животно, години, тегло, местоположение.
  13.         * СЪС ЗАПЕТАЯ И ИНТЕРВАЛ!
  14.    
  15.     След това може да напишеш тип животно и ще ти принтира информация за този тип.
  16.     За да се излезе от програмата, напиши Exit.
  17. */
  18.  
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using Microsoft.SqlServer.Server;
  26.  
  27. namespace TestSharp
  28. {
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             const string bear = "Bear";
  34.             var wildLife = new Dictionary<string, List<Animal>> { [bear] = new List<Animal>() };
  35.  
  36.             Console.WriteLine("Please enter the number of animals you are about to enter");
  37.  
  38.             var numOfEntries = int.Parse(Console.ReadLine());
  39.  
  40.             for (int i = 0; i < numOfEntries; i++)
  41.             {
  42.                 Console.WriteLine("Enter animal in the following format: Animal Type, Age, Wight, Location");
  43.                 var input = Console.ReadLine().Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  44.  
  45.                 var type = input[0];
  46.                 var age = int.Parse(input[1]);
  47.                 var weight = double.Parse(input[2]);
  48.                 var location = input[3];
  49.  
  50.                 var animal = new Animal(type, age, weight, location);
  51.  
  52.                 if (!wildLife.ContainsKey(type)) wildLife[type] = new List<Animal>();
  53.  
  54.  
  55.                 wildLife[type].Add(animal);
  56.             }
  57.  
  58.             Console.WriteLine("Data was populated!" + Environment.NewLine);
  59.             Console.WriteLine($"The number of animals of type Bear: {wildLife["Bear"].Count}");
  60.  
  61.             while (true)
  62.             {
  63.                 Console.WriteLine("Enter the type of animal or type exit to exit.");
  64.                 var input = Console.ReadLine();
  65.  
  66.                 if (input == "exit") break;
  67.  
  68.                 if (wildLife.ContainsKey(input))
  69.                     wildLife[input].ForEach(Console.WriteLine);
  70.                 else
  71.                     Console.WriteLine("Invalid animal type!");
  72.             }
  73.         }
  74.     }
  75.  
  76.     class Animal
  77.     {
  78.         public string AnimalType { get; set; }
  79.  
  80.         public int Age { get; set; }
  81.  
  82.         public double Weight { get; set; }
  83.  
  84.         public string Location { get; set; }
  85.  
  86.         public Animal(string type, int age, double weight, string location)
  87.         {
  88.             this.AnimalType = type;
  89.             this.Age = age;
  90.             this.Weight = weight;
  91.             this.Location = location;
  92.         }
  93.  
  94.         public override string ToString()
  95.         {
  96.             return $"Type: {this.AnimalType}, Age: {this.Age}, Weight: {this.Weight}, Location: {this.Location}";
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement