Advertisement
gospod1978

Object and Class/Student2

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