Έλεγχος ISBN

import gr.aueb.dds.BIO;

/*
 * Read an ISBN number and verify if its checksum is ok.
 * Demonstrates conversion of characters to integers, post-decrement,
 * checksums.
 */
class Isbn {
        public static void main(String args[]) {
                int count = 10;         // Digit count and weight
                int sum = 0;            // Checksum
                int code, value;
                char c;

                while (count > 0) {
                        code = BIO.readChar();
                        if (code == -1) {
                                BIO.println("Short ISBN");
                                return;
                        }
                        c = (char)code;
                        // Integer representation of the digit
                        if (c == 'X')
                                value = 10;
                        else
                                // Should use Character.digit(c, 10)
                                value = c - '0';
                        if (value < 0 || value > 10) {
                                BIO.println("Bad ISBN character");
                                return;
                        }
                        sum = sum + value * count--;
                }
                if (sum % 11 == 0)
                        BIO.println("ISBN checks ok");
                else
                        BIO.println("ISBN is wrong");
        }
}

Εξηγήσεις