Verificar ISBN

[aux_code language=»javascript» theme=»tomorrow» title=»Verificar ISBN» extra_classes=»»]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String e = " ";

        do {
            try {
                System.out.println("Intro ISBN, FIN para cerrar:");
                e = br.readLine();
                if (e.equalsIgnoreCase("fin")) {
                    System.out.println("Me despido...");
                } else {
                    e = e.replaceAll("-", "").replaceAll(" ", "");

                    if (isValidISBN13(e)) {
                        System.out.println("OK");
                    } else {
                        System.out.println("Error");
                    }
                }
            } catch (IOException | NumberFormatException ex) {
                System.out.println("Sólo valores numéricos");
            }
        } while (!e.equalsIgnoreCase("fin"));
    }

    private static boolean isValidISBN13(String e) {
        if (e == null || e.length() != 13) {
            return false;
        }

        try {
            int total = 0;
            for (int x = 0; x < 12; x++) {
                int d = Integer.parseInt(e.substring(x, x + 1));
                total += (x % 2 == 0) ? d : d * 3;
            }

            return (total % 10) == Integer.parseInt(e.substring(12));
        } catch (NumberFormatException ex) {
            return false;
        }
    }
}
[/aux_code]
Etiquetas: Sin etiquetas

Añadir un comentario

Debes estar conectado para publicar un comentario