77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
namespace Supermarket;
|
|
|
|
public sealed class Food : Product
|
|
{
|
|
private const char AllergenSeparator = '|';
|
|
private readonly SortedSet<AllergenType> _allergens;
|
|
|
|
/// <summary>
|
|
/// The constructor of Food
|
|
/// </summary>
|
|
/// <param name="productName">The product name</param>
|
|
/// <param name="barcode">The bar code</param>
|
|
/// <param name="quantity">The quantity</param>
|
|
/// <param name="allergens">The allergens</param>
|
|
public Food(string productName, string barcode, int quantity, params AllergenType[] allergens)
|
|
: base(productName, barcode, quantity)
|
|
{
|
|
_allergens = new SortedSet<AllergenType>(allergens);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The allergens
|
|
/// </summary>
|
|
public AllergenType[] Allergens => _allergens.ToArray();
|
|
|
|
protected override string[] CsvColumnNames => ["Barcode", "ProductName", "Quantity", "Allergens"];
|
|
|
|
protected override string[] CsvColumnValues
|
|
{
|
|
get
|
|
{
|
|
string allergens = string.Join(AllergenSeparator, Allergens);
|
|
return [Barcode, ProductName, Quantity.ToString(), allergens];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an allergen
|
|
/// </summary>
|
|
/// <param name="allergen">The allergen to add</param>
|
|
/// <returns>True if the allergen was successfully added, false otherwise</returns>
|
|
public bool AddAllergen(AllergenType allergen)
|
|
{
|
|
if (ContainsAnyAllergen(allergen))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_allergens.Add(allergen);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes an allergen
|
|
/// </summary>
|
|
/// <param name="allergen">The allergen to remove</param>
|
|
/// <returns>True if the allergen was successfully removed, false otherwise</returns>
|
|
public bool RemoveAllergen(AllergenType allergen) => _allergens.Remove(allergen);
|
|
|
|
/// <summary>
|
|
/// Checks if the food contains any of the given allergens
|
|
/// </summary>
|
|
/// <param name="allergens">The allergens to check</param>
|
|
/// <returns>True if any of the allergens are contained, false otherwise</returns>
|
|
public bool ContainsAnyAllergen(params AllergenType[] allergens)
|
|
{
|
|
foreach (var allergen in allergens)
|
|
{
|
|
if (_allergens.Contains(allergen))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|