JulianJulianov

15.ObjectsAndClasses-Order by Age

Mar 5th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. 15.ObjectsAndClasses-Order by Age
  2. You will receive an unknown number of lines. Each line will be consisted of an array of 3 elements. The first element will be a string and it will represent the name of a person. The second element will be a string and it will represent the ID of the person. The last element will be an integer - the age of the person. When you receive the command “End”, print all the people, ordered by age.
  3. Examples
  4. Input                                       Output
  5. Georgi 123456 20                            Stefan with ID: 524244 is 10 years old.
  6. Pesho 78911 15                              Pesho with ID: 78911 is 15 years old.
  7. Stefan 524244 10                            Georgi with ID: 123456 is 20 years old.
  8. End
  9.  
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16.  
  17. namespace Order_By_Age
  18. {
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             List<string> command = Console.ReadLine().Split().ToList();
  24.             List<dynamic> list = new List<dynamic>();//Създаване на обект "dynamic" без да нужно създаването на "class"!
  25.  
  26.             while (command[0] != "End")
  27.             {
  28.  
  29.                 string name = command[0].ToString();
  30.                 string id = command[1].ToString();
  31.                 int age = int.Parse(command[2]);
  32.                 dynamic person = new { name, id, age };//Така се избягва стандартния начин на създаване на "class"( с пропъртита)!
  33.  
  34.                 list.Add(person);
  35.                 command = Console.ReadLine().Split().ToList();
  36.  
  37.             }
  38.             if (command[0] == "End")
  39.             {
  40.                 var result = list.OrderBy(person => person.age);
  41.                 foreach (var item in result)
  42.                 {
  43.                     Console.WriteLine($"{item.name} with ID: {item.id} is {item.age} years old.");
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment