Advertisement
Guest User

Reto semanal

a guest
Aug 8th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. public class Ejecutable {
  2.  
  3.     public static void main(String[] args) {
  4.         testMethod("0-7475-3269-9", "3-04-013311-X", "304013311X",
  5.                 "0747532699", "A0-7475-3269-9", "045747535679", "0000000000");
  6.     }
  7.  
  8.     public static boolean isISBNCode(final String pCode) {
  9.         boolean result = false;
  10.         if (pCode != null && !pCode.isEmpty()) {
  11.             // Verifico el formato en que viene y lo estandarizo:
  12.             // Si viene con "-" los elimino, si viene con letras no es válido.
  13.             String codeFormat = pCode;
  14.             if (pCode.contains("-")) {
  15.                 codeFormat = pCode.replace('-', ' ');
  16.                 codeFormat = codeFormat.replaceAll("\\s+", "");
  17.             }
  18.             if (codeFormat.length() == 10
  19.                     && tryParseInt(codeFormat.substring(0,
  20.                             codeFormat.length() - 2))) {
  21.                 String lastDigit = String.valueOf(codeFormat.charAt(codeFormat
  22.                         .length() - 1));
  23.                 if (tryParseInt(lastDigit)
  24.                         || "X".equals(lastDigit.toUpperCase())) {
  25.  
  26.                     char[] codeArray = codeFormat.toCharArray();
  27.  
  28.                     int position = 10;
  29.                     int totalSum = 0;
  30.                     for (char character : codeArray) {
  31.                         if (position != 1) {
  32.                             totalSum = totalSum
  33.                                     + (Integer.parseInt(character + "") * position);
  34.                             position--;
  35.                         } else {
  36.                             // Último dígito, hay que verificar.
  37.                             if ("X".equals(character + "".toUpperCase())) {
  38.                                 totalSum = totalSum
  39.                                         + (Integer.parseInt("10") * position);
  40.                             } else {
  41.                                 totalSum = totalSum
  42.                                         + (Integer.parseInt(character + "") * position);
  43.                             }
  44.                         }
  45.                     }
  46.                     // Verifico si el totalSum es múltiplo de 11.
  47.                     if (totalSum % 11 == 0) {
  48.                         result = true;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         return result;
  54.     }
  55.  
  56.     public static boolean tryParseInt(final String pString) {
  57.         boolean result = false;
  58.         try {
  59.             Integer.parseInt(pString);
  60.             result = true;
  61.         } catch (NumberFormatException exception) {
  62.             result = false;
  63.         }
  64.         return result;
  65.     }
  66.  
  67.     public static void testMethod(final String... pValues) {
  68.         if (pValues.length > 0) {
  69.             for (String value : pValues) {
  70.                 if (isISBNCode(value)) {
  71.                     System.out.println("El código " + value + " es válido.");
  72.                 } else {
  73.                     System.out.println("El código " + value + " NO es válido.");
  74.                 }
  75.             }
  76.         }
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement