JulianJulianov

05.ObjectsAndClasses-Students

Feb 28th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. 05.ObjectsAndClasses-Students
  2. Define a class Student, which holds the following information about students: first name, last name, age and hometown.
  3. Read a list of students until you receive the "end" command. After that, you will receive a name of a city. Print only students, which are from the given city in the following format: "{firstName} {lastName} is {age} years old.".
  4. Examples
  5. Input                          Output
  6. John Smith 15 Sofia            John Smith is 15 years old.
  7. Peter Ivanov 14 Plovdiv        Linda Bridge is 16 years old.
  8. Linda Bridge 16 Sofia
  9. Simon Stone 12 Varna
  10. end
  11. Sofia  
  12.  
  13. Anthony Taylor 15 Chicago       Anthony Taylor is 15 years old.
  14. David Anderson 16 Washington    Jack Lewis is 14 years old.
  15. Jack Lewis 14 Chicago           David Lee is 14 years old.
  16. David Lee 14 Chicago
  17. end
  18. Chicago
  19.  
  20. Solution
  21. Define a class student with the following properties: FirstName, LastName, Age and City.
  22.  
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27.  
  28. namespace _05Students
  29. {
  30.     class Program
  31.     {
  32.         static void Main(string[] args)
  33.         {
  34.             var allStudents = new List<Student>();
  35.             var command = "";
  36.             while ((command = Console.ReadLine()) != "end")
  37.             {
  38.                 var array = command.Split();
  39.                 var firstNameStudent = array[0];
  40.                 var lastNameStudent = array[1];
  41.                 var ageStudent = array[2];
  42.                 var homeTown = array[3];
  43.  
  44.                 var createObjectStudent = new Student();
  45.                 createObjectStudent.FirstName = firstNameStudent;
  46.                 createObjectStudent.LastName = lastNameStudent;
  47.                 createObjectStudent.Age = ageStudent;
  48.                 createObjectStudent.HomeTown = homeTown;
  49.                 allStudents.Add(createObjectStudent);
  50.             }
  51.             var town = Console.ReadLine();
  52.             foreach (var student in allStudents)
  53.             {
  54.                 if (student.HomeTown == town)
  55.                 {
  56.                     Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  57.                 }
  58.             }
  59.            
  60.         }
  61.     }
  62.     class Student
  63.     {
  64.         public string FirstName { get; set; }
  65.         public string LastName { get; set; }
  66.         public string Age { get; set; }
  67.         public string HomeTown { get; set; }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment