-
Notifications
You must be signed in to change notification settings - Fork 0
/
BarcodeNumberGenerator.cs
33 lines (28 loc) · 1.12 KB
/
BarcodeNumberGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Linq;
namespace DhlBarcodeGenerator
{
public class BarcodeNumberGenerator
{
private readonly ILuhn luhn;
public BarcodeNumberGenerator(ILuhn luhn)
{
this.luhn = luhn;
}
public string GenerateBarcodeNumber(string postNumberInput)
{
long postNumber;
if (!long.TryParse(postNumberInput, out postNumber))
{
throw new System.ArgumentException("Please enter a valid integer.");
}
var barcodeNumberWithoutCheck = (postNumber * 631).ToString();
var luhnCheckDigit = this.luhn.GenerateLuhnCheckDigit(barcodeNumberWithoutCheck);
var barcodeNumberWithCheck = string.Concat(barcodeNumberWithoutCheck, luhnCheckDigit);
var length = barcodeNumberWithCheck.Length;
var numberOfPlaceholderZeros = 16 - length - 1;
var placeholderZeros = string.Concat(Enumerable.Repeat("0", numberOfPlaceholderZeros));
var finalBarcodeNumber = $"3{placeholderZeros}{barcodeNumberWithCheck}";
return finalBarcodeNumber;
}
}
}