Advertisement
JulianJulianov

06.ObjectAndClasses-Students 2.0

Feb 28th, 2020
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. 06. 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.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Linq;
  26.  
  27. namespace Students
  28. {
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             string command = "";
  34.             var students = new List<Student>();
  35.             while ((command = Console.ReadLine()) != "end")
  36.             {
  37.                 string[] array = command.Split();
  38.                 string firstName = array[0];
  39.                 string lastName = array[1];
  40.                 int age = int.Parse(array[2]);
  41.                 string city = array[3];
  42.  
  43.                 if (IsStudentExisting(students, firstName, lastName))
  44.                 {
  45.  
  46.                     var student = GetStudent(students, firstName, lastName, age);
  47.                 }
  48.                 else
  49.                 {
  50.                     var student = new Student()
  51.                     {
  52.                         FirstName = firstName,
  53.                         LastName = lastName,
  54.                         Age = age,
  55.                         City = city,
  56.  
  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
Advertisement