diff --git a/src/main/java/edu/unl/cse/soft160/isbn/implementation/ISBNVerifier.java b/src/main/java/edu/unl/cse/soft160/isbn/implementation/ISBNVerifier.java index c953529c6dd593ea6ff3e896a9a5f9081df74163..66422b243518afe4463abcfe1dfb6a9124c50a2c 100644 --- a/src/main/java/edu/unl/cse/soft160/isbn/implementation/ISBNVerifier.java +++ b/src/main/java/edu/unl/cse/soft160/isbn/implementation/ISBNVerifier.java @@ -2,34 +2,34 @@ package edu.unl.cse.soft160.isbn.implementation; public class ISBNVerifier { - private static String cleanISBN(String isbn) { - // remove non-numerics - String clean = ""; - for (int i = 0; i < isbn.length(); i++) { - Character character = isbn.charAt(i); - if (Character.isDigit(character)) { - clean += character; - } - } - return clean; - } + private static String cleanISBN(String isbn) { + // remove non-numerics + String clean = ""; + for (int i = 0; i < isbn.length(); i++) { + Character character = isbn.charAt(i); + if (Character.isDigit(character)) { + clean += character; + } + } + return clean; + } - private static boolean hasCorrectCheckDigit(String isbn) { - int checksum = 0; - for (int i = 0; i < 13; i++) { - char character = isbn.charAt(i); - int digit = Character.getNumericValue(character); - if (i % 2 == 0) { - checksum = checksum + digit; - } else { - checksum = checksum + (digit * 3); - } - } - return checksum % 10 == 0; - } + private static boolean hasCorrectCheckDigit(String isbn) { + int checksum = 0; + for (int i = 0; i < 13; i++) { + char character = isbn.charAt(i); + int digit = Character.getNumericValue(character); + if (i % 2 == 0) { + checksum = checksum + digit; + } else { + checksum = checksum + (digit * 3); + } + } + return checksum % 10 == 0; + } - public static boolean isValidISBN(String inputISBN) { - String isbn = cleanISBN(inputISBN); - return isbn.length() == 13 && hasCorrectCheckDigit(isbn); - } + public static boolean isValidISBN(String inputISBN) { + String isbn = cleanISBN(inputISBN); + return isbn.length() == 13 && hasCorrectCheckDigit(isbn); + } } diff --git a/src/main/java/edu/unl/cse/soft160/isbn/meta/GenerateVerifierTestSuite.java b/src/main/java/edu/unl/cse/soft160/isbn/meta/GenerateVerifierTestSuite.java index 58fd80d76ff0225f870357ebd00ef23d2c5ebb20..d864976e19503757b64e0f539e77b82496be7a51 100644 --- a/src/main/java/edu/unl/cse/soft160/isbn/meta/GenerateVerifierTestSuite.java +++ b/src/main/java/edu/unl/cse/soft160/isbn/meta/GenerateVerifierTestSuite.java @@ -6,79 +6,79 @@ import edu.unl.cse.soft160.isbn.implementation.ISBNVerifier; public class GenerateVerifierTestSuite { - private static Random random = new Random(); + private static Random random = new Random(); - public enum TestType { - VALID, INVALID, - } + public enum TestType { + VALID, INVALID, + } - private static int getRandomDigit() { - return random.nextInt(10); - } + private static int getRandomDigit() { + return random.nextInt(10); + } - private static TestType getRandomType() { - return TestType.values()[random.nextInt(TestType.values().length)]; - } + private static TestType getRandomType() { + return TestType.values()[random.nextInt(TestType.values().length)]; + } - private static String corruptString(String string) { - char nonDigit = (char) ('A' + random.nextInt('Z' + 1 - 'A')); - int index = random.nextInt(string.length()); - return string.substring(0, index) + nonDigit + string.substring(index + 1); - } + private static String corruptString(String string) { + char nonDigit = (char) ('A' + random.nextInt('Z' + 1 - 'A')); + int index = random.nextInt(string.length()); + return string.substring(0, index) + nonDigit + string.substring(index + 1); + } - private static void printResults(String testCases, int validISBNCount, int invalidISBNCount) { - System.out.println(testCases); - } + private static void printResults(String testCases, int validISBNCount, int invalidISBNCount) { + System.out.println(testCases); + } - private static String createTestCase(String isbn, TestType type, int testCounter) { - String truthString = Boolean.toString(type == TestType.VALID); - String test = "\t@Test\n\tpublic void test" + testCounter + "() {\n"; - test += "\t\tboolean result = ISBNVerifier.isValidISBN(\"" + isbn + "\");\n"; - test += "\t\tassert" + truthString.substring(0,1).toUpperCase() + truthString.substring(1) + "(result);\n"; - test += "\t}\n\n"; - return test; - } + private static String createTestCase(String isbn, TestType type, int testCounter) { + String truthString = Boolean.toString(type == TestType.VALID); + String test = "\t@Test\n\tpublic void test" + testCounter + "() {\n"; + test += "\t\tboolean result = ISBNVerifier.isValidISBN(\"" + isbn + "\");\n"; + test += "\t\tassert" + truthString.substring(0,1).toUpperCase() + truthString.substring(1) + "(result);\n"; + test += "\t}\n\n"; + return test; + } - private static String delimitISBN(String isbn, String delimiter) { - String result = isbn.substring(0, 3) + delimiter; - result += isbn.substring(3, 4) + delimiter; - result += isbn.substring(4, 7) + delimiter; - result += isbn.substring(7, 12) + delimiter; - result += isbn.substring(12, 13); - return result; - } + private static String delimitISBN(String isbn, String delimiter) { + String result = isbn.substring(0, 3) + delimiter; + result += isbn.substring(3, 4) + delimiter; + result += isbn.substring(4, 7) + delimiter; + result += isbn.substring(7, 12) + delimiter; + result += isbn.substring(12, 13); + return result; + } - private static String createISBN(TestType type) { - String prefix = "979000000000"; - int checkDigit = (120 - (9 * 1) + (7 * 3) + (9 * 1)) % 10; - return prefix + checkDigit; - } + private static String createISBN(TestType type) { + String prefix = "979000000000"; + int checkDigit = (120 - (9 * 1) + (7 * 3) + (9 * 1)) % 10; + return prefix + checkDigit; + } - public static void main(String... arguments) { - String testCases = ""; - int validISBNCount = 0; - int invalidISBNCount = 0; - // Generate test case number 0 - TestType type0 = getRandomType(); - String isbn0 = createISBN(type0); - String test0 = createTestCase(isbn0, type0, 0); - testCases += test0; - // Generate test case number 1 - TestType type1 = getRandomType(); - String isbn1 = createISBN(type1); - String test1 = createTestCase(isbn1, type1, 1); - testCases += test1; - // Generate test case number 2 - TestType type2 = getRandomType(); - String isbn2 = createISBN(type2); - String test2 = createTestCase(isbn2, type2, 2); - testCases += test2; - // Generate test case number 3 - TestType type3 = getRandomType(); - String isbn3 = createISBN(type3); - String test3 = createTestCase(isbn3, type3, 3); - testCases += test3; - // Print the test cases - printResults(testCases, validISBNCount, invalidISBNCount); - } + public static void main(String... arguments) { + String testCases = ""; + int validISBNCount = 0; + int invalidISBNCount = 0; + // Generate test case number 0 + TestType type0 = getRandomType(); + String isbn0 = createISBN(type0); + String test0 = createTestCase(isbn0, type0, 0); + testCases += test0; + // Generate test case number 1 + TestType type1 = getRandomType(); + String isbn1 = createISBN(type1); + String test1 = createTestCase(isbn1, type1, 1); + testCases += test1; + // Generate test case number 2 + TestType type2 = getRandomType(); + String isbn2 = createISBN(type2); + String test2 = createTestCase(isbn2, type2, 2); + testCases += test2; + // Generate test case number 3 + TestType type3 = getRandomType(); + String isbn3 = createISBN(type3); + String test3 = createTestCase(isbn3, type3, 3); + testCases += test3; + // Print the test cases + printResults(testCases, validISBNCount, invalidISBNCount); + } } diff --git a/src/test/java/edu/unl/cse/soft160/isbn/ISBNVerifierTest.java b/src/test/java/edu/unl/cse/soft160/isbn/ISBNVerifierTest.java index 9a39edf6c10e45ef0b7d1c48e7610796f925b076..5476b55466054084ea73e4bc67e7afdc39e2034b 100644 --- a/src/test/java/edu/unl/cse/soft160/isbn/ISBNVerifierTest.java +++ b/src/test/java/edu/unl/cse/soft160/isbn/ISBNVerifierTest.java @@ -8,10 +8,10 @@ import edu.unl.cse.soft160.isbn.implementation.ISBNVerifier; public class ISBNVerifierTest { - @Test - public void testSimpleExample() { - boolean result = ISBNVerifier.isValidISBN("979-3-565-87570-1"); - assertTrue(result); - } + @Test + public void testSimpleExample() { + boolean result = ISBNVerifier.isValidISBN("979-3-565-87570-1"); + assertTrue(result); + } }