namespace Supermarket; public sealed class Food : Product { private const char AllergenSeparator = '|'; private readonly SortedSet _allergens; /// /// The constructor of Food /// /// The product name /// The bar code /// The quantity /// The allergens public Food(string productName, string barcode, int quantity, params AllergenType[] allergens) : base(productName, barcode, quantity) { _allergens = new SortedSet(allergens); } /// /// The allergens /// 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]; } } /// /// Adds an allergen /// /// The allergen to add /// True if the allergen was successfully added, false otherwise public bool AddAllergen(AllergenType allergen) { if (ContainsAnyAllergen(allergen)) { return false; } _allergens.Add(allergen); return true; } /// /// Removes an allergen /// /// The allergen to remove /// True if the allergen was successfully removed, false otherwise public bool RemoveAllergen(AllergenType allergen) => _allergens.Remove(allergen); /// /// Checks if the food contains any of the given allergens /// /// The allergens to check /// True if any of the allergens are contained, false otherwise public bool ContainsAnyAllergen(params AllergenType[] allergens) { foreach (var allergen in allergens) { if (_allergens.Contains(allergen)) { return true; } } return false; } }