Update Lesson 2 Task 1
This commit is contained in:
parent
584fd2c0d8
commit
c2a530340c
1 changed files with 12 additions and 17 deletions
|
@ -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++)
|
||||||
|
{
|
||||||
|
bool descending = Convert.ToBoolean(i);
|
||||||
|
|
||||||
Console.WriteLine("Sorted ascending: ");
|
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)
|
foreach (Product product in products)
|
||||||
{
|
{
|
||||||
Console.WriteLine(product);
|
Console.WriteLine(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
products.Sort((x,y) => y.Price.CompareTo(x.Price));
|
|
||||||
|
|
||||||
Console.WriteLine("Sorted descending: ");
|
|
||||||
|
|
||||||
foreach (Product product in products)
|
|
||||||
{
|
|
||||||
Console.WriteLine(product);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Product
|
class Product
|
||||||
|
@ -61,3 +55,4 @@ class Product
|
||||||
Price = price;
|
Price = price;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue