@Test void testConvert10to13() { Book b; b = new Book("0306406152", "", ""); assertEquals("9780306406157", b.convertToIsbn13()); b = new Book("0596007124", "", ""); assertEquals("9780596007126", b.convertToIsbn13()); b = new Book("1491950358", "", ""); assertEquals("9781491950357", b.convertToIsbn13()); }
public String convertToIsbn13() { if(isbn.length() == 13) { return isbn; } String newIsbn = "978" + isbn.substring(0, 9); int total = 0; int multiplier = 1; for(int i = 0; i < newIsbn.length(); i++) { int digit = newIsbn.charAt(i) - '0'; total += digit * multiplier; multiplier = multiplier == 1 ? 3 : 1; } int checkDigit = 10 - (total % 10); newIsbn += String.valueOf(checkDigit); return newIsbn; }
Book { public String convertToIsbn13() { if(isbn.length() == 13) { return isbn; } String newIsbn = "978" + isbn.substring(0, 9); int total = 0; int multiplier = 1; for(int i = 0; i < newIsbn.length(); i++) { int digit = newIsbn.charAt(i) - '0'; total += digit * multiplier; multiplier = multiplier == 1 ? 3 : 1; } int checkDigit = 10 - (total % 10); newIsbn += String.valueOf(checkDigit); return newIsbn; } }
Book { public String convertToIsbn13() { if(isbn.length() == 13) { return isbn; } String newIsbn = "978" + isbn.substring(0, 9); int total = 0; int multiplier = 1; for(int i = 0; i < newIsbn.length(); i++) { int digit = newIsbn.charAt(i) - '0'; total += digit * multiplier; multiplier = multiplier == 1 ? 3 : 1; } int checkDigit = 10 - (total % 10); newIsbn += String.valueOf(checkDigit); return newIsbn; } Book(String isbn, String author, String name); }
Book { public String convertToIsbn13() { if(isbn.length() == 13) { return isbn; } String newIsbn = "978" + isbn.substring(0, 9); int total = 0; int multiplier = 1; for(int i = 0; i < newIsbn.length(); i++) { int digit = newIsbn.charAt(i) - '0'; total += digit * multiplier; multiplier = multiplier == 1 ? 3 : 1; } int checkDigit = 10 - (total % 10); newIsbn += String.valueOf(checkDigit); return newIsbn; } Book(String isbn, String author, String name); String convertToIsbn13(); String convertToIsbn10(); }
Book { public String convertToIsbn13() { if(isbn.length() == 13) { return isbn; } String newIsbn = "978" + isbn.substring(0, 9); int total = 0; int multiplier = 1; for(int i = 0; i < newIsbn.length(); i++) { int digit = newIsbn.charAt(i) - '0'; total += digit * multiplier; multiplier = multiplier == 1 ? 3 : 1; } int checkDigit = 10 - (total % 10); newIsbn += String.valueOf(checkDigit); return newIsbn; } Book(String isbn, String author, String name); String convertToIsbn13(); String convertToIsbn10();  }