-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from cominvent/kid-generering
KID number generator
- Loading branch information
Showing
14 changed files
with
200 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package no.bekk.bekkopen.common; | ||
|
||
public class Checksums { | ||
public static final String ERROR_INVALID_CHECKSUM = "Invalid checksum : "; | ||
private static int[] BASE_MOD11_WEIGHTS = new int[]{2, 3, 4, 5, 6, 7}; | ||
|
||
/** | ||
* Calculate the check sum for the given weights and number. | ||
* | ||
* @param weights The weights | ||
* @param number The number | ||
* @return The checksum | ||
*/ | ||
public static int calculateMod11CheckSum(int[] weights, StringNumber number) { | ||
int c = calculateChecksum(weights, number, false) % 11; | ||
if (c == 1) { | ||
throw new IllegalArgumentException(ERROR_INVALID_CHECKSUM + number); | ||
} | ||
return c == 0 ? 0 : 11 - c; | ||
} | ||
|
||
/** | ||
* Calculate the check sum for the given weights and number. | ||
* | ||
* @param weights The weights | ||
* @param number The number | ||
* @return The checksum | ||
*/ | ||
public static int calculateMod10CheckSum(int[] weights, StringNumber number) { | ||
int c = calculateChecksum(weights, number, true) % 10; | ||
return c == 0 ? 0 : 10 - c; | ||
} | ||
|
||
public static int calculateChecksum(int[] weights, StringNumber number, boolean tverrsum) { | ||
int checkSum = 0; | ||
for (int i = 0; i < weights.length; i++) { | ||
int product = weights[i] * number.getAt(weights.length - 1 - i); | ||
if (tverrsum) { | ||
checkSum += (product > 9 ? product - 9 : product); | ||
} else { | ||
checkSum += product; | ||
} | ||
} | ||
return checkSum; | ||
} | ||
|
||
public static int[] getMod10Weights(StringNumber k) { | ||
int[] weights = new int[k.getLength() - 1]; | ||
for (int i = 0; i < weights.length; i++) { | ||
if ((i % 2) == 0) { | ||
weights[i] = 2; | ||
} else { | ||
weights[i] = 1; | ||
} | ||
} | ||
return weights; | ||
} | ||
|
||
public static int[] getMod11Weights(StringNumber k) { | ||
int[] weights = new int[k.getLength() - 1]; | ||
for (int i = 0; i < weights.length; i++) { | ||
int j = i % BASE_MOD11_WEIGHTS.length; | ||
weights[i] = BASE_MOD11_WEIGHTS[j]; | ||
} | ||
return weights; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package no.bekk.bekkopen.banking; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class KidnummerTest { | ||
@Test | ||
public void mod10Kid() throws Exception { | ||
assertValidKid("0000001230", Kidnummer.mod10Kid("123", 10)); | ||
assertValidKid("0000012344", Kidnummer.mod10Kid("1234", 10)); | ||
} | ||
|
||
@Test | ||
public void mod11Kid() throws Exception { | ||
assertValidKid("0000001236", Kidnummer.mod11Kid("123", 10)); | ||
assertValidKid("0000012343", Kidnummer.mod11Kid("1234", 10)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void tooLongBaseMod10() { | ||
Kidnummer.mod10Kid("1234567890", 3); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void tooLongBaseMod101() { | ||
Kidnummer.mod11Kid("1234567890", 3); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void belowRangeMod10() { | ||
Kidnummer.mod10Kid("12", 1); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void belowRangeMod11() { | ||
Kidnummer.mod11Kid("12", 1); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void aboveRangeMod10() { | ||
Kidnummer.mod10Kid("12", 26); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void aboveRangeMod11() { | ||
Kidnummer.mod11Kid("12", 26); | ||
} | ||
|
||
private void assertValidKid(String expected, Kidnummer kidnummer) { | ||
KidnummerValidator.isValid(kidnummer.toString()); | ||
assertEquals(expected, kidnummer.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.