Deserboy

Object and Classes - Students 2.0 Error

Jul 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. namespace Objects_and_Classes
  6. {
  7.     class Student
  8.     {
  9.         public string FirstName { get; set; }
  10.         public string LastName { get; set; }
  11.         public int Age { get; set; }
  12.         public string Hometown { get; set; }
  13.     }
  14. }
  15. namespace Objects_and_Classes
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             List<Student> students = new List<Student>();
  22.  
  23.             string input = Console.ReadLine();
  24.             while (input != "end")
  25.             {
  26.                 string[] tokens = input.Split(" ");
  27.                 string firstName = tokens[0];
  28.                 string lastName = tokens[1];
  29.                 int age = int.Parse(tokens[2]);
  30.                 string city = tokens[3];
  31.                 if (students.Count > 0)
  32.                 {
  33.                     for (int i = 0; i < students.Count; i++)
  34.                     {
  35.                         if (firstName == students[i].FirstName && lastName == students[i].LastName)
  36.                         {
  37.                             students[students.Count - 1].Age = age;
  38.                             students[students.Count - 1].Hometown = city;
  39.                         }
  40.                         else
  41.                         {
  42.                             Student student = new Student();
  43.                             student.FirstName = firstName;
  44.                             student.LastName = lastName;
  45.                             student.Age = age;
  46.                             student.Hometown = city;
  47.                             students.Add(student);
  48.                             input = Console.ReadLine();
  49.                             break;
  50.                         }
  51.                     }
  52.                 }
  53.  
  54.                 input = Console.ReadLine();
  55.             }
  56.             string cityEnd = Console.ReadLine();
  57.             foreach (Student student in students)
  58.             {
  59.                 if (cityEnd == student.Hometown)
  60.                 {
  61.                     Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
Add Comment
Please, Sign In to add comment