Update Lesson 2 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-03-19 13:05:55 +02:00
parent 584fd2c0d8
commit c2a530340c
GPG key ID: 1E95E66A9C9D6A36

View file

@ -10,39 +10,33 @@ for (int i = 0; i < 10; i++)
{ {
string? name; string? name;
float price; float price;
string? userInput;
do do
{ {
Console.Write(string.Format("Enter product {0} name: ", i + 1)); Console.Write($"Enter product {i + 1} name: ");
name = Console.ReadLine(); name = Console.ReadLine();
} while (name == null); } while (name == null);
do do
{ {
Console.Write(string.Format("Enter product {0} price: ", i + 1)); Console.Write($"Enter product {i + 1} price: ");
userInput = Console.ReadLine(); } while (!float.TryParse(Console.ReadLine(), out price));
} while (!float.TryParse(userInput, out price));
products.Add(new Product(name, price)); products.Add(new Product(name, price));
} }
products.Sort((x,y) => x.Price.CompareTo(y.Price)); for (int i = 0; i < 2; i++)
Console.WriteLine("Sorted ascending: ");
foreach (Product product in products)
{
Console.WriteLine(product);
}
products.Sort((x,y) => y.Price.CompareTo(x.Price));
Console.WriteLine("Sorted descending: ");
foreach (Product product in products)
{ {
bool descending = Convert.ToBoolean(i);
products.Sort((first,second) => (descending ? second.Price.CompareTo(first.Price) : first.Price.CompareTo(second.Price)));
Console.WriteLine($"Sorted {(descending ? "descending" : "ascending")}:");
foreach (Product product in products)
{
Console.WriteLine(product); Console.WriteLine(product);
}
} }
class Product class Product
@ -61,3 +55,4 @@ class Product
Price = price; Price = price;
} }
} }