From 0080761cdd49d47b690674e7140de7d73fd1d3ed Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Wed, 15 Mar 2023 19:12:01 +0200 Subject: [PATCH] Add Lesson 2 Task 2 --- Lesson_2/Task_2/Program.cs | 55 +++++++++++++++++++++++++++++++++++ Lesson_2/Task_2/Task_2.csproj | 10 +++++++ 2 files changed, 65 insertions(+) create mode 100644 Lesson_2/Task_2/Program.cs create mode 100644 Lesson_2/Task_2/Task_2.csproj diff --git a/Lesson_2/Task_2/Program.cs b/Lesson_2/Task_2/Program.cs new file mode 100644 index 0000000..9bd749b --- /dev/null +++ b/Lesson_2/Task_2/Program.cs @@ -0,0 +1,55 @@ +/* +* 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 people = new List() +{ + new Person("Tom",18), + new Person("Bob",26), + new Person("Sam",40), + new Person("Vadim",57), + new Person("Alex",80) +}; + +string? userInput; + +int minimum; +int maximum; + +do +{ + Console.Write("Enter minimum age: "); + userInput = Console.ReadLine(); +} while (!int.TryParse(userInput, out minimum) || minimum < 0 || minimum > 130); + +do +{ + Console.Write("Enter maximum age: "); + userInput = Console.ReadLine(); +} while (!int.TryParse(userInput, out maximum) || maximum < minimum || maximum > 130); + +var selected = from s in people where s.Age >= minimum && s.Age <= maximum select s; + +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; + } +} diff --git a/Lesson_2/Task_2/Task_2.csproj b/Lesson_2/Task_2/Task_2.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/Lesson_2/Task_2/Task_2.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + +