TheBulgarianWolf

Oldest Family Member

Jan 15th, 2021
792
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.  
  5. namespace OldestFamilyMember
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Enter the number of members you want to enter: ");
  12.             int numberOfMembers = int.Parse(Console.ReadLine());
  13.             Family newFamily = new Family();
  14.             for (int i = 0; i < numberOfMembers; i++)
  15.             {
  16.                 string[] personInput = Console.ReadLine().Split(" ");
  17.                 string name = personInput[0];
  18.                 int age = int.Parse(personInput[1]);
  19.                 Person newPerson = new Person(name, age);
  20.                 newFamily.AddMember(newPerson);
  21.  
  22.             }
  23.             Person oldestPerson= newFamily.GetOldestMember(newFamily.FamilyMembers);
  24.             string oldestPersonName = oldestPerson.Name;
  25.             int oldestPersonAge = oldestPerson.Age;
  26.             Console.WriteLine("The oldest person is: {0} and is {1} years old.",oldestPersonName,oldestPersonAge);
  27.         }
  28.     }
  29.  
  30.     class Person
  31.     {
  32.         public Person(string name,int age)
  33.         {
  34.             Name = name;
  35.             Age = age;
  36.         }
  37.  
  38.         public string Name { get; set; }
  39.  
  40.         public int Age { get; set; }
  41.     }
  42.  
  43.     class Family
  44.     {
  45.         public Family()
  46.         {
  47.             FamilyMembers = new List<Person>();
  48.         }
  49.         public List<Person> FamilyMembers { get; set; }
  50.  
  51.         public void AddMember(Person member)
  52.         {
  53.             FamilyMembers.Add(member);
  54.         }
  55.  
  56.         public Person GetOldestMember(List<Person> familyMembers)
  57.         {
  58.             Person oldestMember = familyMembers.OrderByDescending(f => f.Age).First();
  59.             return oldestMember;
  60.         }
  61.     }
  62. }
  63.  
RAW Paste Data