Added XMLDoc where needed

This commit is contained in:
MarcUs7i 2025-03-19 22:23:39 +01:00
parent cc027f15af
commit f8ffe9c7aa
3 changed files with 82 additions and 0 deletions

View file

@ -5,12 +5,22 @@ 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"];
@ -24,6 +34,11 @@ public sealed class Food : Product
}
}
/// <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))
@ -35,8 +50,18 @@ public sealed class Food : Product
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)

View file

@ -12,8 +12,14 @@ public sealed class NonFood : Product
_reviews = new List<Review>();
}
/// <summary>
/// The list of reviews
/// </summary>
public Review[] Reviews => _reviews.ToArray();
/// <summary>
/// The average rating
/// </summary>
public double? AverageRating
{
get
@ -36,6 +42,10 @@ public sealed class NonFood : Product
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);

View file

@ -6,6 +6,12 @@ public abstract class Product
public const string Invalid = "Invalid!";
/// <summary>
/// The constructor of Product
/// </summary>
/// <param name="productName">The product name</param>
/// <param name="barcode">The bar code</param>
/// <param name="quantity">The quantity</param>
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;
}
/// <summary>
/// The product name
/// </summary>
public string ProductName { get; }
/// <summary>
/// The bar code of the product
/// </summary>
public string Barcode { get; }
/// <summary>
/// The quantity of the product
/// </summary>
public int Quantity { get; }
/// <summary>
/// The CSV column names
/// </summary>
protected virtual string[] CsvColumnNames => [nameof(Barcode), nameof(ProductName), nameof(Quantity)];
/// <summary>
/// The CSV column values
/// </summary>
protected virtual string[] CsvColumnValues => [Barcode, ProductName, Quantity.ToString()];
/// <summary>
/// The CSV header
/// </summary>
/// <returns>The formatted CSV header</returns>
public string GetCsvHeader() => string.Join(Separator, CsvColumnNames);
/// <summary>
/// The CSV line
/// </summary>
/// <returns>The formatted CSV line with values</returns>
public string ToCsv() => ToCsvLine(CsvColumnValues, Separator);
/// <summary>
/// The CSV line generator
/// </summary>
/// <param name="values">The values</param>
/// <param name="separator">The separator</param>
/// <returns>The formatted CSV line</returns>
protected static string ToCsvLine(string[] values, char separator) => String.Join(separator, values);
/// <summary>
/// Checks if the bar code is valid
/// </summary>
/// <param name="barcode">The bar code to check</param>
/// <returns>True if the bar code is valid, false otherwise</returns>
public static bool IsBarcodeValid(string? barcode)
{
if (barcode == null)
@ -75,6 +115,13 @@ public abstract class Product
return calculatedCheckDigit == int.Parse(barcode[^1].ToString());
}
/// <summary>
/// Appends new values to an existing array
/// </summary>
/// <param name="existingArray">The array to append to</param>
/// <param name="newValues">The array of values to append</param>
/// <typeparam name="T">The type</typeparam>
/// <returns>The complete array</returns>
protected static T[] AppendToArray<T>(T[] existingArray, params T[] newValues)
{
// we are allowed to use Lists, so why not? :)