alexbancheva

Problem_6_Students_part2._Лаб_Classes

Jul 13th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Problem_6_Students_part2._Лаб_Classes
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.  
  13.             List<Student> students = new List<Student>();
  14.  
  15.             while (input != "end")
  16.             {
  17.                 string[] data = input
  18.                     .Split(" ")
  19.                     .ToArray();
  20.  
  21.                 string firstName = data[0];
  22.                 string lastName = data[1];
  23.                 int age = int.Parse(data[2]);
  24.                 string hometown = data[3];
  25.  
  26.                 Student student = new Student();
  27.  
  28.                 if (IsExistingStudent(students, firstName, lastName))
  29.                 {
  30.  
  31.                     student = GetStudent(students, firstName, lastName);
  32.                     student.FirstName = firstName;
  33.                     student.LastName = lastName;
  34.                     student.Age = age;
  35.                     student.HomeTown = hometown;
  36.  
  37.                 }
  38.                 else
  39.                 {
  40.                     student.FirstName = firstName;
  41.                     student.LastName = lastName;
  42.                     student.Age = age;
  43.                     student.HomeTown = hometown;
  44.  
  45.                     students.Add(student);
  46.                 }
  47.                
  48.  
  49.                 input = Console.ReadLine();
  50.             }
  51.  
  52.             string homeTown = Console.ReadLine();
  53.  
  54.             List<Student> FiltredStudents = students
  55.                 .Where(x => x.HomeTown == homeTown)
  56.                 .ToList();
  57.  
  58.             foreach (Student student in FiltredStudents)
  59.             {
  60.                 Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  61.             }
  62.         }
  63.  
  64.         static bool IsExistingStudent(List<Student> students, string firstName, string lastName)
  65.         {
  66.  
  67.             foreach (Student student in students)
  68.             {
  69.                 if (firstName == student.FirstName && lastName == student.LastName)
  70.                 {
  71.                     return true;
  72.                 }
  73.             }
  74.  
  75.             return false;
  76.  
  77.  
  78.         }
  79.  
  80.         static Student GetStudent(List<Student> students, string firstName, string lastName)
  81.         {
  82.             Student existingStudent = null;
  83.  
  84.             foreach (Student student in students)
  85.             {
  86.                 if (firstName == student.FirstName && lastName == student.LastName)
  87.                 {
  88.                     existingStudent = student;
  89.                 }
  90.             }
  91.             return existingStudent;
  92.        
  93.         }
  94.  
  95.     }
  96.  
  97.  class Student
  98.     {
  99.         public string FirstName { get; set; }
  100.  
  101.         public string LastName { get; set; }
  102.  
  103.         public int Age { get; set; }
  104.  
  105.         public string HomeTown { get; set; }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment