Wasteful repeated method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasteful repeated method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasteful repeated method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
WastedWasteful repeated method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasted method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasteful repeated method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasted method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.
Wasted method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Wasted method calls
You're calling str.length()
multiple times within the same scope. It would be better to call only once, save the result and reuse:
int length = str.length();
Input validation
As @Pimgd pointed out, you may get a NumberFormatException
here:
array[i] = Integer.parseInt("" + reverse.charAt(i));
I propose validating like this:
int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
return false;
}
Naming
These methods could be named better:
- Instead of
validate
, how aboutisValid
- Instead of
creditCardType
, how aboutgetCreditCardType
These variables could be named better:
- Instead of
input1Char
, how aboutfirst1
- Instead of
input2Chars
, how aboutfirst2
- Instead of
input6Chars
, how aboutfirst6
- ... and so on
Unit testing
It's important to test validators thoroughly. Consider adding unit tests, for example:
@Test
public void testValidVisa() {
assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}
These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.