Skip to content

Commit

Permalink
checkstyle indentation fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeyadWaleed7 committed Dec 7, 2024
1 parent b1853f2 commit 89c1955
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 88 deletions.
106 changes: 53 additions & 53 deletions src/main/java/algorithm/VerhoeffSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

}
70 changes: 35 additions & 35 deletions src/test/java/algorithm/VerhoeffSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

0 comments on commit 89c1955

Please sign in to comment.