This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2023_Stanislav_Mykhailenko/Lesson_2/Task_2/Program.cs
Stanislav Mykhailenko b8010176e1
Update Lesson 2 Task 2
2023-03-19 13:09:58 +02:00

52 lines
1.1 KiB
C#

/*
* Lesson 2 Task 2: get a collection of users, then ask minimum and maximum search age and output the matching users
* Author: Stanislav Mykhailenko
* License: Unlicense
*/
List<Person> people = new List<Person>()
{
new Person("Tom",18),
new Person("Bob",26),
new Person("Sam",40),
new Person("Vadim",57),
new Person("Alex",80)
};
int minimum;
int maximum;
do
{
Console.Write("Enter minimum age: ");
} while (!int.TryParse(Console.ReadLine(), out minimum) || minimum < 0 || minimum > 130);
do
{
Console.Write("Enter maximum age: ");
userInput = Console.ReadLine();
} while (!int.TryParse(Console.ReadLine(), out maximum) || maximum < minimum || maximum > 130);
var selected = from person in people where person.Age >= minimum && person.Age <= maximum select person;
foreach (Person person in selected)
{
Console.WriteLine(person);
}
class Person
{
public string Name { get;}
public int Age { get;}
public override string ToString()
{
return "Name: " + Name + ", Age: " + Age;
}
public Person(string name, int age)
{
Name = name;
Age = age;
}
}