JulianJulianov

05.ObjectAndClasses-Students

Mar 5th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. 05.ObjectAndClasses-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. using System;
  24. using System.Collections.Generic;
  25. using System.Linq;
  26.  
  27. namespace _05Students
  28. {
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             var allStudents = new List<Student>();
  34.             var command = "";
  35.             while ((command = Console.ReadLine()) != "end")
  36.             {
  37.                 var array = command.Split();
  38.                 var firstNameStudent = array[0];
  39.                 var lastNameStudent = array[1];
  40.                 var ageStudent = array[2];
  41.                 var homeTown = array[3];
  42.  
  43.                 var createObjectStudent = new Student();
  44.                 createObjectStudent.FirstName = firstNameStudent;
  45.                 createObjectStudent.LastName = lastNameStudent;
  46.                 createObjectStudent.Age = ageStudent;
  47.                 createObjectStudent.HomeTown = homeTown;
  48.                 allStudents.Add(createObjectStudent);
  49.             }
  50.             var town = Console.ReadLine();
  51.             foreach (var student in allStudents)
  52.             {
  53.                 if (student.HomeTown == town)
  54.                 {
  55.                     Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  56.                 }
  57.             }
  58.            
  59.         }
  60.     }
  61.     class Student
  62.     {
  63.         public string FirstName { get; set; }
  64.         public string LastName { get; set; }
  65.         public string Age { get; set; }
  66.         public string HomeTown { get; set; }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment