Sunday, May 14, 2017

What is Program Design by Example - ISBN-10

Writing a program by an engineer starts, just like creating the blueprint of a building by an architect, by design.  Before we get to the design phase, we have to understand and then analyze the problem.

Problem:  Given a book title and its ISBN-10 number, you need to validate if the ISBN number is valid or not.
Starting Out with Java: Early Objects (5th Edition) 5th Edition
ISBN-10: 0133776743

Analysis:  ISBN-10 is a unique number that must be generated based on an algorithm that must ensure its unique value and its validity must be verifiable.  Let's find out how the algorithm works and if there is a reliable source that we can refer to.
I can see that it does have a check digit at the end of the number:
https://www.isbn-international.org/content/standard-book-numbering-turns-50
I can see how it is calculated in ISBN-13, but not in ISBN-10
https://www.isbn-international.org/sites/default/files/ISBN%20Manual%202012%20-corr.pdf
I got it, this looks reliable source
https://www.isbn-information.com/the-10-digit-isbn.html
Let's work out the example in the document to see if I understand it.
Now, let's use the given ISBN and do the calculation by hand on paper so I'll understand how it is done, before I decide on how to write a program.
So, the number is 0133776743
Let's multiply 0*10+1*9+3*8+3*7+7*6+7*5+6*4+7*3+4*2 = 184
184 mod 11 = 8
Thus, 11-8=3 is the correct remainder, so it must be correct.

Design:
start
    input number
    while number length is not 10             //validate length
           input number
    endwhile
    set index=0
    while index < 9 then                           //skip the last digit
           if number[index] is digit then
               total=total+number[index]*(10-index)         //accumulator
           else
                output "Not a digit, bye"                 //validate each digit
                exit program with code 1
           index=index+1
   endwhile
   total=total mod 11
   if total eq 10 then               //show 0 for remainder 10     
       output "The check digit is: "+0
   else
       output "The check digit is: "+11-total
   endif
stop


Code:
import java.util.Scanner;




/** Application to calculate ISBN-10 check digit
 @author Zoltan Szabo
 @version 1.0.0
*/
public class MyISBN10{
public static void main(String args[]){
    Scanner kbd=new Scanner(System.in);
    String isbn;
    int total=0;
    System.out.print("Please, enter an ISBN-10 number: ");
    isbn=kbd.nextLine();
    while(isbn.length()!=10){  //validate input length
        System.out.println("This application can only handle ISBN-10, do not type more than 10 digits.");
        System.out.print("Again: Please, enter an ISBN-10 number: ");
        isbn=kbd.nextLine();
    }
    for(int index=0;index<9;index++){   //process one digit at a time
        if(Character.isDigit(isbn.charAt(index)))  //validate if number was read
           total+=Integer.parseInt(isbn.substring(index,index+1))*(10-index);
         else{
            System.out.print("Not a digit, bye!");
            System.exit(1);
            }
    }
    System.out.print("The check digit is: "+(((total%11)==10)?0:11-(total%11)));
}
}

Test: ( not just valid, but also invalid inputs )

Please, enter an ISBN-10 number: 0133776743
The check digit is: 3
Please, enter an ISBN-10 number: as0133776743
This application can only handle ISBN-10, do not type more than 10 digits.
Again: Please, enter an ISBN-10 number: 01337767aa
Not a digit, bye!

Challenge:  Do the same for ISBN-13

No comments:

Post a Comment