SHARE
TWEET

Oldest Member

Deserboy Nov 12th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace DefiningClasses
  8. {
  9.     class Person
  10.     {
  11.         private string name;
  12.         private int age;
  13.         public string Name
  14.         {
  15.             get
  16.             {
  17.                 return name;
  18.             }
  19.             set
  20.             {
  21.                 this.name = value;
  22.             }
  23.         }
  24.         public int Age
  25.         {
  26.             get
  27.             {
  28.                 return age;
  29.             }
  30.             set
  31.             {
  32.                 this.age = value;
  33.             }
  34.         }
  35.         public Person()
  36.         {
  37.             this.name = "No name";
  38.             this.age = 1;
  39.         }
  40.         public Person(int age) : this()
  41.         {
  42.             this.name = "No name";
  43.             this.age = age;
  44.         }
  45.         public Person(string name,int age) : this(age)
  46.         {
  47.             this.name = name;
  48.             this.age = age;
  49.         }
  50.     }
  51.     class Family
  52.     {
  53.         private List<Person> list = new List<Person>();
  54.         public void AddMember(Person member)
  55.         {
  56.             list.Add(member);
  57.         }
  58.         public Person GetOldestMember()
  59.         {
  60.             int highestAge = 1;
  61.             Person oldestMember = new Person();
  62.  
  63.             foreach (var member in list)
  64.             {
  65.                 int currentAge = member.Age;
  66.                 if (currentAge > highestAge)
  67.                 {
  68.                     highestAge = currentAge;
  69.                     oldestMember = member;
  70.                 }
  71.             }
  72.             return oldestMember;
  73.         }
  74.     }
  75.     class StartUp
  76.     {
  77.         static void Main(string[] args)
  78.         {
  79.             int n = int.Parse(Console.ReadLine());
  80.             Family family = new Family();
  81.             for (int i = 0;i < n; i++)
  82.             {
  83.                 string[] input = Console.ReadLine().Split().ToArray();
  84.                 Person person = new Person(input[0], int.Parse(input[1]));
  85.                 family.AddMember(person);
  86.             }
  87.             Person oldestPerson = family.GetOldestMember();
  88.             Console.WriteLine(oldestPerson.Name + " " + oldestPerson.Age);
  89.         }
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top