diff --git a/Supermarket/Food.cs b/Supermarket/Food.cs index e89d13d..8a2b3c6 100644 --- a/Supermarket/Food.cs +++ b/Supermarket/Food.cs @@ -5,12 +5,22 @@ 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"]; @@ -24,6 +34,11 @@ public sealed class Food : Product } } + /// + /// Adds an allergen + /// + /// The allergen to add + /// True if the allergen was successfully added, false otherwise public bool AddAllergen(AllergenType allergen) { if (ContainsAnyAllergen(allergen)) @@ -35,8 +50,18 @@ public sealed class Food : Product 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) diff --git a/Supermarket/NonFood.cs b/Supermarket/NonFood.cs index a502df0..27a7f00 100644 --- a/Supermarket/NonFood.cs +++ b/Supermarket/NonFood.cs @@ -12,8 +12,14 @@ public sealed class NonFood : Product _reviews = new List(); } + /// + /// The list of reviews + /// public Review[] Reviews => _reviews.ToArray(); + /// + /// The average rating + /// public double? AverageRating { get @@ -36,6 +42,10 @@ public sealed class NonFood : Product AverageRating?.ToString("F1", CultureInfo.InvariantCulture) ?? String.Empty ]; + /// + /// Adds a review + /// + /// The review to add public void AddReview(Review review) { _reviews.Add(review); diff --git a/Supermarket/Product.cs b/Supermarket/Product.cs index 0593e0e..34bfe22 100644 --- a/Supermarket/Product.cs +++ b/Supermarket/Product.cs @@ -6,6 +6,12 @@ public abstract class Product public const string Invalid = "Invalid!"; + /// + /// The constructor of Product + /// + /// The product name + /// The bar code + /// The quantity protected Product(string productName, string barcode, int quantity) { if (string.IsNullOrWhiteSpace(productName)) @@ -17,22 +23,56 @@ public abstract class Product Quantity = quantity > 0 ? quantity : 0; } + /// + /// The product name + /// public string ProductName { get; } + /// + /// The bar code of the product + /// public string Barcode { get; } + /// + /// The quantity of the product + /// public int Quantity { get; } + /// + /// The CSV column names + /// protected virtual string[] CsvColumnNames => [nameof(Barcode), nameof(ProductName), nameof(Quantity)]; + /// + /// The CSV column values + /// protected virtual string[] CsvColumnValues => [Barcode, ProductName, Quantity.ToString()]; + /// + /// The CSV header + /// + /// The formatted CSV header public string GetCsvHeader() => string.Join(Separator, CsvColumnNames); + /// + /// The CSV line + /// + /// The formatted CSV line with values public string ToCsv() => ToCsvLine(CsvColumnValues, Separator); + /// + /// The CSV line generator + /// + /// The values + /// The separator + /// The formatted CSV line protected static string ToCsvLine(string[] values, char separator) => String.Join(separator, values); + /// + /// Checks if the bar code is valid + /// + /// The bar code to check + /// True if the bar code is valid, false otherwise public static bool IsBarcodeValid(string? barcode) { if (barcode == null) @@ -75,6 +115,13 @@ public abstract class Product return calculatedCheckDigit == int.Parse(barcode[^1].ToString()); } + /// + /// Appends new values to an existing array + /// + /// The array to append to + /// The array of values to append + /// The type + /// The complete array protected static T[] AppendToArray(T[] existingArray, params T[] newValues) { // we are allowed to use Lists, so why not? :)