Advertisement
tamaro_skaljic

GenerateCustomerIdentifier

Sep 10th, 2019 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class GenerateCustomerIdentifier {
  4.    
  5.     public static void main(String[] args) {
  6.        
  7.         GenerateCustomerId(); /* Generate a new customer number */
  8.         IsValid("KD8743311734", true); /* Check if a given customerId is valid and output this */
  9.        
  10.     }
  11.    
  12.     static String customerId;
  13.     static String validationId;
  14.     static int checksum;
  15.    
  16.     static void GenerateCustomerId() { /* Generate a new customerId */
  17.        
  18.         do{
  19.             checksum = 0;
  20.            
  21.             customerId = "KD";
  22.             for (int i = 2; i <= 9; i++) { /* Generate 8 random integers */
  23.                 int decimal = Integer.parseInt(GenerateRandomInt().charAt(0)+"");
  24.                 customerId += decimal;
  25.                 checksum += decimal;
  26.             }
  27.             customerId += checksum;
  28.            
  29.         }while(IsValid(customerId, false) == false); /* Check if the customerId is valid and don't output this. */
  30.        
  31.         System.out.printf("Here is your new customer ID: %s\n", customerId);
  32.        
  33.     }
  34.    
  35.     static String GenerateRandomInt() { /* Generate a random integer between 1 and 9 and return it as string */
  36.         return (byte)((Math.random() * 9) + 1)+"";
  37.     }
  38.    
  39.     static boolean IsValid(String customerId, boolean output) { /* Return a proof that an existing customerId is valid */
  40.        
  41.         String[] customerIdArray = customerId.split("");
  42.         boolean result;
  43.        
  44.         checksum = 0;
  45.        
  46.         validationId = "KD";
  47.         for (int i = 2; i <= 9; i++) { /* transfer the data in a new array to re-calculate the checksum */
  48.             validationId += customerIdArray[i];
  49.             checksum += Integer.parseInt(customerIdArray[i].charAt(0)+"");
  50.         }
  51.         validationId += checksum;
  52.        
  53.         if (customerId.equals(validationId)) { /* Compare the given customerId with the new generated checksum */
  54.             result = true;
  55.         } else {
  56.             result = false;
  57.         }
  58.        
  59.         if (output) {
  60.             System.out.printf("Is %s valid? %s\n", customerId, result);
  61.         }
  62.        
  63.         return result;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement