diff --git a/src/main/java/algorithm/VerhoeffSnippet.java b/src/main/java/algorithm/VerhoeffSnippet.java index 1a4cadf..f9051cd 100644 --- a/src/main/java/algorithm/VerhoeffSnippet.java +++ b/src/main/java/algorithm/VerhoeffSnippet.java @@ -29,67 +29,67 @@ */ public class VerhoeffSnippet { - private static final int[][] d = { - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, - {1, 0, 3, 2, 5, 4, 7, 6, 9, 8}, - {2, 3, 0, 1, 6, 7, 4, 5, 8, 9}, - {3, 2, 1, 0, 7, 6, 5, 4, 9, 8}, - {4, 5, 6, 7, 0, 1, 2, 3, 8, 9}, - {5, 4, 7, 6, 1, 0, 3, 2, 9, 8}, - {6, 7, 4, 5, 2, 3, 0, 1, 8, 9}, - {7, 6, 5, 4, 3, 2, 1, 0, 9, 8}, - {8, 9, 8, 9, 8, 9, 8, 9, 0, 1}, - {9, 8, 9, 8, 9, 8, 9, 8, 1, 0} - }; + private static final int[][] d = { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 0, 3, 2, 5, 4, 7, 6, 9, 8}, + {2, 3, 0, 1, 6, 7, 4, 5, 8, 9}, + {3, 2, 1, 0, 7, 6, 5, 4, 9, 8}, + {4, 5, 6, 7, 0, 1, 2, 3, 8, 9}, + {5, 4, 7, 6, 1, 0, 3, 2, 9, 8}, + {6, 7, 4, 5, 2, 3, 0, 1, 8, 9}, + {7, 6, 5, 4, 3, 2, 1, 0, 9, 8}, + {8, 9, 8, 9, 8, 9, 8, 9, 0, 1}, + {9, 8, 9, 8, 9, 8, 9, 8, 1, 0} + }; - private static final int[][] p = { - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, - {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, - {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, - {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, - {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, - {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, - {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, - {7, 0, 4, 6, 9, 1, 3, 2, 5, 8} - }; + private static final int[][] p = { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, + {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, + {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, + {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, + {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, + {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, + {7, 0, 4, 6, 9, 1, 3, 2, 5, 8} + }; - private static final int[] inv = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}; + private static final int[] inv = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}; - /** - * Validates a number using the Verhoeff checksum algorithm. - * - * @param num the numeric string to validate - * @return true if the number is valid according to Verhoeff algorithm, false otherwise - */ - public static boolean validateVerhoeff(String num) { - int c = 0; - int length = num.length(); + /** + * Validates a number using the Verhoeff checksum algorithm. + * + * @param num the numeric string to validate + * @return true if the number is valid according to Verhoeff algorithm, false otherwise + */ + public static boolean validateVerhoeff(String num) { + int c = 0; + int length = num.length(); - // Adjust index for validation of the full number (including check digit) - for (int i = 0; i < length; i++) { - int digit = Character.getNumericValue(num.charAt(length - i - 1)); - c = d[c][p[(i + 1) % 8][digit]]; // Correct permutation index - } - - return c == 0; // Final checksum must be zero + // Adjust index for validation of the full number (including check digit) + for (int i = 0; i < length; i++) { + int digit = Character.getNumericValue(num.charAt(length - i - 1)); + c = d[c][p[(i + 1) % 8][digit]]; // Correct permutation index } - /** - * Generates a Verhoeff check digit for a given numeric string. - * - * @param num the numeric string for which to generate the check digit - * @return the generated Verhoeff check digit as a string - */ - public static String generateVerhoeff(String num) { - int c = 0; - int length = num.length(); + return c == 0; // Final checksum must be zero + } - for (int i = 0; i < length; i++) { - int digit = Character.getNumericValue(num.charAt(length - i - 1)); - c = d[c][p[(i % 8)][digit]]; - } + /** + * Generates a Verhoeff check digit for a given numeric string. + * + * @param num the numeric string for which to generate the check digit + * @return the generated Verhoeff check digit as a string + */ + public static String generateVerhoeff(String num) { + int c = 0; + int length = num.length(); - return Integer.toString(inv[c]); + for (int i = 0; i < length; i++) { + int digit = Character.getNumericValue(num.charAt(length - i - 1)); + c = d[c][p[(i % 8)][digit]]; } + return Integer.toString(inv[c]); + } + } \ No newline at end of file diff --git a/src/test/java/algorithm/VerhoeffSnippetTest.java b/src/test/java/algorithm/VerhoeffSnippetTest.java index fb3230a..7e4f42e 100644 --- a/src/test/java/algorithm/VerhoeffSnippetTest.java +++ b/src/test/java/algorithm/VerhoeffSnippetTest.java @@ -34,45 +34,45 @@ */ class VerhoeffSnippetTest { - /** - * Tests the {@link VerhoeffSnippet#validateVerhoeff(String)} method. - */ - @Test - void testValidateVerhoeff() { - String validInput = "1428579"; // Correct Verhoeff input with a valid check digit - String invalidInput = "1428570"; // Incorrect Verhoeff input with an invalid check digit + /** + * Tests the {@link VerhoeffSnippet#validateVerhoeff(String)} method. + */ + @Test + void testValidateVerhoeff() { + String validInput = "1428579"; // Correct Verhoeff input with a valid check digit + String invalidInput = "1428570"; // Incorrect Verhoeff input with an invalid check digit - // Validate valid input - boolean isValid = VerhoeffSnippet.validateVerhoeff(validInput); - System.out.println("Testing validateVerhoeff with valid input: " - + validInput + " -> " + isValid); - assertTrue(isValid, "Expected " + validInput + " to be valid, but it was not."); + // Validate valid input + boolean isValid = VerhoeffSnippet.validateVerhoeff(validInput); + System.out.println("Testing validateVerhoeff with valid input: " + + validInput + " -> " + isValid); + assertTrue(isValid, "Expected " + validInput + " to be valid, but it was not."); - // Validate invalid input - boolean isInvalid = VerhoeffSnippet.validateVerhoeff(invalidInput); - System.out.println("Testing validateVerhoeff with invalid input: " - + invalidInput + " -> " + isInvalid); - assertFalse(isInvalid, "Expected " + invalidInput + " to be invalid, but it was not."); - } + // Validate invalid input + boolean isInvalid = VerhoeffSnippet.validateVerhoeff(invalidInput); + System.out.println("Testing validateVerhoeff with invalid input: " + + invalidInput + " -> " + isInvalid); + assertFalse(isInvalid, "Expected " + invalidInput + " to be invalid, but it was not."); + } - /** - * Tests the {@link VerhoeffSnippet#generateVerhoeff(String)} method. - */ - @Test - void testGenerateVerhoeff() { - String baseInput = "142857"; // Base input without the Verhoeff check digit + /** + * Tests the {@link VerhoeffSnippet#generateVerhoeff(String)} method. + */ + @Test + void testGenerateVerhoeff() { + String baseInput = "142857"; // Base input without the Verhoeff check digit - // Generate a Verhoeff check digit - String checkDigit = VerhoeffSnippet.generateVerhoeff(baseInput); - assertTrue("9".equals(checkDigit), - "Expected check digit to be 9 for input " + baseInput + ", but got " + checkDigit); + // Generate a Verhoeff check digit + String checkDigit = VerhoeffSnippet.generateVerhoeff(baseInput); + assertTrue("9".equals(checkDigit), + "Expected check digit to be 9 for input " + baseInput + ", but got " + checkDigit); - // Combine and validate - String fullInput = baseInput + checkDigit; - boolean isValid = VerhoeffSnippet.validateVerhoeff(fullInput); - System.out.println("Generated check digit for input " + baseInput + " -> " + checkDigit); - System.out.println("Testing validateVerhoeff with full input: " + fullInput + " -> " + isValid); + // Combine and validate + String fullInput = baseInput + checkDigit; + boolean isValid = VerhoeffSnippet.validateVerhoeff(fullInput); + System.out.println("Generated check digit for input " + baseInput + " -> " + checkDigit); + System.out.println("Testing validateVerhoeff with full input: " + fullInput + " -> " + isValid); - assertTrue(isValid, "Expected " + fullInput + " to be valid, but it was not."); - } + assertTrue(isValid, "Expected " + fullInput + " to be valid, but it was not."); + } } \ No newline at end of file