Update Lesson 2 Task 1
This commit is contained in:
parent
49969a1b5f
commit
722437f3f7
1 changed files with 34 additions and 30 deletions
|
@ -8,51 +8,55 @@ List<Product> products = new List<Product>();
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
string? name;
|
string? name;
|
||||||
float price;
|
float price;
|
||||||
|
|
||||||
do
|
while (true)
|
||||||
{
|
{
|
||||||
Console.Write($"Enter product {i + 1} name: ");
|
Console.Write($"Enter product {i + 1} name: ");
|
||||||
name = Console.ReadLine();
|
name = Console.ReadLine();
|
||||||
} while (name == null);
|
if (name != null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
do
|
while (true)
|
||||||
{
|
{
|
||||||
Console.Write($"Enter product {i + 1} price: ");
|
Console.Write($"Enter product {i + 1} price: ");
|
||||||
} while (!float.TryParse(Console.ReadLine(), out price));
|
if (float.TryParse(Console.ReadLine(), out price))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
products.Add(new Product(name, price));
|
products.Add(new Product(name, price));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
for (int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
bool descending = Convert.ToBoolean(i);
|
bool descending = Convert.ToBoolean(i);
|
||||||
|
|
||||||
products.Sort((first,second) => (descending ? second.Price.CompareTo(first.Price) : first.Price.CompareTo(second.Price)));
|
products.Sort((first,second) => (descending ? second.Price.CompareTo(first.Price) : first.Price.CompareTo(second.Price)));
|
||||||
|
|
||||||
Console.WriteLine($"Sorted {(descending ? "descending" : "ascending")}:");
|
Console.WriteLine($"Sorted {(descending ? "descending" : "ascending")}:");
|
||||||
|
|
||||||
foreach (Product product in products)
|
foreach (Product product in products)
|
||||||
{
|
{
|
||||||
Console.WriteLine(product);
|
Console.WriteLine(product);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Product
|
class Product
|
||||||
{
|
{
|
||||||
public string Name { get;}
|
public string Name { get;}
|
||||||
public float Price { get;}
|
public float Price { get;}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return "Name: " + Name + ", Price: " + Price;
|
return "Name: " + Name + ", Price: " + Price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Product(string name, float price)
|
public Product(string name, float price)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
Price = price;
|
Price = price;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue