Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class GenerateCustomerIdentifier {
- public static void main(String[] args) {
- GenerateCustomerId(); /* Generate a new customer number */
- IsValid("KD8743311734", true); /* Check if a given customerId is valid and output this */
- }
- static String customerId;
- static String validationId;
- static int checksum;
- static void GenerateCustomerId() { /* Generate a new customerId */
- do{
- checksum = 0;
- customerId = "KD";
- for (int i = 2; i <= 9; i++) { /* Generate 8 random integers */
- int decimal = Integer.parseInt(GenerateRandomInt().charAt(0)+"");
- customerId += decimal;
- checksum += decimal;
- }
- customerId += checksum;
- }while(IsValid(customerId, false) == false); /* Check if the customerId is valid and don't output this. */
- System.out.printf("Here is your new customer ID: %s\n", customerId);
- }
- static String GenerateRandomInt() { /* Generate a random integer between 1 and 9 and return it as string */
- return (byte)((Math.random() * 9) + 1)+"";
- }
- static boolean IsValid(String customerId, boolean output) { /* Return a proof that an existing customerId is valid */
- String[] customerIdArray = customerId.split("");
- boolean result;
- checksum = 0;
- validationId = "KD";
- for (int i = 2; i <= 9; i++) { /* transfer the data in a new array to re-calculate the checksum */
- validationId += customerIdArray[i];
- checksum += Integer.parseInt(customerIdArray[i].charAt(0)+"");
- }
- validationId += checksum;
- if (customerId.equals(validationId)) { /* Compare the given customerId with the new generated checksum */
- result = true;
- } else {
- result = false;
- }
- if (output) {
- System.out.printf("Is %s valid? %s\n", customerId, result);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement