Fixed Product.cs & implemented NonFood.cs

This commit is contained in:
MarcUs7i 2025-03-19 22:02:13 +01:00
parent dedbe0b6f8
commit c91591578d
2 changed files with 41 additions and 18 deletions

View file

@ -9,32 +9,35 @@ public sealed class NonFood : Product
public NonFood(string productName, string barcode, int quantity)
: base(productName, barcode, quantity)
{
// TODO
_reviews = null!;
Console.WriteLine($"Remove me, I just make code compile {_reviews}");
_reviews = new List<Review>();
}
// TODO
public Review[] Reviews => null!;
public Review[] Reviews => _reviews.ToArray();
public double? AverageRating
{
get
{
// TODO
return -1D;
if (_reviews.Count == 0)
{
return null;
}
return _reviews.Average(review => (double)review.Rating);
}
}
// TODO
protected override string[] CsvColumnNames => null!;
// TODO
// hint: .ToString("F1", CultureInfo.InvariantCulture)
protected override string[] CsvColumnValues => null!;
protected override string[] CsvColumnNames => ["Barcode", "ProductName", "Quantity", "AverageRating"];
protected override string[] CsvColumnValues => [
Barcode,
ProductName,
Quantity.ToString("F0", CultureInfo.InvariantCulture),
AverageRating?.ToString("F1", CultureInfo.InvariantCulture) ?? String.Empty
];
public void AddReview(Review review)
{
// TODO
_reviews.Add(review);
}
}

View file

@ -8,6 +8,10 @@ public abstract class Product
protected Product(string productName, string barcode, int quantity)
{
if (string.IsNullOrWhiteSpace(productName))
{
productName = Invalid;
}
ProductName = productName;
Barcode = IsBarcodeValid(barcode) ? barcode : Invalid;
Quantity = quantity > 0 ? quantity : 0;
@ -26,10 +30,18 @@ public abstract class Product
public string GetCsvHeader()
{
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}";
}
return header;
}
@ -38,10 +50,18 @@ public abstract class Product
protected static string ToCsvLine(string[] values, char separator)
{
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}";
}
return line;
}