Implemented Food.cs & learned to use string.Join
This commit is contained in:
parent
c91591578d
commit
cc027f15af
2 changed files with 30 additions and 52 deletions
|
|
@ -8,35 +8,45 @@ public sealed class Food : Product
|
|||
public Food(string productName, string barcode, int quantity, params AllergenType[] allergens)
|
||||
: base(productName, barcode, quantity)
|
||||
{
|
||||
// TODO
|
||||
_allergens = null!;
|
||||
Console.WriteLine($"Remove me, I just make code compile {_allergens}");
|
||||
_allergens = new SortedSet<AllergenType>(allergens);
|
||||
}
|
||||
|
||||
// TODO
|
||||
public AllergenType[] Allergens => null!;
|
||||
|
||||
// TODO
|
||||
protected override string[] CsvColumnNames => null!;
|
||||
|
||||
public AllergenType[] Allergens => _allergens.ToArray();
|
||||
|
||||
protected override string[] CsvColumnNames => ["Barcode", "ProductName", "Quantity", "Allergens"];
|
||||
|
||||
protected override string[] CsvColumnValues
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO
|
||||
return null!;
|
||||
string allergens = string.Join(AllergenSeparator, Allergens);
|
||||
return [Barcode, ProductName, Quantity.ToString(), allergens];
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
public bool AddAllergen(AllergenType allergen) => false;
|
||||
|
||||
// TODO
|
||||
public bool RemoveAllergen(AllergenType allergen) => false;
|
||||
|
||||
public bool AddAllergen(AllergenType allergen)
|
||||
{
|
||||
if (ContainsAnyAllergen(allergen))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_allergens.Add(allergen);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemoveAllergen(AllergenType allergen) => _allergens.Remove(allergen);
|
||||
|
||||
public bool ContainsAnyAllergen(params AllergenType[] allergens)
|
||||
{
|
||||
// TODO
|
||||
foreach (var allergen in allergens)
|
||||
{
|
||||
if (_allergens.Contains(allergen))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,44 +27,12 @@ public abstract class Product
|
|||
|
||||
protected virtual string[] CsvColumnValues => [Barcode, ProductName, Quantity.ToString()];
|
||||
|
||||
public string GetCsvHeader()
|
||||
{
|
||||
string header = String.Empty;
|
||||
for (var i = 0; i < CsvColumnNames.Length; i++)
|
||||
{
|
||||
string columnName = CsvColumnNames[i];
|
||||
|
||||
if(i == CsvColumnNames.Length - 1)
|
||||
{
|
||||
header += $"{columnName}";
|
||||
break;
|
||||
}
|
||||
header += $"{columnName}{Separator}";
|
||||
}
|
||||
public string GetCsvHeader() => string.Join(Separator, CsvColumnNames);
|
||||
|
||||
return header;
|
||||
}
|
||||
|
||||
public string ToCsv() => ToCsvLine(CsvColumnValues, Separator);
|
||||
|
||||
protected static string ToCsvLine(string[] values, char separator)
|
||||
{
|
||||
string line = string.Empty;
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
string value = values[i];
|
||||
protected static string ToCsvLine(string[] values, char separator) => String.Join(separator, values);
|
||||
|
||||
if (i == values.Length - 1)
|
||||
{
|
||||
line += $"{value}";
|
||||
break;
|
||||
}
|
||||
line += $"{value}{separator}";
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
public static bool IsBarcodeValid(string? barcode)
|
||||
{
|
||||
if (barcode == null)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue