From f7a7591933d3b2d2d493f106c72d67927c5042ed Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Wed, 15 Mar 2023 19:29:32 +0200 Subject: [PATCH] Add Lesson 2 Task 3 --- Lesson_2/Task_3/Program.cs | 57 +++++++++++++++++++++++++++++++++++ Lesson_2/Task_3/Task_3.csproj | 10 ++++++ 2 files changed, 67 insertions(+) create mode 100644 Lesson_2/Task_3/Program.cs create mode 100644 Lesson_2/Task_3/Task_3.csproj diff --git a/Lesson_2/Task_3/Program.cs b/Lesson_2/Task_3/Program.cs new file mode 100644 index 0000000..030975b --- /dev/null +++ b/Lesson_2/Task_3/Program.cs @@ -0,0 +1,57 @@ +/* +* Lesson 2 Task 3: get a collection of customer, then ask for a name and output the matching users, and also count how many of the selected users are older than 18 +* Author: Stanislav Mykhailenko +* License: Unlicense +*/ + +List customers = new List() +{ + new Customer("Tom", 14, "Street 1"), + new Customer("Bob", 26, "Street 2"), + new Customer("Sam", 10, "Street 3"), + new Customer("Vadim", 57, "Street 4"), + new Customer("Alex", 80, "Street 5"), + new Customer("Alex", 15, "Street 6"), + new Customer("Tom", 65, "Street 7"), + new Customer("Andrew", 33, "Street 8"), + new Customer("Bob", 22, "Street 9"), + new Customer("Alex", 77, "Street 10") +}; + +string? name; + +do +{ + Console.Write("Enter name: "); + name = Console.ReadLine(); +} while (name == null); + +var selected = from s in customers where s.Name == name select s; + +foreach (Customer customer in selected) +{ + Console.WriteLine(customer); +} + +var number = selected.Where(s => s.Age >= 18).Count(); + +Console.WriteLine("Amount of customers older than 18: " + number); + +class Customer +{ + public string Name { get;} + public int Age { get;} + public string Address { get;} + + public override string ToString() + { + return "Name: " + Name + ", Age: " + Age + ", Address: " + Address; + } + + public Customer(string name, int age, string address) + { + Name = name; + Age = age; + Address = address; + } +} diff --git a/Lesson_2/Task_3/Task_3.csproj b/Lesson_2/Task_3/Task_3.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/Lesson_2/Task_3/Task_3.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + +