JulianJulianov

06.ObjectsAndClasses-Students 2.0

Mar 5th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. 06.ObjectsAndClasses-Students 2.0
  2. Use the class from the previous problem. If you receive a student, which already exists (first name and last name should be unique) overwrite the information.
  3. Input                                             Output
  4. John Smith 15 Sofia                               John Smith is 15 years old.
  5. Peter Ivanov 14 Plovdiv                           Linda Bridge is 27 years old.
  6. Peter Ivanov 25 Plovdiv
  7. Linda Bridge 16 Sofia
  8. Linda Bridge 27 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 26 years old.
  15. Jack Lewis 14 Chicago                             David Lee is 18 years old.
  16. David Lee 14 Chicago
  17. Jack Lewis 26 Chicago
  18. David Lee 18 Chicago
  19. end
  20. Chicago
  21.  
  22. Hints
  23. Check if the given student already exists.
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28.  
  29. namespace Students
  30. {
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             string command = "";
  36.             var students = new List<Student>();
  37.             while ((command = Console.ReadLine()) != "end")
  38.             {
  39.                 string[] array = command.Split();
  40.                 string firstName = array[0];
  41.                 string lastName = array[1];
  42.                 int age = int.Parse(array[2]);
  43.                 string city = array[3];
  44.  
  45.                 if (IsStudentExisting(students, firstName, lastName))
  46.                 {
  47.                     var student = GetStudent(students, firstName, lastName, age);
  48.                 }
  49.                 else
  50.                 {
  51.                     var student = new Student()
  52.                     {
  53.                         FirstName = firstName,
  54.                         LastName = lastName,
  55.                         Age = age,
  56.                         City = city,
  57.                     };
  58.                     students.Add(student);
  59.                 }
  60.             }
  61.             string filterCity = Console.ReadLine();
  62.             var filteredStudents = students.Where(s => s.City == filterCity).ToList();
  63.  
  64.             foreach (Student student in filteredStudents)
  65.             {
  66.  
  67.                 Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  68.             }
  69.         }
  70.  
  71.         static bool IsStudentExisting(List<Student> students, string firstName, string lastName)
  72.         {
  73.             foreach (Student student in students)
  74.             {
  75.                 if (student.FirstName == firstName && student.LastName == lastName)
  76.                 {
  77.                     return true;
  78.                 }
  79.             }
  80.             return false;
  81.         }
  82.  
  83.         static Student GetStudent(List<Student> students, string firstName, string lastName, int age)
  84.         {
  85.             Student existingStudent = null;
  86.             foreach (Student student in students)
  87.             {
  88.                 if (student.FirstName == firstName && student.LastName == lastName)
  89.                 {
  90.                     existingStudent = student;
  91.                     existingStudent.Age = age;
  92.                 }
  93.             }
  94.             return existingStudent;
  95.         }
  96.     }
  97.     class Student
  98.     {
  99.         public string FirstName { get; set; }
  100.         public string LastName { get; set; }
  101.         public int Age { get; set; }
  102.         public string City { get; set; }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment