Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace Problem_6_Students_part2._Лаб_Classes
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- List<Student> students = new List<Student>();
- while (input != "end")
- {
- string[] data = input
- .Split(" ")
- .ToArray();
- string firstName = data[0];
- string lastName = data[1];
- int age = int.Parse(data[2]);
- string hometown = data[3];
- Student student = new Student();
- if (IsExistingStudent(students, firstName, lastName))
- {
- student = GetStudent(students, firstName, lastName);
- student.FirstName = firstName;
- student.LastName = lastName;
- student.Age = age;
- student.HomeTown = hometown;
- }
- else
- {
- student.FirstName = firstName;
- student.LastName = lastName;
- student.Age = age;
- student.HomeTown = hometown;
- students.Add(student);
- }
- input = Console.ReadLine();
- }
- string homeTown = Console.ReadLine();
- List<Student> FiltredStudents = students
- .Where(x => x.HomeTown == homeTown)
- .ToList();
- foreach (Student student in FiltredStudents)
- {
- Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
- }
- }
- static bool IsExistingStudent(List<Student> students, string firstName, string lastName)
- {
- foreach (Student student in students)
- {
- if (firstName == student.FirstName && lastName == student.LastName)
- {
- return true;
- }
- }
- return false;
- }
- static Student GetStudent(List<Student> students, string firstName, string lastName)
- {
- Student existingStudent = null;
- foreach (Student student in students)
- {
- if (firstName == student.FirstName && lastName == student.LastName)
- {
- existingStudent = student;
- }
- }
- return existingStudent;
- }
- }
- class Student
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- public string HomeTown { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment