/* * Created on Mar 23, 2006 * * This class converts ISBN-10s to ISBN-13s */ package book2price.util; import book2price.model.data.ISBNResults; /** * This class converts ISBN-10s to ISBN-13s * @author BooksPrice.com * You may use the code in any commercial or non commercial use free of any charge. * The program is provided 'AS IS' with no guarntee of any kind. * Please visit us to compare book prices at: www.BooksPrice.com */ public class ISBNConvertUtil { /** * Converts an EAN or an ISBN 13 to ISBN 10 * @param isbn13OrEAN * @return String the ISBN 10 */ public static String convertFromIsbn13ToIsbn10(String isbn13OrEAN){ String isbn10 = ""; if (isbn13OrEAN==null){ throw new IllegalArgumentException("Not valid ISBN"); } isbn13OrEAN = isbn13OrEAN.replaceAll("-",""); if (isbn13OrEAN.length()!=13){ throw new IllegalArgumentException("Not valid ISBN"); } isbn10 = isbn13OrEAN.substring(3,isbn13OrEAN.length()-1); char [] allChars = isbn10.toCharArray(); int sum = 0; for (int i = 0; i < allChars.length; i++) { char current = allChars[i]; int currentNumber = Character.getNumericValue(current); if(currentNumber<0||currentNumber>9){ throw new IllegalArgumentException("Not valid ISBN"); } sum += currentNumber*(10-i); } int modulu = sum%11; int checkDigit = 11 - modulu; //if the checkDigit is 10 should be x if (checkDigit==10){ isbn10 += 'X'; } else if(checkDigit==11){ isbn10 += '0'; } else{ isbn10 += checkDigit; } return isbn10; } /** * Converts an isbn String of 10 to an EAN * @param isbn10 The isbn 10 * @return String the isbn 13 */ public static String convertToEAN(String isbn10){ String isbn13 = ""; String prefix = "978"; if(isbn10==null){ throw new IllegalArgumentException("Not valid ISBN"); } isbn10 = isbn10.replaceAll("-",""); if(isbn10.length()<9 || isbn10.length()>10){ throw new IllegalArgumentException("Not valid ISBN"); } //remove the last digit if(isbn10.length()==10){ isbn10 = isbn10.substring(0,9); } isbn13 = prefix + isbn10; char [] allChars = isbn13.toCharArray(); int sum = 0; for (int i = 0; i < allChars.length; i++) { char current = allChars[i]; int currentNumber = Character.getNumericValue(current); if(currentNumber<0||currentNumber>9){ throw new IllegalArgumentException("Not valid ISBN"); } if(i % 2 == 0){ sum += currentNumber; } else{ sum += currentNumber*3; } } int result = sum/10; int modulu = sum%10; int checkDigit = 10 - modulu; //if the checkDigit is 10 should be zero checkDigit=checkDigit==10?0:checkDigit; isbn13 += checkDigit; return isbn13; } /** * Converts one or several books (up to 1000) * @param groupNumber The groupNumber of the publisher (1 digit) * @param publisherNumber The publisherNumber (3 digits) * @param titleNumber The number of the title (5 digits) * @param numOfBooks The number of books to convert - max 1000 * @return String the isbn 13 */ public static String[] convertToEAN(int groupNumber,int publisherNumber, int titleNumber, int numOfBooks){ if(numOfBooks>1000||numOfBooks<1){ throw new IllegalArgumentException("Number of Books Is too large"); } String [] ret = new String[numOfBooks]; for (int i=0;i1000||numOfBooks<1){ throw new IllegalArgumentException("Number of Books Is too large"); } String [] ret = new String[numOfBooks]; for (int i=0;i