From c2a530340c1d1df6c5aabdecda44bb8643136640 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sun, 19 Mar 2023 13:05:55 +0200 Subject: [PATCH] Update Lesson 2 Task 1 --- Lesson_2/Task_1/Program.cs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Lesson_2/Task_1/Program.cs b/Lesson_2/Task_1/Program.cs index 058edc9..645f377 100644 --- a/Lesson_2/Task_1/Program.cs +++ b/Lesson_2/Task_1/Program.cs @@ -10,39 +10,33 @@ for (int i = 0; i < 10; i++) { string? name; float price; - string? userInput; do { - Console.Write(string.Format("Enter product {0} name: ", i + 1)); + Console.Write($"Enter product {i + 1} name: "); name = Console.ReadLine(); } while (name == null); do { - Console.Write(string.Format("Enter product {0} price: ", i + 1)); - userInput = Console.ReadLine(); - } while (!float.TryParse(userInput, out price)); + Console.Write($"Enter product {i + 1} price: "); + } while (!float.TryParse(Console.ReadLine(), out price)); products.Add(new Product(name, price)); } -products.Sort((x,y) => x.Price.CompareTo(y.Price)); - -Console.WriteLine("Sorted ascending: "); - -foreach (Product product in products) +for (int i = 0; i < 2; i++) { - Console.WriteLine(product); -} + bool descending = Convert.ToBoolean(i); -products.Sort((x,y) => y.Price.CompareTo(x.Price)); + products.Sort((first,second) => (descending ? second.Price.CompareTo(first.Price) : first.Price.CompareTo(second.Price))); -Console.WriteLine("Sorted descending: "); + Console.WriteLine($"Sorted {(descending ? "descending" : "ascending")}:"); -foreach (Product product in products) -{ - Console.WriteLine(product); + foreach (Product product in products) + { + Console.WriteLine(product); + } } class Product @@ -61,3 +55,4 @@ class Product Price = price; } } +