Advertisement
Guest User

ISBN.java

a guest
Mar 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class ISBN {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         Scanner input = new Scanner(System.in);
  9.         Character countryCode;
  10.         String publisherCode;
  11.         String bookCode;
  12.         String uniqueCode;
  13.         String[] arrISBN;
  14.         int sumOfISBN;
  15.         int validasi;
  16.  
  17.         System.out.println("Masukan kode kelompok negara (1 karakter):");
  18.         countryCode = input.nextLine().charAt(0);
  19.  
  20.         System.out.println("Masukan kode penerbit (4 karakter):");
  21.         publisherCode = input.nextLine();
  22.  
  23.         if (publisherCode.length() > 4) {
  24.             String cutPublisherCode = publisherCode.substring(0, 4);
  25.             publisherCode = cutPublisherCode;
  26.         }
  27.  
  28.         System.out.println("Masukan kode unik buku (4 karakter):");
  29.         bookCode = input.nextLine();
  30.  
  31.         if (bookCode.length() > 4) {
  32.             String cutbookCode = bookCode.substring(0, 4);
  33.             bookCode = cutbookCode;
  34.         }
  35.  
  36.         arrISBN = buildISBNArray(countryCode, publisherCode, bookCode);
  37.         sumOfISBN = getSumOfISBN(arrISBN);
  38.         uniqueCode = Integer.toString(sumOfISBN % 11);
  39.         validasi = validasiISBN(arrISBN, sumOfISBN, Integer.parseInt(uniqueCode));
  40.  
  41.         System.out.println();
  42.         System.out.println("ISBN nya adalah: ");
  43.         System.out.println(countryCode+"-"+publisherCode+"-"+bookCode+"-"+uniqueCode);
  44.         System.out.println();
  45.         System.out.println(sumOfISBN+" mod 11 = "+(validasi % 11)+" atau "+sumOfISBN+" = "+(validasi % 11)+" (mod 11)");
  46.     }
  47.  
  48.     public static String[] buildISBNArray(Character countryCode, String publisherCode, String bookCode){
  49.         String[] arrCountryCode = new String[1];
  50.         arrCountryCode[0] = Character.toString(countryCode);
  51.         String[] arrPublisherCode = publisherCode.split("");
  52.         String[] arrBookCode = bookCode.split("");
  53.         String[] arrISBN = concatAll(arrCountryCode, arrPublisherCode, arrBookCode);
  54.  
  55.         return arrISBN;
  56.     }
  57.  
  58.     public static int validasiISBN(String[] arrISBN, int sumOfISBN, int uniqueCode){
  59.         return sumOfISBN + ((arrISBN.length + 1) * uniqueCode);
  60.     }
  61.  
  62.     public static int getSumOfISBN(String[] arrISBN){
  63.  
  64.         int i, sum = 0;
  65.  
  66.         for(i=0; i<arrISBN.length; i++){
  67.             sum = sum + ((i+1) * Integer.parseInt(arrISBN[i]));
  68.         }
  69.  
  70.         return sum;
  71.     }
  72.  
  73.     public static <T> T[] concatAll(T[] first, T[]... rest) {
  74.         int totalLength = first.length;
  75.         for (T[] array : rest) {
  76.             totalLength += array.length;
  77.         }
  78.         T[] result = Arrays.copyOf(first, totalLength);
  79.         int offset = first.length;
  80.         for (T[] array : rest) {
  81.             System.arraycopy(array, 0, result, offset, array.length);
  82.             offset += array.length;
  83.         }
  84.         return result;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement