Implemented Product.cs
This commit is contained in:
parent
e67c72f20f
commit
dedbe0b6f8
1 changed files with 69 additions and 19 deletions
|
|
@ -8,9 +8,9 @@ public abstract class Product
|
||||||
|
|
||||||
protected Product(string productName, string barcode, int quantity)
|
protected Product(string productName, string barcode, int quantity)
|
||||||
{
|
{
|
||||||
// TODO
|
ProductName = productName;
|
||||||
ProductName = null!;
|
Barcode = IsBarcodeValid(barcode) ? barcode : Invalid;
|
||||||
Barcode = null!;
|
Quantity = quantity > 0 ? quantity : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ProductName { get; }
|
public string ProductName { get; }
|
||||||
|
|
@ -18,31 +18,81 @@ public abstract class Product
|
||||||
public string Barcode { get; }
|
public string Barcode { get; }
|
||||||
|
|
||||||
public int Quantity { get; }
|
public int Quantity { get; }
|
||||||
|
|
||||||
// TODO
|
|
||||||
protected virtual string[] CsvColumnNames => null!;
|
|
||||||
|
|
||||||
// TODO
|
protected virtual string[] CsvColumnNames => [nameof(Barcode), nameof(ProductName), nameof(Quantity)];
|
||||||
protected virtual string[] CsvColumnValues => null!;
|
|
||||||
|
|
||||||
// TODO
|
protected virtual string[] CsvColumnValues => [Barcode, ProductName, Quantity.ToString()];
|
||||||
public string GetCsvHeader() => null!;
|
|
||||||
|
public string GetCsvHeader()
|
||||||
|
{
|
||||||
|
string header = String.Empty;
|
||||||
|
foreach (var columnName in CsvColumnNames)
|
||||||
|
{
|
||||||
|
header += $"{columnName}{Separator}";
|
||||||
|
}
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToCsv() => ToCsvLine(CsvColumnValues, Separator);
|
||||||
|
|
||||||
// TODO
|
protected static string ToCsvLine(string[] values, char separator)
|
||||||
public string ToCsv() => null!;
|
{
|
||||||
|
string line = string.Empty;
|
||||||
// TODO
|
foreach (var value in values)
|
||||||
protected static string ToCsvLine(string[] values, char separator) => null!;
|
{
|
||||||
|
line += $"{value}{separator}";
|
||||||
|
}
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
public static bool IsBarcodeValid(string? barcode)
|
public static bool IsBarcodeValid(string? barcode)
|
||||||
{
|
{
|
||||||
// TODO
|
if (barcode == null)
|
||||||
return false;
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(barcode.Length is > 8 or < 8)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int evenWeight = 3;
|
||||||
|
const int oddWeight = 1;
|
||||||
|
int weight = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < barcode.Length; i++)
|
||||||
|
{
|
||||||
|
if (!char.IsDigit(barcode[i]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip check digit
|
||||||
|
if (i == 7)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int digit = int.Parse(barcode[i].ToString());
|
||||||
|
weight += i % 2 == 0 ? digit * evenWeight : digit * oddWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
int calculatedCheckDigit = 10 - (weight % 10);
|
||||||
|
if (calculatedCheckDigit == 10)
|
||||||
|
{
|
||||||
|
calculatedCheckDigit = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return calculatedCheckDigit == int.Parse(barcode[^1].ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static T[] AppendToArray<T>(T[] existingArray, params T[] newValues)
|
protected static T[] AppendToArray<T>(T[] existingArray, params T[] newValues)
|
||||||
{
|
{
|
||||||
// TODO
|
// we are allowed to use Lists, so why not? :)
|
||||||
return null!;
|
List<T> result = new(existingArray.Length + newValues.Length);
|
||||||
|
result.AddRange(existingArray);
|
||||||
|
result.AddRange(newValues);
|
||||||
|
return result.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue