2023-03-15 16:47:53 +00:00
|
|
|
/*
|
|
|
|
* Lesson 2 Task 1: get a collection of ten products and their prices, then output them sorted by their price, both ascending and descending
|
|
|
|
* Author: Stanislav Mykhailenko
|
|
|
|
* License: Unlicense
|
|
|
|
*/
|
|
|
|
|
2023-03-20 00:14:49 +00:00
|
|
|
using ProductClass;
|
|
|
|
|
2023-03-15 16:47:53 +00:00
|
|
|
List<Product> products = new List<Product>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
2023-03-20 10:37:59 +00:00
|
|
|
string name;
|
2023-03-19 12:09:19 +00:00
|
|
|
float price;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Console.Write($"Enter product {i + 1} name: ");
|
2023-03-20 10:37:59 +00:00
|
|
|
string? input = Console.ReadLine();
|
|
|
|
if (!string.IsNullOrEmpty(input))
|
|
|
|
{
|
|
|
|
name = input;
|
2023-03-19 12:09:19 +00:00
|
|
|
break;
|
2023-03-20 10:37:59 +00:00
|
|
|
}
|
2023-03-19 12:09:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 10:37:59 +00:00
|
|
|
|
2023-03-19 12:09:19 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Console.Write($"Enter product {i + 1} price: ");
|
|
|
|
if (float.TryParse(Console.ReadLine(), out price))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
products.Add(new Product(name, price));
|
2023-03-15 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 11:05:55 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
2023-03-15 16:47:53 +00:00
|
|
|
{
|
2023-03-19 12:09:19 +00:00
|
|
|
bool descending = Convert.ToBoolean(i);
|
2023-03-15 16:47:53 +00:00
|
|
|
|
2023-03-19 12:09:19 +00:00
|
|
|
products.Sort((first,second) => (descending ? second.Price.CompareTo(first.Price) : first.Price.CompareTo(second.Price)));
|
2023-03-15 16:47:53 +00:00
|
|
|
|
2023-03-19 12:09:19 +00:00
|
|
|
Console.WriteLine($"Sorted {(descending ? "descending" : "ascending")}:");
|
2023-03-15 16:47:53 +00:00
|
|
|
|
2023-03-19 12:09:19 +00:00
|
|
|
foreach (Product product in products)
|
|
|
|
{
|
2023-03-20 15:09:43 +00:00
|
|
|
Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
|
2023-03-19 12:09:19 +00:00
|
|
|
}
|
2023-03-15 16:47:53 +00:00
|
|
|
}
|