completed everything

This commit is contained in:
MarcUs7i 2025-03-21 09:36:40 +01:00
parent 05406dafc8
commit 46e7e22722

View file

@ -75,7 +75,7 @@ public abstract class Product
/// <returns>True if the bar code is valid, false otherwise</returns> /// <returns>True if the bar code is valid, false otherwise</returns>
public static bool IsBarcodeValid(string? barcode) public static bool IsBarcodeValid(string? barcode)
{ {
if (barcode == null) if (string.IsNullOrEmpty(barcode) || barcode.Length != 8)
{ {
return false; return false;
} }
@ -102,13 +102,13 @@ public abstract class Product
break; break;
} }
int digit = int.Parse(barcode[i].ToString()); int digit = barcode[i] - '0';
weight += i % 2 == 0 ? digit * evenWeight : digit * oddWeight; weight += i % 2 == 0 ? digit * evenWeight : digit * oddWeight;
} }
int calculatedCheckDigit = 10 - (weight % 10) % 10; int calculatedCheckDigit = (10 - (weight % 10)) % 10;
return calculatedCheckDigit == int.Parse(barcode[^1].ToString()); return calculatedCheckDigit == barcode[^1] - '0';
} }
/// <summary> /// <summary>