Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class ISBN {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner input = new Scanner(System.in);
- Character countryCode;
- String publisherCode;
- String bookCode;
- String uniqueCode;
- String[] arrISBN;
- int sumOfISBN;
- int validasi;
- System.out.println("Masukan kode kelompok negara (1 karakter):");
- countryCode = input.nextLine().charAt(0);
- System.out.println("Masukan kode penerbit (4 karakter):");
- publisherCode = input.nextLine();
- if (publisherCode.length() > 4) {
- String cutPublisherCode = publisherCode.substring(0, 4);
- publisherCode = cutPublisherCode;
- }
- System.out.println("Masukan kode unik buku (4 karakter):");
- bookCode = input.nextLine();
- if (bookCode.length() > 4) {
- String cutbookCode = bookCode.substring(0, 4);
- bookCode = cutbookCode;
- }
- arrISBN = buildISBNArray(countryCode, publisherCode, bookCode);
- sumOfISBN = getSumOfISBN(arrISBN);
- uniqueCode = Integer.toString(sumOfISBN % 11);
- validasi = validasiISBN(arrISBN, sumOfISBN, Integer.parseInt(uniqueCode));
- System.out.println();
- System.out.println("ISBN nya adalah: ");
- System.out.println(countryCode+"-"+publisherCode+"-"+bookCode+"-"+uniqueCode);
- System.out.println();
- System.out.println(sumOfISBN+" mod 11 = "+(validasi % 11)+" atau "+sumOfISBN+" = "+(validasi % 11)+" (mod 11)");
- }
- public static String[] buildISBNArray(Character countryCode, String publisherCode, String bookCode){
- String[] arrCountryCode = new String[1];
- arrCountryCode[0] = Character.toString(countryCode);
- String[] arrPublisherCode = publisherCode.split("");
- String[] arrBookCode = bookCode.split("");
- String[] arrISBN = concatAll(arrCountryCode, arrPublisherCode, arrBookCode);
- return arrISBN;
- }
- public static int validasiISBN(String[] arrISBN, int sumOfISBN, int uniqueCode){
- return sumOfISBN + ((arrISBN.length + 1) * uniqueCode);
- }
- public static int getSumOfISBN(String[] arrISBN){
- int i, sum = 0;
- for(i=0; i<arrISBN.length; i++){
- sum = sum + ((i+1) * Integer.parseInt(arrISBN[i]));
- }
- return sum;
- }
- public static <T> T[] concatAll(T[] first, T[]... rest) {
- int totalLength = first.length;
- for (T[] array : rest) {
- totalLength += array.length;
- }
- T[] result = Arrays.copyOf(first, totalLength);
- int offset = first.length;
- for (T[] array : rest) {
- System.arraycopy(array, 0, result, offset, array.length);
- offset += array.length;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement