added EAN validator
This commit is contained in:
25
EANValidator/EANValidator.sln
Normal file
25
EANValidator/EANValidator.sln
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.32112.339
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EANValidator", "EANValidator\EANValidator.csproj", "{6FB2C33C-C18F-425A-BE4C-9176F622EA7C}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{6FB2C33C-C18F-425A-BE4C-9176F622EA7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{6FB2C33C-C18F-425A-BE4C-9176F622EA7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{6FB2C33C-C18F-425A-BE4C-9176F622EA7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{6FB2C33C-C18F-425A-BE4C-9176F622EA7C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {05DCC730-96C0-43C6-A882-F091287D481B}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
10
EANValidator/EANValidator/EANValidator.csproj
Normal file
10
EANValidator/EANValidator/EANValidator.csproj
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
70
EANValidator/EANValidator/Program.cs
Normal file
70
EANValidator/EANValidator/Program.cs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
string mode = "NULL"; int givenCheckNo = 0;
|
||||||
|
|
||||||
|
Console.Write("Pls input complete EAN:\n-> ");
|
||||||
|
string input = Console.ReadLine();
|
||||||
|
|
||||||
|
if (input == "q")
|
||||||
|
{
|
||||||
|
Console.WriteLine("Bye bye!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input == null || input == "") continue;
|
||||||
|
|
||||||
|
string[] split = input.Select(x => x.ToString()).ToArray();
|
||||||
|
|
||||||
|
if (split.Count() == 13) mode = "VALIDATE";
|
||||||
|
if (split.Count() == 12) mode = "GENERATE";
|
||||||
|
|
||||||
|
if (mode == "NULL") continue;
|
||||||
|
|
||||||
|
bool skip = false;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < split.Count(); i++)
|
||||||
|
{
|
||||||
|
string sign = split[i];
|
||||||
|
int val;
|
||||||
|
|
||||||
|
if (!int.TryParse(sign, out val))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"no: {val} | sum: {sum}");
|
||||||
|
|
||||||
|
if (i == 12) givenCheckNo = val;
|
||||||
|
|
||||||
|
if (i % 2 == 1)
|
||||||
|
{
|
||||||
|
val *= 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < 12) sum += val;
|
||||||
|
}
|
||||||
|
if (skip) continue;
|
||||||
|
|
||||||
|
int temp = Convert.ToInt32(Math.Ceiling(sum / 10f));
|
||||||
|
|
||||||
|
int nextFullTen = temp * 10;
|
||||||
|
|
||||||
|
int checksum = nextFullTen - sum;
|
||||||
|
|
||||||
|
string msg = $"Generated Checksum: {checksum}";
|
||||||
|
|
||||||
|
if (mode== "VALIDATE")
|
||||||
|
{
|
||||||
|
msg += $"\nGiven Checksum: {givenCheckNo}";
|
||||||
|
msg += $"\nThey do{(givenCheckNo == checksum ? "" : "n't")} match";
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine(msg);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user