Advertisement
yanchevilian

06. Students 2.0 C# Tech Module

Feb 22nd, 2021
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04._Students
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string command = Console.ReadLine();
  12.             List<Student> allStudents = new List<Student>();
  13.             while (command != "end")
  14.             {
  15.                 string[] inputInformation = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  16.                 string firstName = inputInformation[0];
  17.                 string lastName = inputInformation[1];
  18.                 int age = int.Parse(inputInformation[2]);
  19.                 string homeTown = inputInformation[3];
  20.                 Student student = allStudents.FirstOrDefault(s => s.FirstName == firstName && s.LastName == lastName);
  21.                 if (student == null)
  22.                 {
  23.                     allStudents.Add(new Student()
  24.                     {
  25.                         FirstName = firstName,
  26.                         LastName = lastName,
  27.                         Age = age,
  28.                         HomeTown = homeTown
  29.                     });
  30.                 }
  31.                 else
  32.                 {
  33.                     student.FirstName = firstName;
  34.                     student.LastName = lastName;
  35.                     student.Age = age;
  36.                     student.HomeTown = homeTown;
  37.                 }
  38.                 command = Console.ReadLine();
  39.             }
  40.             string town = Console.ReadLine();
  41.             List<Student> filteredStudents = allStudents.Where(currentStudent => currentStudent.HomeTown == town).ToList();
  42.  
  43.             foreach (Student student in filteredStudents)
  44.             {
  45.                 Console.WriteLine(student.getData());
  46.             }
  47.         }
  48.         class Student
  49.         {
  50.             public string FirstName { get; set; }
  51.             public string LastName { get; set; }
  52.             public int Age { get; set; }
  53.  
  54.             public string HomeTown { get; set; }
  55.  
  56.             public string getData()
  57.             {
  58.                 return $"{FirstName} {LastName} is {Age} years old.";
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement