-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* | ||
* Credit Card Number | ||
* https://github.com/sualeh/credit_card_number | ||
* Copyright (c) 2014-2024, Sualeh Fatehi. | ||
* | ||
*/ | ||
|
||
package com.example; | ||
|
||
import java.util.Random; | ||
|
||
public class CardNumberGenerator { | ||
|
||
public static int calculateCheckDigit(final String cardNumber) { | ||
int sum = 0; | ||
boolean alternate = true; | ||
|
||
for (int i = cardNumber.length() - 1; i >= 0; i--) { | ||
int n = Integer.parseInt(cardNumber.substring(i, i + 1)); | ||
|
||
if (alternate) { | ||
n *= 2; | ||
if (n > 9) { | ||
n = n % 10 + 1; | ||
} | ||
} | ||
|
||
sum += n; | ||
alternate = !alternate; | ||
} | ||
|
||
return (10 - sum % 10) % 10; | ||
} | ||
|
||
public static String generateRandomCardNumber(final String bin) { | ||
final Random random = new Random(); | ||
final StringBuilder cardNumber = new StringBuilder(bin); | ||
|
||
// Generate the remaining digits except the last one | ||
while (cardNumber.length() < 15) { | ||
cardNumber.append(random.nextInt(10)); | ||
} | ||
|
||
// Calculate the check digit | ||
final int checkDigit = calculateCheckDigit(cardNumber.toString()); | ||
cardNumber.append(checkDigit); | ||
|
||
return cardNumber.toString(); | ||
} | ||
|
||
public static void main(final String[] args) { | ||
final String bin = args[0]; | ||
final String cardNumber = generateRandomCardNumber(bin); | ||
System.out.println("Generated Card Number: " + cardNumber); | ||
} | ||
} |