Skip to content

Commit

Permalink
Add random card number generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Nov 18, 2024
1 parent 30d2ac4 commit 7f28e86
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/test/java/com/example/CardNumberGenerator.java
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);
}
}

0 comments on commit 7f28e86

Please sign in to comment.