📄 ccservicesimpl.java
字号:
/*
* @author : Neelesh
* @Version : 2.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : CCServicesImpl.java
* Creation/Modification History :
*
* Neelesh 04-Apr-2003 Created
*
*/
package oracle.otnsamples.ws;
/**
* Class providing credit card validation methods. This implementation
* uses LUHN algorith to validate 16 digit number cards.
*
* @author Neelesh
* @version 2.0
*/
public class CCServicesImpl implements CCServices {
/**
* This method validates the credit card number. The underlying validation
* mechanism depends on the implementation provided.
* @param <b>cardNumber</b> The credit card number
* @throws <b>CCException</b> Exception thrown if the card number is invalid.
*/
public void validateCard( String cardNumber )
throws CCException {
if( cardNumber == null ) {
throw new CCException( "Credit Card Number is null, invalid entry." );
}
int len = cardNumber.trim().length();
if( len != 16 ) {
throw new CCException( "Credit Card Number should have 16 digits" );
}
// Convert to char array of digits
char[] digits = cardNumber.toCharArray();
boolean odd = false;
int sum = 0;
int digit = 0;
// Odd number of digits in credit card
if( digits.length % 2 != 0 ) {
odd = true;
}
// LUHN Algorithm is used to validate the card .
for( int i = digits.length - 1;i >= 0;i-- ) {
digit = digits[i] - 48;
if (digit<0 || digit>9) {
throw new CCException("Credit card number has invalid characters");
}
if( ( !odd && ( i % 2 == 0 ) ) || ( odd && ( i % 2 != 0 ) ) ) {
digit *= 2;
}
if( digit > 9 ) {
sum += 1 + ( digit % 10 );
}
else {
sum += digit;
}
}
// NOTE : THIS IS COMMENTED BECAUSE WE DONT WANT USERS TO ENTER ACTUAL
// CARD NUMBERS
//if(sum % 10 !=0) throw new CCException("Invalid CreditCard");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -