Created
October 12, 2019 12:11
-
-
Save Retterath/bb5f4d9c97cd4e8134a5dc4d552f59e3 to your computer and use it in GitHub Desktop.
This is a little demonstration of how we can use classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Linq; | |
namespace DefiningClasses | |
{ | |
public class Family | |
{ | |
private List<Person> people; | |
public Family() | |
{ | |
this.people = new List<Person>(); | |
} | |
public void AddMember(Person person) | |
{ | |
this.people.Add(person); | |
} | |
public Person GetOldestMember() | |
=> people.OrderByDescending(a => a.Age).FirstOrDefault(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DefiningClasses | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
public class Person | |
{ | |
private string name; | |
private int age; | |
// The first should take no arguments | |
// The second should accept only an integer number for the age and produce a person with name & quot; No name" | |
// and age equal to the passed parameter. | |
// The third one should accept a string for the name and an integer for the age and should produce a person | |
// with the given name and age. | |
public Person() | |
{ | |
} | |
public Person(int age) | |
:this() // Im going to call ctor that is empty | |
{ | |
this.Age = age; | |
} | |
public Person(string name, int age) | |
:this(age) | |
{ | |
this.Name = name; | |
} | |
public string Name | |
{ | |
get { return this.name; } | |
set { this.name = value; } | |
} | |
public int Age | |
{ | |
get { return this.age; } | |
set { | |
this.age = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace FamilyReunion | |
{ | |
public class StartUp | |
{ | |
public static void Main(string[] args) | |
{ | |
Family family = new Family(); | |
int count = int.Parse(Console.ReadLine()); | |
for (int i = 0; i < count; i++) | |
{ | |
string[] personInfo = Console.ReadLine() | |
.Split() | |
.ToArray(); | |
string name = personInfo[0]; | |
int age = int.Parse(personInfo[1]); | |
Person person = new Person(name, age); | |
family.AddMember(person); | |
} | |
Person olders = family.GetOldestMember(); | |
Console.WriteLine($"{olders.Name} {olders.Age}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The programm checks how many family members are given from the user and then asks for detailed information.
Example input:
3
Jordan 23
Tom 21
Philip 46
Example output:
Philip 46