53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System.Globalization;
|
|
|
|
namespace Supermarket;
|
|
|
|
public sealed class NonFood : Product
|
|
{
|
|
private readonly List<Review> _reviews;
|
|
|
|
public NonFood(string productName, string barcode, int quantity)
|
|
: base(productName, barcode, quantity)
|
|
{
|
|
_reviews = new List<Review>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The list of reviews
|
|
/// </summary>
|
|
public Review[] Reviews => _reviews.ToArray();
|
|
|
|
/// <summary>
|
|
/// The average rating
|
|
/// </summary>
|
|
public double? AverageRating
|
|
{
|
|
get
|
|
{
|
|
if (_reviews.Count == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return _reviews.Average(review => (double)review.Rating);
|
|
}
|
|
}
|
|
|
|
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
|
|
];
|
|
|
|
/// <summary>
|
|
/// Adds a review
|
|
/// </summary>
|
|
/// <param name="review">The review to add</param>
|
|
public void AddReview(Review review)
|
|
{
|
|
_reviews.Add(review);
|
|
}
|
|
}
|