Fixed Product.cs & implemented NonFood.cs
This commit is contained in:
parent
dedbe0b6f8
commit
c91591578d
2 changed files with 41 additions and 18 deletions
|
|
@ -9,32 +9,35 @@ public sealed class NonFood : Product
|
||||||
public NonFood(string productName, string barcode, int quantity)
|
public NonFood(string productName, string barcode, int quantity)
|
||||||
: base(productName, barcode, quantity)
|
: base(productName, barcode, quantity)
|
||||||
{
|
{
|
||||||
// TODO
|
_reviews = new List<Review>();
|
||||||
_reviews = null!;
|
|
||||||
Console.WriteLine($"Remove me, I just make code compile {_reviews}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
public Review[] Reviews => _reviews.ToArray();
|
||||||
public Review[] Reviews => null!;
|
|
||||||
|
|
||||||
public double? AverageRating
|
public double? AverageRating
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
// TODO
|
if (_reviews.Count == 0)
|
||||||
return -1D;
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _reviews.Average(review => (double)review.Rating);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
protected override string[] CsvColumnNames => ["Barcode", "ProductName", "Quantity", "AverageRating"];
|
||||||
protected override string[] CsvColumnNames => null!;
|
|
||||||
|
|
||||||
// TODO
|
protected override string[] CsvColumnValues => [
|
||||||
// hint: .ToString("F1", CultureInfo.InvariantCulture)
|
Barcode,
|
||||||
protected override string[] CsvColumnValues => null!;
|
ProductName,
|
||||||
|
Quantity.ToString("F0", CultureInfo.InvariantCulture),
|
||||||
|
AverageRating?.ToString("F1", CultureInfo.InvariantCulture) ?? String.Empty
|
||||||
|
];
|
||||||
|
|
||||||
public void AddReview(Review review)
|
public void AddReview(Review review)
|
||||||
{
|
{
|
||||||
// TODO
|
_reviews.Add(review);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,10 @@ public abstract class Product
|
||||||
|
|
||||||
protected Product(string productName, string barcode, int quantity)
|
protected Product(string productName, string barcode, int quantity)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(productName))
|
||||||
|
{
|
||||||
|
productName = Invalid;
|
||||||
|
}
|
||||||
ProductName = productName;
|
ProductName = productName;
|
||||||
Barcode = IsBarcodeValid(barcode) ? barcode : Invalid;
|
Barcode = IsBarcodeValid(barcode) ? barcode : Invalid;
|
||||||
Quantity = quantity > 0 ? quantity : 0;
|
Quantity = quantity > 0 ? quantity : 0;
|
||||||
|
|
@ -26,10 +30,18 @@ public abstract class Product
|
||||||
public string GetCsvHeader()
|
public string GetCsvHeader()
|
||||||
{
|
{
|
||||||
string header = String.Empty;
|
string header = String.Empty;
|
||||||
foreach (var columnName in CsvColumnNames)
|
for (var i = 0; i < CsvColumnNames.Length; i++)
|
||||||
{
|
{
|
||||||
|
string columnName = CsvColumnNames[i];
|
||||||
|
|
||||||
|
if(i == CsvColumnNames.Length - 1)
|
||||||
|
{
|
||||||
|
header += $"{columnName}";
|
||||||
|
break;
|
||||||
|
}
|
||||||
header += $"{columnName}{Separator}";
|
header += $"{columnName}{Separator}";
|
||||||
}
|
}
|
||||||
|
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,10 +50,18 @@ public abstract class Product
|
||||||
protected static string ToCsvLine(string[] values, char separator)
|
protected static string ToCsvLine(string[] values, char separator)
|
||||||
{
|
{
|
||||||
string line = string.Empty;
|
string line = string.Empty;
|
||||||
foreach (var value in values)
|
for (var i = 0; i < values.Length; i++)
|
||||||
{
|
{
|
||||||
|
string value = values[i];
|
||||||
|
|
||||||
|
if (i == values.Length - 1)
|
||||||
|
{
|
||||||
|
line += $"{value}";
|
||||||
|
break;
|
||||||
|
}
|
||||||
line += $"{value}{separator}";
|
line += $"{value}{separator}";
|
||||||
}
|
}
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue