-
Mar 30th, 2024, 07:43 AM
#1
Lot Code Generator
In manufacturing, it’s common to add a lot code to products to identify when or where they were made. The lot codes are usually made up of alphanumeric characters and are sequential in nature so. For example, a lot code sequence might look like the following, ABCD, ABCE, ABCF, etc.
When creating the lot codes, it’s also good practice to not use specific letters such as I, O, Q, S, W and others as these characters can be misinterpreted as other letters or numbers. The challenge is to create a method that will automatically generate a series of unique lot codes using a group of specific alphanumeric characters. The c# class below does that by converting a given lot code to base 10, adding 1 to it, then converting back to a lot code using only the specific characters that are to be included in any given lot code. Enjoy!
Code:
public static class LotCodeGenerator
{
/// <summary>
/// This method works by converting the given lotcode to base 10,
/// adding 1 to it, then converting it back to the lotcode base
/// </summary>
/// <param name="lotCode">The starting lotcode.</param>
/// <param name="allowedLotCodeCharacters">The characters that are allowed in the lotcode.</param>
/// <param name="minLength">The minimum length of the returned lotcode.</param>
/// <returns>The return value will be padded on the left with the first allowed lotcode character
/// if its length is less than the min length.
/// </returns>
public static string NextLotCode(string lotCode, string allowedLotCodeCharacters, int minLength)
{
//convert the lot code to an int
int lotCodeInt = alphaLotToInt(lotCode, allowedLotCodeCharacters);
//increment it by one
lotCodeInt++;
//convert back to a lot code
string ret = intToAlphaLot(lotCodeInt, allowedLotCodeCharacters, minLength);
return ret;
}
private static string intToAlphaLot(int value, string allowedLotCodeCharacters, int minReturnLength)
{
string ret = "";
//the base of the return value will be the number of allowed characters
int lotCodeBase = allowedLotCodeCharacters.Length;
//convert the int value to a lot code using by converting its base
while (value > 0)
{
decimal x = value / lotCodeBase;
int q = (int)Math.Floor(x);
int remainer = value - (q * lotCodeBase);
char c = allowedLotCodeCharacters[remainer];
ret = c + ret;
value = q;
}
//pad the return with the first lot code char
ret = ret.PadLeft(minReturnLength, allowedLotCodeCharacters[0]);
return ret;
}
private static int alphaLotToInt(string lotCode, string allowedLotCodeCharacters)
{
//make sure all of the lotCode characters are in the allowedLotCodeCharacters
foreach (char c in lotCode)
{
if (!allowedLotCodeCharacters.Contains(c))
throw new Exception("The lot code has characters that are not in the allowed lot code characters");
}
int ret = 0;
int lotCodeBase = allowedLotCodeCharacters.Length;
int power = 0;
foreach (char c in lotCode.Reverse())
{
int charValue = allowedLotCodeCharacters.IndexOf(c);
//raise the base to the power, multiply it by the char index and add it to the return
int addValue = charValue * (int)(Math.Pow(lotCodeBase, power++));
ret += addValue;
}
return ret;
}
}
Usage:
Code:
string allowedLotCodeCharacters = "ABCDEFGHJKLMNPRTUVXYZ";
string lotCode = "AAAA";
int minLength = 4;
string nextLotCode = LotCodeGenerator.NextLotCode(lotCode, allowedLotCodeCharacters, minLength);
Console.WriteLine($"{lotCode} --> {nextLotCode}");
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|